ScsiDisk.c 201 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419
  1. /** @file
  2. SCSI disk driver that layers on every SCSI IO protocol in the system.
  3. Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
  4. Copyright (c) 1985 - 2022, American Megatrends International LLC.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "ScsiDisk.h"
  8. EFI_DRIVER_BINDING_PROTOCOL gScsiDiskDriverBinding = {
  9. ScsiDiskDriverBindingSupported,
  10. ScsiDiskDriverBindingStart,
  11. ScsiDiskDriverBindingStop,
  12. 0xa,
  13. NULL,
  14. NULL
  15. };
  16. EFI_DISK_INFO_PROTOCOL gScsiDiskInfoProtocolTemplate = {
  17. EFI_DISK_INFO_SCSI_INTERFACE_GUID,
  18. ScsiDiskInfoInquiry,
  19. ScsiDiskInfoIdentify,
  20. ScsiDiskInfoSenseData,
  21. ScsiDiskInfoWhichIde
  22. };
  23. /**
  24. Allocates an aligned buffer for SCSI disk.
  25. This function allocates an aligned buffer for the SCSI disk to perform
  26. SCSI IO operations. The alignment requirement is from SCSI IO interface.
  27. @param ScsiDiskDevice The SCSI disk involved for the operation.
  28. @param BufferSize The request buffer size.
  29. @return A pointer to the aligned buffer or NULL if the allocation fails.
  30. **/
  31. VOID *
  32. AllocateAlignedBuffer (
  33. IN SCSI_DISK_DEV *ScsiDiskDevice,
  34. IN UINTN BufferSize
  35. )
  36. {
  37. return AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize), ScsiDiskDevice->ScsiIo->IoAlign);
  38. }
  39. /**
  40. Frees an aligned buffer for SCSI disk.
  41. This function frees an aligned buffer for the SCSI disk to perform
  42. SCSI IO operations.
  43. @param Buffer The aligned buffer to be freed.
  44. @param BufferSize The request buffer size.
  45. **/
  46. VOID
  47. FreeAlignedBuffer (
  48. IN VOID *Buffer,
  49. IN UINTN BufferSize
  50. )
  51. {
  52. if (Buffer != NULL) {
  53. FreeAlignedPages (Buffer, EFI_SIZE_TO_PAGES (BufferSize));
  54. }
  55. }
  56. /**
  57. Remove trailing spaces from the string.
  58. @param String The ASCII string to remove the trailing spaces.
  59. @retval the new length of the string.
  60. **/
  61. UINTN
  62. RemoveTrailingSpaces (
  63. IN OUT CHAR8 *String
  64. )
  65. {
  66. UINTN Length;
  67. Length = AsciiStrLen (String);
  68. if (Length == 0) {
  69. return 0;
  70. }
  71. while ((Length > 0) && (String[Length-1] == ' ')) {
  72. Length--;
  73. }
  74. String[Length] = '\0';
  75. return Length;
  76. }
  77. /**
  78. The user Entry Point for module ScsiDisk.
  79. The user code starts with this function.
  80. @param ImageHandle The firmware allocated handle for the EFI image.
  81. @param SystemTable A pointer to the EFI System Table.
  82. @retval EFI_SUCCESS The entry point is executed successfully.
  83. @retval other Some error occurs when executing this entry point.
  84. **/
  85. EFI_STATUS
  86. EFIAPI
  87. InitializeScsiDisk (
  88. IN EFI_HANDLE ImageHandle,
  89. IN EFI_SYSTEM_TABLE *SystemTable
  90. )
  91. {
  92. EFI_STATUS Status;
  93. //
  94. // Install driver model protocol(s).
  95. //
  96. Status = EfiLibInstallDriverBindingComponentName2 (
  97. ImageHandle,
  98. SystemTable,
  99. &gScsiDiskDriverBinding,
  100. ImageHandle,
  101. &gScsiDiskComponentName,
  102. &gScsiDiskComponentName2
  103. );
  104. ASSERT_EFI_ERROR (Status);
  105. return Status;
  106. }
  107. /**
  108. Test to see if this driver supports ControllerHandle.
  109. This service is called by the EFI boot service ConnectController(). In order
  110. to make drivers as small as possible, there are a few calling restrictions for
  111. this service. ConnectController() must follow these calling restrictions.
  112. If any other agent wishes to call Supported() it must also follow these
  113. calling restrictions.
  114. @param This Protocol instance pointer.
  115. @param ControllerHandle Handle of device to test
  116. @param RemainingDevicePath Optional parameter use to pick a specific child
  117. device to start.
  118. @retval EFI_SUCCESS This driver supports this device
  119. @retval EFI_ALREADY_STARTED This driver is already running on this device
  120. @retval other This driver does not support this device
  121. **/
  122. EFI_STATUS
  123. EFIAPI
  124. ScsiDiskDriverBindingSupported (
  125. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  126. IN EFI_HANDLE Controller,
  127. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
  128. )
  129. {
  130. EFI_STATUS Status;
  131. EFI_SCSI_IO_PROTOCOL *ScsiIo;
  132. UINT8 DeviceType;
  133. Status = gBS->OpenProtocol (
  134. Controller,
  135. &gEfiScsiIoProtocolGuid,
  136. (VOID **)&ScsiIo,
  137. This->DriverBindingHandle,
  138. Controller,
  139. EFI_OPEN_PROTOCOL_BY_DRIVER
  140. );
  141. if (EFI_ERROR (Status)) {
  142. return Status;
  143. }
  144. Status = ScsiIo->GetDeviceType (ScsiIo, &DeviceType);
  145. if (!EFI_ERROR (Status)) {
  146. if ((DeviceType == EFI_SCSI_TYPE_DISK) ||
  147. (DeviceType == EFI_SCSI_TYPE_CDROM) ||
  148. (DeviceType == EFI_SCSI_TYPE_WLUN))
  149. {
  150. Status = EFI_SUCCESS;
  151. } else {
  152. Status = EFI_UNSUPPORTED;
  153. }
  154. }
  155. gBS->CloseProtocol (
  156. Controller,
  157. &gEfiScsiIoProtocolGuid,
  158. This->DriverBindingHandle,
  159. Controller
  160. );
  161. return Status;
  162. }
  163. /**
  164. Start this driver on ControllerHandle.
  165. This service is called by the EFI boot service ConnectController(). In order
  166. to make drivers as small as possible, there are a few calling restrictions for
  167. this service. ConnectController() must follow these calling restrictions. If
  168. any other agent wishes to call Start() it must also follow these calling
  169. restrictions.
  170. @param This Protocol instance pointer.
  171. @param ControllerHandle Handle of device to bind driver to
  172. @param RemainingDevicePath Optional parameter use to pick a specific child
  173. device to start.
  174. @retval EFI_SUCCESS This driver is added to ControllerHandle
  175. @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
  176. @retval other This driver does not support this device
  177. **/
  178. EFI_STATUS
  179. EFIAPI
  180. ScsiDiskDriverBindingStart (
  181. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  182. IN EFI_HANDLE Controller,
  183. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
  184. )
  185. {
  186. EFI_STATUS Status;
  187. EFI_SCSI_IO_PROTOCOL *ScsiIo;
  188. SCSI_DISK_DEV *ScsiDiskDevice;
  189. BOOLEAN Temp;
  190. UINT8 Index;
  191. UINT8 MaxRetry;
  192. BOOLEAN NeedRetry;
  193. BOOLEAN MustReadCapacity;
  194. CHAR8 VendorStr[VENDOR_IDENTIFICATION_LENGTH + 1];
  195. CHAR8 ProductStr[PRODUCT_IDENTIFICATION_LENGTH + 1];
  196. CHAR16 DeviceStr[VENDOR_IDENTIFICATION_LENGTH + PRODUCT_IDENTIFICATION_LENGTH + 2];
  197. MustReadCapacity = TRUE;
  198. ScsiDiskDevice = (SCSI_DISK_DEV *)AllocateZeroPool (sizeof (SCSI_DISK_DEV));
  199. if (ScsiDiskDevice == NULL) {
  200. return EFI_OUT_OF_RESOURCES;
  201. }
  202. Status = gBS->OpenProtocol (
  203. Controller,
  204. &gEfiScsiIoProtocolGuid,
  205. (VOID **)&ScsiIo,
  206. This->DriverBindingHandle,
  207. Controller,
  208. EFI_OPEN_PROTOCOL_BY_DRIVER
  209. );
  210. if (EFI_ERROR (Status)) {
  211. FreePool (ScsiDiskDevice);
  212. return Status;
  213. }
  214. ScsiDiskDevice->Signature = SCSI_DISK_DEV_SIGNATURE;
  215. ScsiDiskDevice->ScsiIo = ScsiIo;
  216. ScsiDiskDevice->BlkIo.Revision = EFI_BLOCK_IO_PROTOCOL_REVISION3;
  217. ScsiDiskDevice->BlkIo.Media = &ScsiDiskDevice->BlkIoMedia;
  218. ScsiDiskDevice->BlkIo.Media->IoAlign = ScsiIo->IoAlign;
  219. ScsiDiskDevice->BlkIo.Reset = ScsiDiskReset;
  220. ScsiDiskDevice->BlkIo.ReadBlocks = ScsiDiskReadBlocks;
  221. ScsiDiskDevice->BlkIo.WriteBlocks = ScsiDiskWriteBlocks;
  222. ScsiDiskDevice->BlkIo.FlushBlocks = ScsiDiskFlushBlocks;
  223. ScsiDiskDevice->BlkIo2.Media = &ScsiDiskDevice->BlkIoMedia;
  224. ScsiDiskDevice->BlkIo2.Reset = ScsiDiskResetEx;
  225. ScsiDiskDevice->BlkIo2.ReadBlocksEx = ScsiDiskReadBlocksEx;
  226. ScsiDiskDevice->BlkIo2.WriteBlocksEx = ScsiDiskWriteBlocksEx;
  227. ScsiDiskDevice->BlkIo2.FlushBlocksEx = ScsiDiskFlushBlocksEx;
  228. ScsiDiskDevice->StorageSecurity.ReceiveData = ScsiDiskReceiveData;
  229. ScsiDiskDevice->StorageSecurity.SendData = ScsiDiskSendData;
  230. ScsiDiskDevice->EraseBlock.Revision = EFI_ERASE_BLOCK_PROTOCOL_REVISION;
  231. ScsiDiskDevice->EraseBlock.EraseLengthGranularity = 1;
  232. ScsiDiskDevice->EraseBlock.EraseBlocks = ScsiDiskEraseBlocks;
  233. ScsiDiskDevice->UnmapInfo.MaxBlkDespCnt = 1;
  234. ScsiDiskDevice->BlockLimitsVpdSupported = FALSE;
  235. ScsiDiskDevice->Handle = Controller;
  236. InitializeListHead (&ScsiDiskDevice->AsyncTaskQueue);
  237. ScsiIo->GetDeviceType (ScsiIo, &(ScsiDiskDevice->DeviceType));
  238. switch (ScsiDiskDevice->DeviceType) {
  239. case EFI_SCSI_TYPE_DISK:
  240. ScsiDiskDevice->BlkIo.Media->BlockSize = 0x200;
  241. MustReadCapacity = TRUE;
  242. break;
  243. case EFI_SCSI_TYPE_CDROM:
  244. ScsiDiskDevice->BlkIo.Media->BlockSize = 0x800;
  245. ScsiDiskDevice->BlkIo.Media->ReadOnly = TRUE;
  246. MustReadCapacity = FALSE;
  247. break;
  248. case EFI_SCSI_TYPE_WLUN:
  249. MustReadCapacity = FALSE;
  250. break;
  251. }
  252. //
  253. // The Sense Data Array's initial size is 6
  254. //
  255. ScsiDiskDevice->SenseDataNumber = 6;
  256. ScsiDiskDevice->SenseData = (EFI_SCSI_SENSE_DATA *)AllocateZeroPool (
  257. sizeof (EFI_SCSI_SENSE_DATA) * ScsiDiskDevice->SenseDataNumber
  258. );
  259. if (ScsiDiskDevice->SenseData == NULL) {
  260. gBS->CloseProtocol (
  261. Controller,
  262. &gEfiScsiIoProtocolGuid,
  263. This->DriverBindingHandle,
  264. Controller
  265. );
  266. FreePool (ScsiDiskDevice);
  267. return EFI_OUT_OF_RESOURCES;
  268. }
  269. //
  270. // Retrieve device information
  271. //
  272. MaxRetry = 2;
  273. for (Index = 0; Index < MaxRetry; Index++) {
  274. Status = ScsiDiskInquiryDevice (ScsiDiskDevice, &NeedRetry);
  275. if (!EFI_ERROR (Status)) {
  276. break;
  277. }
  278. if (!NeedRetry) {
  279. FreePool (ScsiDiskDevice->SenseData);
  280. gBS->CloseProtocol (
  281. Controller,
  282. &gEfiScsiIoProtocolGuid,
  283. This->DriverBindingHandle,
  284. Controller
  285. );
  286. FreePool (ScsiDiskDevice);
  287. return EFI_DEVICE_ERROR;
  288. }
  289. }
  290. //
  291. // The second parameter "TRUE" means must
  292. // retrieve media capacity
  293. //
  294. Status = ScsiDiskDetectMedia (ScsiDiskDevice, MustReadCapacity, &Temp);
  295. if (!EFI_ERROR (Status)) {
  296. //
  297. // Determine if Block IO & Block IO2 should be produced on this controller
  298. // handle
  299. //
  300. if (DetermineInstallBlockIo (Controller)) {
  301. InitializeInstallDiskInfo (ScsiDiskDevice, Controller);
  302. Status = gBS->InstallMultipleProtocolInterfaces (
  303. &Controller,
  304. &gEfiBlockIoProtocolGuid,
  305. &ScsiDiskDevice->BlkIo,
  306. &gEfiBlockIo2ProtocolGuid,
  307. &ScsiDiskDevice->BlkIo2,
  308. &gEfiDiskInfoProtocolGuid,
  309. &ScsiDiskDevice->DiskInfo,
  310. NULL
  311. );
  312. if (!EFI_ERROR (Status)) {
  313. if (DetermineInstallEraseBlock (ScsiDiskDevice, Controller)) {
  314. Status = gBS->InstallProtocolInterface (
  315. &Controller,
  316. &gEfiEraseBlockProtocolGuid,
  317. EFI_NATIVE_INTERFACE,
  318. &ScsiDiskDevice->EraseBlock
  319. );
  320. if (EFI_ERROR (Status)) {
  321. DEBUG ((DEBUG_ERROR, "ScsiDisk: Failed to install the Erase Block Protocol! Status = %r\n", Status));
  322. }
  323. }
  324. if (DetermineInstallStorageSecurity (ScsiDiskDevice, Controller)) {
  325. Status = gBS->InstallProtocolInterface (
  326. &Controller,
  327. &gEfiStorageSecurityCommandProtocolGuid,
  328. EFI_NATIVE_INTERFACE,
  329. &ScsiDiskDevice->StorageSecurity
  330. );
  331. if (EFI_ERROR (Status)) {
  332. DEBUG ((DEBUG_ERROR, "ScsiDisk: Failed to install the Storage Security Command Protocol! Status = %r\n", Status));
  333. }
  334. }
  335. CopyMem (
  336. VendorStr,
  337. &ScsiDiskDevice->InquiryData.Reserved_5_95[VENDOR_IDENTIFICATION_OFFSET],
  338. VENDOR_IDENTIFICATION_LENGTH
  339. );
  340. VendorStr[VENDOR_IDENTIFICATION_LENGTH] = 0;
  341. RemoveTrailingSpaces (VendorStr);
  342. CopyMem (
  343. ProductStr,
  344. &ScsiDiskDevice->InquiryData.Reserved_5_95[PRODUCT_IDENTIFICATION_OFFSET],
  345. PRODUCT_IDENTIFICATION_LENGTH
  346. );
  347. ProductStr[PRODUCT_IDENTIFICATION_LENGTH] = 0;
  348. RemoveTrailingSpaces (ProductStr);
  349. UnicodeSPrint (DeviceStr, sizeof (DeviceStr), L"%a %a", VendorStr, ProductStr);
  350. ScsiDiskDevice->ControllerNameTable = NULL;
  351. AddUnicodeString2 (
  352. "eng",
  353. gScsiDiskComponentName.SupportedLanguages,
  354. &ScsiDiskDevice->ControllerNameTable,
  355. DeviceStr,
  356. TRUE
  357. );
  358. AddUnicodeString2 (
  359. "en",
  360. gScsiDiskComponentName2.SupportedLanguages,
  361. &ScsiDiskDevice->ControllerNameTable,
  362. DeviceStr,
  363. FALSE
  364. );
  365. return EFI_SUCCESS;
  366. }
  367. }
  368. }
  369. gBS->FreePool (ScsiDiskDevice->SenseData);
  370. gBS->FreePool (ScsiDiskDevice);
  371. gBS->CloseProtocol (
  372. Controller,
  373. &gEfiScsiIoProtocolGuid,
  374. This->DriverBindingHandle,
  375. Controller
  376. );
  377. return Status;
  378. }
  379. /**
  380. Stop this driver on ControllerHandle.
  381. This service is called by the EFI boot service DisconnectController().
  382. In order to make drivers as small as possible, there are a few calling
  383. restrictions for this service. DisconnectController() must follow these
  384. calling restrictions. If any other agent wishes to call Stop() it must
  385. also follow these calling restrictions.
  386. @param This Protocol instance pointer.
  387. @param ControllerHandle Handle of device to stop driver on
  388. @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
  389. children is zero stop the entire bus driver.
  390. @param ChildHandleBuffer List of Child Handles to Stop.
  391. @retval EFI_SUCCESS This driver is removed ControllerHandle
  392. @retval other This driver was not removed from this device
  393. **/
  394. EFI_STATUS
  395. EFIAPI
  396. ScsiDiskDriverBindingStop (
  397. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  398. IN EFI_HANDLE Controller,
  399. IN UINTN NumberOfChildren,
  400. IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
  401. )
  402. {
  403. EFI_BLOCK_IO_PROTOCOL *BlkIo;
  404. EFI_ERASE_BLOCK_PROTOCOL *EraseBlock;
  405. SCSI_DISK_DEV *ScsiDiskDevice;
  406. EFI_STATUS Status;
  407. Status = gBS->OpenProtocol (
  408. Controller,
  409. &gEfiBlockIoProtocolGuid,
  410. (VOID **)&BlkIo,
  411. This->DriverBindingHandle,
  412. Controller,
  413. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  414. );
  415. if (EFI_ERROR (Status)) {
  416. return Status;
  417. }
  418. ScsiDiskDevice = SCSI_DISK_DEV_FROM_BLKIO (BlkIo);
  419. //
  420. // Wait for the BlockIo2 requests queue to become empty
  421. //
  422. while (!IsListEmpty (&ScsiDiskDevice->AsyncTaskQueue)) {
  423. }
  424. //
  425. // If Erase Block Protocol is installed, then uninstall this protocol.
  426. //
  427. Status = gBS->OpenProtocol (
  428. Controller,
  429. &gEfiEraseBlockProtocolGuid,
  430. (VOID **)&EraseBlock,
  431. This->DriverBindingHandle,
  432. Controller,
  433. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  434. );
  435. if (!EFI_ERROR (Status)) {
  436. Status = gBS->UninstallProtocolInterface (
  437. Controller,
  438. &gEfiEraseBlockProtocolGuid,
  439. &ScsiDiskDevice->EraseBlock
  440. );
  441. if (EFI_ERROR (Status)) {
  442. return Status;
  443. }
  444. }
  445. Status = gBS->UninstallMultipleProtocolInterfaces (
  446. Controller,
  447. &gEfiBlockIoProtocolGuid,
  448. &ScsiDiskDevice->BlkIo,
  449. &gEfiBlockIo2ProtocolGuid,
  450. &ScsiDiskDevice->BlkIo2,
  451. &gEfiDiskInfoProtocolGuid,
  452. &ScsiDiskDevice->DiskInfo,
  453. NULL
  454. );
  455. if (!EFI_ERROR (Status)) {
  456. gBS->CloseProtocol (
  457. Controller,
  458. &gEfiScsiIoProtocolGuid,
  459. This->DriverBindingHandle,
  460. Controller
  461. );
  462. ReleaseScsiDiskDeviceResources (ScsiDiskDevice);
  463. return EFI_SUCCESS;
  464. }
  465. //
  466. // errors met
  467. //
  468. return Status;
  469. }
  470. /**
  471. Reset SCSI Disk.
  472. @param This The pointer of EFI_BLOCK_IO_PROTOCOL
  473. @param ExtendedVerification The flag about if extend verificate
  474. @retval EFI_SUCCESS The device was reset.
  475. @retval EFI_DEVICE_ERROR The device is not functioning properly and could
  476. not be reset.
  477. @return EFI_STATUS is returned from EFI_SCSI_IO_PROTOCOL.ResetDevice().
  478. **/
  479. EFI_STATUS
  480. EFIAPI
  481. ScsiDiskReset (
  482. IN EFI_BLOCK_IO_PROTOCOL *This,
  483. IN BOOLEAN ExtendedVerification
  484. )
  485. {
  486. EFI_TPL OldTpl;
  487. SCSI_DISK_DEV *ScsiDiskDevice;
  488. EFI_STATUS Status;
  489. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  490. ScsiDiskDevice = SCSI_DISK_DEV_FROM_BLKIO (This);
  491. Status = ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo);
  492. if (EFI_ERROR (Status)) {
  493. if (Status == EFI_UNSUPPORTED) {
  494. Status = EFI_SUCCESS;
  495. } else {
  496. Status = EFI_DEVICE_ERROR;
  497. goto Done;
  498. }
  499. }
  500. if (!ExtendedVerification) {
  501. goto Done;
  502. }
  503. Status = ScsiDiskDevice->ScsiIo->ResetBus (ScsiDiskDevice->ScsiIo);
  504. if (EFI_ERROR (Status)) {
  505. Status = EFI_DEVICE_ERROR;
  506. goto Done;
  507. }
  508. Done:
  509. gBS->RestoreTPL (OldTpl);
  510. return Status;
  511. }
  512. /**
  513. The function is to Read Block from SCSI Disk.
  514. @param This The pointer of EFI_BLOCK_IO_PROTOCOL.
  515. @param MediaId The Id of Media detected
  516. @param Lba The logic block address
  517. @param BufferSize The size of Buffer
  518. @param Buffer The buffer to fill the read out data
  519. @retval EFI_SUCCESS Successfully to read out block.
  520. @retval EFI_DEVICE_ERROR Fail to detect media.
  521. @retval EFI_NO_MEDIA Media is not present.
  522. @retval EFI_MEDIA_CHANGED Media has changed.
  523. @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
  524. @retval EFI_INVALID_PARAMETER Invalid parameter passed in.
  525. **/
  526. EFI_STATUS
  527. EFIAPI
  528. ScsiDiskReadBlocks (
  529. IN EFI_BLOCK_IO_PROTOCOL *This,
  530. IN UINT32 MediaId,
  531. IN EFI_LBA Lba,
  532. IN UINTN BufferSize,
  533. OUT VOID *Buffer
  534. )
  535. {
  536. SCSI_DISK_DEV *ScsiDiskDevice;
  537. EFI_BLOCK_IO_MEDIA *Media;
  538. EFI_STATUS Status;
  539. UINTN BlockSize;
  540. UINTN NumberOfBlocks;
  541. BOOLEAN MediaChange;
  542. EFI_TPL OldTpl;
  543. MediaChange = FALSE;
  544. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  545. ScsiDiskDevice = SCSI_DISK_DEV_FROM_BLKIO (This);
  546. Media = ScsiDiskDevice->BlkIo.Media;
  547. if (!IS_DEVICE_FIXED (ScsiDiskDevice)) {
  548. Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange);
  549. if (EFI_ERROR (Status)) {
  550. Status = EFI_DEVICE_ERROR;
  551. goto Done;
  552. }
  553. if (MediaChange) {
  554. gBS->ReinstallProtocolInterface (
  555. ScsiDiskDevice->Handle,
  556. &gEfiBlockIoProtocolGuid,
  557. &ScsiDiskDevice->BlkIo,
  558. &ScsiDiskDevice->BlkIo
  559. );
  560. gBS->ReinstallProtocolInterface (
  561. ScsiDiskDevice->Handle,
  562. &gEfiBlockIo2ProtocolGuid,
  563. &ScsiDiskDevice->BlkIo2,
  564. &ScsiDiskDevice->BlkIo2
  565. );
  566. if (DetermineInstallEraseBlock (ScsiDiskDevice, ScsiDiskDevice->Handle)) {
  567. gBS->ReinstallProtocolInterface (
  568. ScsiDiskDevice->Handle,
  569. &gEfiEraseBlockProtocolGuid,
  570. &ScsiDiskDevice->EraseBlock,
  571. &ScsiDiskDevice->EraseBlock
  572. );
  573. }
  574. if (DetermineInstallStorageSecurity (ScsiDiskDevice, ScsiDiskDevice->Handle)) {
  575. gBS->ReinstallProtocolInterface (
  576. ScsiDiskDevice->Handle,
  577. &gEfiStorageSecurityCommandProtocolGuid,
  578. &ScsiDiskDevice->StorageSecurity,
  579. &ScsiDiskDevice->StorageSecurity
  580. );
  581. }
  582. if (Media->MediaPresent) {
  583. Status = EFI_MEDIA_CHANGED;
  584. } else {
  585. Status = EFI_NO_MEDIA;
  586. }
  587. goto Done;
  588. }
  589. }
  590. //
  591. // Get the intrinsic block size
  592. //
  593. BlockSize = Media->BlockSize;
  594. if (BlockSize == 0) {
  595. Status = EFI_DEVICE_ERROR;
  596. goto Done;
  597. }
  598. NumberOfBlocks = BufferSize / BlockSize;
  599. if (!(Media->MediaPresent)) {
  600. Status = EFI_NO_MEDIA;
  601. goto Done;
  602. }
  603. if (MediaId != Media->MediaId) {
  604. Status = EFI_MEDIA_CHANGED;
  605. goto Done;
  606. }
  607. if (Buffer == NULL) {
  608. Status = EFI_INVALID_PARAMETER;
  609. goto Done;
  610. }
  611. if (BufferSize == 0) {
  612. Status = EFI_SUCCESS;
  613. goto Done;
  614. }
  615. if (BufferSize % BlockSize != 0) {
  616. Status = EFI_BAD_BUFFER_SIZE;
  617. goto Done;
  618. }
  619. if (Lba > Media->LastBlock) {
  620. Status = EFI_INVALID_PARAMETER;
  621. goto Done;
  622. }
  623. if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {
  624. Status = EFI_INVALID_PARAMETER;
  625. goto Done;
  626. }
  627. if ((Media->IoAlign > 1) && (((UINTN)Buffer & (Media->IoAlign - 1)) != 0)) {
  628. Status = EFI_INVALID_PARAMETER;
  629. goto Done;
  630. }
  631. //
  632. // If all the parameters are valid, then perform read sectors command
  633. // to transfer data from device to host.
  634. //
  635. Status = ScsiDiskReadSectors (ScsiDiskDevice, Buffer, Lba, NumberOfBlocks);
  636. Done:
  637. gBS->RestoreTPL (OldTpl);
  638. return Status;
  639. }
  640. /**
  641. The function is to Write Block to SCSI Disk.
  642. @param This The pointer of EFI_BLOCK_IO_PROTOCOL
  643. @param MediaId The Id of Media detected
  644. @param Lba The logic block address
  645. @param BufferSize The size of Buffer
  646. @param Buffer The buffer to fill the read out data
  647. @retval EFI_SUCCESS Successfully to read out block.
  648. @retval EFI_WRITE_PROTECTED The device can not be written to.
  649. @retval EFI_DEVICE_ERROR Fail to detect media.
  650. @retval EFI_NO_MEDIA Media is not present.
  651. @retval EFI_MEDIA_CHANGED Media has changed.
  652. @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
  653. @retval EFI_INVALID_PARAMETER Invalid parameter passed in.
  654. **/
  655. EFI_STATUS
  656. EFIAPI
  657. ScsiDiskWriteBlocks (
  658. IN EFI_BLOCK_IO_PROTOCOL *This,
  659. IN UINT32 MediaId,
  660. IN EFI_LBA Lba,
  661. IN UINTN BufferSize,
  662. IN VOID *Buffer
  663. )
  664. {
  665. SCSI_DISK_DEV *ScsiDiskDevice;
  666. EFI_BLOCK_IO_MEDIA *Media;
  667. EFI_STATUS Status;
  668. UINTN BlockSize;
  669. UINTN NumberOfBlocks;
  670. BOOLEAN MediaChange;
  671. EFI_TPL OldTpl;
  672. MediaChange = FALSE;
  673. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  674. ScsiDiskDevice = SCSI_DISK_DEV_FROM_BLKIO (This);
  675. Media = ScsiDiskDevice->BlkIo.Media;
  676. if (!IS_DEVICE_FIXED (ScsiDiskDevice)) {
  677. Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange);
  678. if (EFI_ERROR (Status)) {
  679. Status = EFI_DEVICE_ERROR;
  680. goto Done;
  681. }
  682. if (MediaChange) {
  683. gBS->ReinstallProtocolInterface (
  684. ScsiDiskDevice->Handle,
  685. &gEfiBlockIoProtocolGuid,
  686. &ScsiDiskDevice->BlkIo,
  687. &ScsiDiskDevice->BlkIo
  688. );
  689. gBS->ReinstallProtocolInterface (
  690. ScsiDiskDevice->Handle,
  691. &gEfiBlockIo2ProtocolGuid,
  692. &ScsiDiskDevice->BlkIo2,
  693. &ScsiDiskDevice->BlkIo2
  694. );
  695. if (DetermineInstallEraseBlock (ScsiDiskDevice, ScsiDiskDevice->Handle)) {
  696. gBS->ReinstallProtocolInterface (
  697. ScsiDiskDevice->Handle,
  698. &gEfiEraseBlockProtocolGuid,
  699. &ScsiDiskDevice->EraseBlock,
  700. &ScsiDiskDevice->EraseBlock
  701. );
  702. }
  703. if (DetermineInstallStorageSecurity (ScsiDiskDevice, ScsiDiskDevice->Handle)) {
  704. gBS->ReinstallProtocolInterface (
  705. ScsiDiskDevice->Handle,
  706. &gEfiStorageSecurityCommandProtocolGuid,
  707. &ScsiDiskDevice->StorageSecurity,
  708. &ScsiDiskDevice->StorageSecurity
  709. );
  710. }
  711. if (Media->MediaPresent) {
  712. Status = EFI_MEDIA_CHANGED;
  713. } else {
  714. Status = EFI_NO_MEDIA;
  715. }
  716. goto Done;
  717. }
  718. }
  719. //
  720. // Get the intrinsic block size
  721. //
  722. BlockSize = Media->BlockSize;
  723. if (BlockSize == 0) {
  724. Status = EFI_DEVICE_ERROR;
  725. goto Done;
  726. }
  727. NumberOfBlocks = BufferSize / BlockSize;
  728. if (!(Media->MediaPresent)) {
  729. Status = EFI_NO_MEDIA;
  730. goto Done;
  731. }
  732. if (MediaId != Media->MediaId) {
  733. Status = EFI_MEDIA_CHANGED;
  734. goto Done;
  735. }
  736. if (Media->ReadOnly) {
  737. Status = EFI_WRITE_PROTECTED;
  738. goto Done;
  739. }
  740. if (BufferSize == 0) {
  741. Status = EFI_SUCCESS;
  742. goto Done;
  743. }
  744. if (Buffer == NULL) {
  745. Status = EFI_INVALID_PARAMETER;
  746. goto Done;
  747. }
  748. if (BufferSize % BlockSize != 0) {
  749. Status = EFI_BAD_BUFFER_SIZE;
  750. goto Done;
  751. }
  752. if (Lba > Media->LastBlock) {
  753. Status = EFI_INVALID_PARAMETER;
  754. goto Done;
  755. }
  756. if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {
  757. Status = EFI_INVALID_PARAMETER;
  758. goto Done;
  759. }
  760. if ((Media->IoAlign > 1) && (((UINTN)Buffer & (Media->IoAlign - 1)) != 0)) {
  761. Status = EFI_INVALID_PARAMETER;
  762. goto Done;
  763. }
  764. //
  765. // if all the parameters are valid, then perform read sectors command
  766. // to transfer data from device to host.
  767. //
  768. Status = ScsiDiskWriteSectors (ScsiDiskDevice, Buffer, Lba, NumberOfBlocks);
  769. Done:
  770. gBS->RestoreTPL (OldTpl);
  771. return Status;
  772. }
  773. /**
  774. Flush Block to Disk.
  775. EFI_SUCCESS is returned directly.
  776. @param This The pointer of EFI_BLOCK_IO_PROTOCOL
  777. @retval EFI_SUCCESS All outstanding data was written to the device
  778. **/
  779. EFI_STATUS
  780. EFIAPI
  781. ScsiDiskFlushBlocks (
  782. IN EFI_BLOCK_IO_PROTOCOL *This
  783. )
  784. {
  785. //
  786. // return directly
  787. //
  788. return EFI_SUCCESS;
  789. }
  790. /**
  791. Reset SCSI Disk.
  792. @param This The pointer of EFI_BLOCK_IO2_PROTOCOL.
  793. @param ExtendedVerification The flag about if extend verificate.
  794. @retval EFI_SUCCESS The device was reset.
  795. @retval EFI_DEVICE_ERROR The device is not functioning properly and could
  796. not be reset.
  797. @return EFI_STATUS is returned from EFI_SCSI_IO_PROTOCOL.ResetDevice().
  798. **/
  799. EFI_STATUS
  800. EFIAPI
  801. ScsiDiskResetEx (
  802. IN EFI_BLOCK_IO2_PROTOCOL *This,
  803. IN BOOLEAN ExtendedVerification
  804. )
  805. {
  806. EFI_TPL OldTpl;
  807. SCSI_DISK_DEV *ScsiDiskDevice;
  808. EFI_STATUS Status;
  809. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  810. ScsiDiskDevice = SCSI_DISK_DEV_FROM_BLKIO2 (This);
  811. Status = ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo);
  812. if (EFI_ERROR (Status)) {
  813. if (Status == EFI_UNSUPPORTED) {
  814. Status = EFI_SUCCESS;
  815. } else {
  816. Status = EFI_DEVICE_ERROR;
  817. goto Done;
  818. }
  819. }
  820. if (!ExtendedVerification) {
  821. goto Done;
  822. }
  823. Status = ScsiDiskDevice->ScsiIo->ResetBus (ScsiDiskDevice->ScsiIo);
  824. if (EFI_ERROR (Status)) {
  825. Status = EFI_DEVICE_ERROR;
  826. goto Done;
  827. }
  828. Done:
  829. gBS->RestoreTPL (OldTpl);
  830. return Status;
  831. }
  832. /**
  833. The function is to Read Block from SCSI Disk.
  834. @param This The pointer of EFI_BLOCK_IO_PROTOCOL.
  835. @param MediaId The Id of Media detected.
  836. @param Lba The logic block address.
  837. @param Token A pointer to the token associated with the transaction.
  838. @param BufferSize The size of Buffer.
  839. @param Buffer The buffer to fill the read out data.
  840. @retval EFI_SUCCESS The read request was queued if Token-> Event is
  841. not NULL. The data was read correctly from the
  842. device if theToken-> Event is NULL.
  843. @retval EFI_DEVICE_ERROR The device reported an error while attempting
  844. to perform the read operation.
  845. @retval EFI_NO_MEDIA There is no media in the device.
  846. @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
  847. @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of
  848. the intrinsic block size of the device.
  849. @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not
  850. valid, or the buffer is not on proper
  851. alignment.
  852. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
  853. lack of resources.
  854. **/
  855. EFI_STATUS
  856. EFIAPI
  857. ScsiDiskReadBlocksEx (
  858. IN EFI_BLOCK_IO2_PROTOCOL *This,
  859. IN UINT32 MediaId,
  860. IN EFI_LBA Lba,
  861. IN OUT EFI_BLOCK_IO2_TOKEN *Token,
  862. IN UINTN BufferSize,
  863. OUT VOID *Buffer
  864. )
  865. {
  866. SCSI_DISK_DEV *ScsiDiskDevice;
  867. EFI_BLOCK_IO_MEDIA *Media;
  868. EFI_STATUS Status;
  869. UINTN BlockSize;
  870. UINTN NumberOfBlocks;
  871. BOOLEAN MediaChange;
  872. EFI_TPL OldTpl;
  873. MediaChange = FALSE;
  874. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  875. ScsiDiskDevice = SCSI_DISK_DEV_FROM_BLKIO2 (This);
  876. Media = ScsiDiskDevice->BlkIo.Media;
  877. if (!IS_DEVICE_FIXED (ScsiDiskDevice)) {
  878. Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange);
  879. if (EFI_ERROR (Status)) {
  880. Status = EFI_DEVICE_ERROR;
  881. goto Done;
  882. }
  883. if (MediaChange) {
  884. gBS->ReinstallProtocolInterface (
  885. ScsiDiskDevice->Handle,
  886. &gEfiBlockIoProtocolGuid,
  887. &ScsiDiskDevice->BlkIo,
  888. &ScsiDiskDevice->BlkIo
  889. );
  890. gBS->ReinstallProtocolInterface (
  891. ScsiDiskDevice->Handle,
  892. &gEfiBlockIo2ProtocolGuid,
  893. &ScsiDiskDevice->BlkIo2,
  894. &ScsiDiskDevice->BlkIo2
  895. );
  896. if (DetermineInstallEraseBlock (ScsiDiskDevice, ScsiDiskDevice->Handle)) {
  897. gBS->ReinstallProtocolInterface (
  898. ScsiDiskDevice->Handle,
  899. &gEfiEraseBlockProtocolGuid,
  900. &ScsiDiskDevice->EraseBlock,
  901. &ScsiDiskDevice->EraseBlock
  902. );
  903. }
  904. if (DetermineInstallStorageSecurity (ScsiDiskDevice, ScsiDiskDevice->Handle)) {
  905. gBS->ReinstallProtocolInterface (
  906. ScsiDiskDevice->Handle,
  907. &gEfiStorageSecurityCommandProtocolGuid,
  908. &ScsiDiskDevice->StorageSecurity,
  909. &ScsiDiskDevice->StorageSecurity
  910. );
  911. }
  912. if (Media->MediaPresent) {
  913. Status = EFI_MEDIA_CHANGED;
  914. } else {
  915. Status = EFI_NO_MEDIA;
  916. }
  917. goto Done;
  918. }
  919. }
  920. //
  921. // Get the intrinsic block size
  922. //
  923. BlockSize = Media->BlockSize;
  924. if (BlockSize == 0) {
  925. Status = EFI_DEVICE_ERROR;
  926. goto Done;
  927. }
  928. NumberOfBlocks = BufferSize / BlockSize;
  929. if (!(Media->MediaPresent)) {
  930. Status = EFI_NO_MEDIA;
  931. goto Done;
  932. }
  933. if (MediaId != Media->MediaId) {
  934. Status = EFI_MEDIA_CHANGED;
  935. goto Done;
  936. }
  937. if (Buffer == NULL) {
  938. Status = EFI_INVALID_PARAMETER;
  939. goto Done;
  940. }
  941. if (BufferSize == 0) {
  942. if ((Token != NULL) && (Token->Event != NULL)) {
  943. Token->TransactionStatus = EFI_SUCCESS;
  944. gBS->SignalEvent (Token->Event);
  945. }
  946. Status = EFI_SUCCESS;
  947. goto Done;
  948. }
  949. if (BufferSize % BlockSize != 0) {
  950. Status = EFI_BAD_BUFFER_SIZE;
  951. goto Done;
  952. }
  953. if (Lba > Media->LastBlock) {
  954. Status = EFI_INVALID_PARAMETER;
  955. goto Done;
  956. }
  957. if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {
  958. Status = EFI_INVALID_PARAMETER;
  959. goto Done;
  960. }
  961. if ((Media->IoAlign > 1) && (((UINTN)Buffer & (Media->IoAlign - 1)) != 0)) {
  962. Status = EFI_INVALID_PARAMETER;
  963. goto Done;
  964. }
  965. //
  966. // If all the parameters are valid, then perform read sectors command
  967. // to transfer data from device to host.
  968. //
  969. if ((Token != NULL) && (Token->Event != NULL)) {
  970. Token->TransactionStatus = EFI_SUCCESS;
  971. Status = ScsiDiskAsyncReadSectors (
  972. ScsiDiskDevice,
  973. Buffer,
  974. Lba,
  975. NumberOfBlocks,
  976. Token
  977. );
  978. } else {
  979. Status = ScsiDiskReadSectors (
  980. ScsiDiskDevice,
  981. Buffer,
  982. Lba,
  983. NumberOfBlocks
  984. );
  985. }
  986. Done:
  987. gBS->RestoreTPL (OldTpl);
  988. return Status;
  989. }
  990. /**
  991. The function is to Write Block to SCSI Disk.
  992. @param This The pointer of EFI_BLOCK_IO_PROTOCOL.
  993. @param MediaId The Id of Media detected.
  994. @param Lba The logic block address.
  995. @param Token A pointer to the token associated with the transaction.
  996. @param BufferSize The size of Buffer.
  997. @param Buffer The buffer to fill the read out data.
  998. @retval EFI_SUCCESS The data were written correctly to the device.
  999. @retval EFI_WRITE_PROTECTED The device cannot be written to.
  1000. @retval EFI_NO_MEDIA There is no media in the device.
  1001. @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
  1002. @retval EFI_DEVICE_ERROR The device reported an error while attempting
  1003. to perform the write operation.
  1004. @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of
  1005. the intrinsic block size of the device.
  1006. @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not
  1007. valid, or the buffer is not on proper
  1008. alignment.
  1009. **/
  1010. EFI_STATUS
  1011. EFIAPI
  1012. ScsiDiskWriteBlocksEx (
  1013. IN EFI_BLOCK_IO2_PROTOCOL *This,
  1014. IN UINT32 MediaId,
  1015. IN EFI_LBA Lba,
  1016. IN OUT EFI_BLOCK_IO2_TOKEN *Token,
  1017. IN UINTN BufferSize,
  1018. IN VOID *Buffer
  1019. )
  1020. {
  1021. SCSI_DISK_DEV *ScsiDiskDevice;
  1022. EFI_BLOCK_IO_MEDIA *Media;
  1023. EFI_STATUS Status;
  1024. UINTN BlockSize;
  1025. UINTN NumberOfBlocks;
  1026. BOOLEAN MediaChange;
  1027. EFI_TPL OldTpl;
  1028. MediaChange = FALSE;
  1029. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  1030. ScsiDiskDevice = SCSI_DISK_DEV_FROM_BLKIO2 (This);
  1031. Media = ScsiDiskDevice->BlkIo.Media;
  1032. if (!IS_DEVICE_FIXED (ScsiDiskDevice)) {
  1033. Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange);
  1034. if (EFI_ERROR (Status)) {
  1035. Status = EFI_DEVICE_ERROR;
  1036. goto Done;
  1037. }
  1038. if (MediaChange) {
  1039. gBS->ReinstallProtocolInterface (
  1040. ScsiDiskDevice->Handle,
  1041. &gEfiBlockIoProtocolGuid,
  1042. &ScsiDiskDevice->BlkIo,
  1043. &ScsiDiskDevice->BlkIo
  1044. );
  1045. gBS->ReinstallProtocolInterface (
  1046. ScsiDiskDevice->Handle,
  1047. &gEfiBlockIo2ProtocolGuid,
  1048. &ScsiDiskDevice->BlkIo2,
  1049. &ScsiDiskDevice->BlkIo2
  1050. );
  1051. if (DetermineInstallEraseBlock (ScsiDiskDevice, ScsiDiskDevice->Handle)) {
  1052. gBS->ReinstallProtocolInterface (
  1053. ScsiDiskDevice->Handle,
  1054. &gEfiEraseBlockProtocolGuid,
  1055. &ScsiDiskDevice->EraseBlock,
  1056. &ScsiDiskDevice->EraseBlock
  1057. );
  1058. }
  1059. if (DetermineInstallStorageSecurity (ScsiDiskDevice, ScsiDiskDevice->Handle)) {
  1060. gBS->ReinstallProtocolInterface (
  1061. ScsiDiskDevice->Handle,
  1062. &gEfiStorageSecurityCommandProtocolGuid,
  1063. &ScsiDiskDevice->StorageSecurity,
  1064. &ScsiDiskDevice->StorageSecurity
  1065. );
  1066. }
  1067. if (Media->MediaPresent) {
  1068. Status = EFI_MEDIA_CHANGED;
  1069. } else {
  1070. Status = EFI_NO_MEDIA;
  1071. }
  1072. goto Done;
  1073. }
  1074. }
  1075. //
  1076. // Get the intrinsic block size
  1077. //
  1078. BlockSize = Media->BlockSize;
  1079. if (BlockSize == 0) {
  1080. Status = EFI_DEVICE_ERROR;
  1081. goto Done;
  1082. }
  1083. NumberOfBlocks = BufferSize / BlockSize;
  1084. if (!(Media->MediaPresent)) {
  1085. Status = EFI_NO_MEDIA;
  1086. goto Done;
  1087. }
  1088. if (MediaId != Media->MediaId) {
  1089. Status = EFI_MEDIA_CHANGED;
  1090. goto Done;
  1091. }
  1092. if (Media->ReadOnly) {
  1093. Status = EFI_WRITE_PROTECTED;
  1094. goto Done;
  1095. }
  1096. if (BufferSize == 0) {
  1097. if ((Token != NULL) && (Token->Event != NULL)) {
  1098. Token->TransactionStatus = EFI_SUCCESS;
  1099. gBS->SignalEvent (Token->Event);
  1100. }
  1101. Status = EFI_SUCCESS;
  1102. goto Done;
  1103. }
  1104. if (Buffer == NULL) {
  1105. Status = EFI_INVALID_PARAMETER;
  1106. goto Done;
  1107. }
  1108. if (BufferSize % BlockSize != 0) {
  1109. Status = EFI_BAD_BUFFER_SIZE;
  1110. goto Done;
  1111. }
  1112. if (Lba > Media->LastBlock) {
  1113. Status = EFI_INVALID_PARAMETER;
  1114. goto Done;
  1115. }
  1116. if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {
  1117. Status = EFI_INVALID_PARAMETER;
  1118. goto Done;
  1119. }
  1120. if ((Media->IoAlign > 1) && (((UINTN)Buffer & (Media->IoAlign - 1)) != 0)) {
  1121. Status = EFI_INVALID_PARAMETER;
  1122. goto Done;
  1123. }
  1124. //
  1125. // if all the parameters are valid, then perform write sectors command
  1126. // to transfer data from device to host.
  1127. //
  1128. if ((Token != NULL) && (Token->Event != NULL)) {
  1129. Token->TransactionStatus = EFI_SUCCESS;
  1130. Status = ScsiDiskAsyncWriteSectors (
  1131. ScsiDiskDevice,
  1132. Buffer,
  1133. Lba,
  1134. NumberOfBlocks,
  1135. Token
  1136. );
  1137. } else {
  1138. Status = ScsiDiskWriteSectors (
  1139. ScsiDiskDevice,
  1140. Buffer,
  1141. Lba,
  1142. NumberOfBlocks
  1143. );
  1144. }
  1145. Done:
  1146. gBS->RestoreTPL (OldTpl);
  1147. return Status;
  1148. }
  1149. /**
  1150. Flush the Block Device.
  1151. @param This Indicates a pointer to the calling context.
  1152. @param Token A pointer to the token associated with the transaction.
  1153. @retval EFI_SUCCESS All outstanding data was written to the device.
  1154. @retval EFI_DEVICE_ERROR The device reported an error while attempting to
  1155. write data.
  1156. @retval EFI_WRITE_PROTECTED The device cannot be written to.
  1157. @retval EFI_NO_MEDIA There is no media in the device.
  1158. @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
  1159. **/
  1160. EFI_STATUS
  1161. EFIAPI
  1162. ScsiDiskFlushBlocksEx (
  1163. IN EFI_BLOCK_IO2_PROTOCOL *This,
  1164. IN OUT EFI_BLOCK_IO2_TOKEN *Token
  1165. )
  1166. {
  1167. SCSI_DISK_DEV *ScsiDiskDevice;
  1168. EFI_BLOCK_IO_MEDIA *Media;
  1169. EFI_STATUS Status;
  1170. BOOLEAN MediaChange;
  1171. EFI_TPL OldTpl;
  1172. MediaChange = FALSE;
  1173. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  1174. ScsiDiskDevice = SCSI_DISK_DEV_FROM_BLKIO2 (This);
  1175. Media = ScsiDiskDevice->BlkIo.Media;
  1176. if (!IS_DEVICE_FIXED (ScsiDiskDevice)) {
  1177. Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange);
  1178. if (EFI_ERROR (Status)) {
  1179. Status = EFI_DEVICE_ERROR;
  1180. goto Done;
  1181. }
  1182. if (MediaChange) {
  1183. gBS->ReinstallProtocolInterface (
  1184. ScsiDiskDevice->Handle,
  1185. &gEfiBlockIoProtocolGuid,
  1186. &ScsiDiskDevice->BlkIo,
  1187. &ScsiDiskDevice->BlkIo
  1188. );
  1189. gBS->ReinstallProtocolInterface (
  1190. ScsiDiskDevice->Handle,
  1191. &gEfiBlockIo2ProtocolGuid,
  1192. &ScsiDiskDevice->BlkIo2,
  1193. &ScsiDiskDevice->BlkIo2
  1194. );
  1195. if (DetermineInstallEraseBlock (ScsiDiskDevice, ScsiDiskDevice->Handle)) {
  1196. gBS->ReinstallProtocolInterface (
  1197. ScsiDiskDevice->Handle,
  1198. &gEfiEraseBlockProtocolGuid,
  1199. &ScsiDiskDevice->EraseBlock,
  1200. &ScsiDiskDevice->EraseBlock
  1201. );
  1202. }
  1203. if (DetermineInstallStorageSecurity (ScsiDiskDevice, ScsiDiskDevice->Handle)) {
  1204. gBS->ReinstallProtocolInterface (
  1205. ScsiDiskDevice->Handle,
  1206. &gEfiStorageSecurityCommandProtocolGuid,
  1207. &ScsiDiskDevice->StorageSecurity,
  1208. &ScsiDiskDevice->StorageSecurity
  1209. );
  1210. }
  1211. if (Media->MediaPresent) {
  1212. Status = EFI_MEDIA_CHANGED;
  1213. } else {
  1214. Status = EFI_NO_MEDIA;
  1215. }
  1216. goto Done;
  1217. }
  1218. }
  1219. if (!(Media->MediaPresent)) {
  1220. Status = EFI_NO_MEDIA;
  1221. goto Done;
  1222. }
  1223. if (Media->ReadOnly) {
  1224. Status = EFI_WRITE_PROTECTED;
  1225. goto Done;
  1226. }
  1227. //
  1228. // Wait for the BlockIo2 requests queue to become empty
  1229. //
  1230. while (!IsListEmpty (&ScsiDiskDevice->AsyncTaskQueue)) {
  1231. }
  1232. Status = EFI_SUCCESS;
  1233. //
  1234. // Signal caller event
  1235. //
  1236. if ((Token != NULL) && (Token->Event != NULL)) {
  1237. Token->TransactionStatus = EFI_SUCCESS;
  1238. gBS->SignalEvent (Token->Event);
  1239. }
  1240. Done:
  1241. gBS->RestoreTPL (OldTpl);
  1242. return Status;
  1243. }
  1244. /**
  1245. Internal helper notify function which process the result of an asynchronous
  1246. SCSI UNMAP Command and signal the event passed from EraseBlocks.
  1247. @param Event The instance of EFI_EVENT.
  1248. @param Context The parameter passed in.
  1249. **/
  1250. VOID
  1251. EFIAPI
  1252. ScsiDiskAsyncUnmapNotify (
  1253. IN EFI_EVENT Event,
  1254. IN VOID *Context
  1255. )
  1256. {
  1257. SCSI_ERASEBLK_REQUEST *EraseBlkReq;
  1258. EFI_SCSI_IO_SCSI_REQUEST_PACKET *CommandPacket;
  1259. EFI_ERASE_BLOCK_TOKEN *Token;
  1260. EFI_STATUS Status;
  1261. gBS->CloseEvent (Event);
  1262. EraseBlkReq = (SCSI_ERASEBLK_REQUEST *)Context;
  1263. CommandPacket = &EraseBlkReq->CommandPacket;
  1264. Token = EraseBlkReq->Token;
  1265. Token->TransactionStatus = EFI_SUCCESS;
  1266. Status = CheckHostAdapterStatus (CommandPacket->HostAdapterStatus);
  1267. if (EFI_ERROR (Status)) {
  1268. DEBUG ((
  1269. DEBUG_ERROR,
  1270. "ScsiDiskAsyncUnmapNotify: Host adapter indicating error status 0x%x.\n",
  1271. CommandPacket->HostAdapterStatus
  1272. ));
  1273. Token->TransactionStatus = Status;
  1274. goto Done;
  1275. }
  1276. Status = CheckTargetStatus (CommandPacket->TargetStatus);
  1277. if (EFI_ERROR (Status)) {
  1278. DEBUG ((
  1279. DEBUG_ERROR,
  1280. "ScsiDiskAsyncUnmapNotify: Target indicating error status 0x%x.\n",
  1281. CommandPacket->HostAdapterStatus
  1282. ));
  1283. Token->TransactionStatus = Status;
  1284. goto Done;
  1285. }
  1286. Done:
  1287. RemoveEntryList (&EraseBlkReq->Link);
  1288. FreePool (CommandPacket->OutDataBuffer);
  1289. FreePool (EraseBlkReq->CommandPacket.Cdb);
  1290. FreePool (EraseBlkReq);
  1291. gBS->SignalEvent (Token->Event);
  1292. }
  1293. /**
  1294. Require the device server to cause one or more LBAs to be unmapped.
  1295. @param ScsiDiskDevice The pointer of ScsiDiskDevice.
  1296. @param Lba The start block number.
  1297. @param Blocks Total block number to be unmapped.
  1298. @param Token The pointer to the token associated with the
  1299. non-blocking erase block request.
  1300. @retval EFI_SUCCESS Target blocks have been successfully unmapped.
  1301. @retval EFI_DEVICE_ERROR Fail to unmap the target blocks.
  1302. **/
  1303. EFI_STATUS
  1304. ScsiDiskUnmap (
  1305. IN SCSI_DISK_DEV *ScsiDiskDevice,
  1306. IN UINT64 Lba,
  1307. IN UINTN Blocks,
  1308. IN EFI_ERASE_BLOCK_TOKEN *Token OPTIONAL
  1309. )
  1310. {
  1311. EFI_SCSI_IO_PROTOCOL *ScsiIo;
  1312. SCSI_ERASEBLK_REQUEST *EraseBlkReq;
  1313. EFI_SCSI_IO_SCSI_REQUEST_PACKET *CommandPacket;
  1314. EFI_SCSI_DISK_UNMAP_BLOCK_DESP *BlkDespPtr;
  1315. EFI_STATUS Status;
  1316. EFI_STATUS ReturnStatus;
  1317. UINT8 *Cdb;
  1318. UINT32 MaxLbaCnt;
  1319. UINT32 MaxBlkDespCnt;
  1320. UINT32 BlkDespCnt;
  1321. UINT16 UnmapParamListLen;
  1322. VOID *UnmapParamList;
  1323. EFI_EVENT AsyncUnmapEvent;
  1324. EFI_TPL OldTpl;
  1325. ScsiIo = ScsiDiskDevice->ScsiIo;
  1326. MaxLbaCnt = ScsiDiskDevice->UnmapInfo.MaxLbaCnt;
  1327. MaxBlkDespCnt = ScsiDiskDevice->UnmapInfo.MaxBlkDespCnt;
  1328. EraseBlkReq = NULL;
  1329. UnmapParamList = NULL;
  1330. AsyncUnmapEvent = NULL;
  1331. ReturnStatus = EFI_SUCCESS;
  1332. if (Blocks / (UINTN)MaxLbaCnt > MaxBlkDespCnt) {
  1333. ReturnStatus = EFI_DEVICE_ERROR;
  1334. goto Done;
  1335. }
  1336. EraseBlkReq = AllocateZeroPool (sizeof (SCSI_ERASEBLK_REQUEST));
  1337. if (EraseBlkReq == NULL) {
  1338. ReturnStatus = EFI_DEVICE_ERROR;
  1339. goto Done;
  1340. }
  1341. EraseBlkReq->CommandPacket.Cdb = AllocateZeroPool (0xA);
  1342. if (EraseBlkReq->CommandPacket.Cdb == NULL) {
  1343. ReturnStatus = EFI_DEVICE_ERROR;
  1344. goto Done;
  1345. }
  1346. BlkDespCnt = (UINT32)((Blocks - 1) / MaxLbaCnt + 1);
  1347. UnmapParamListLen = (UINT16)(sizeof (EFI_SCSI_DISK_UNMAP_PARAM_LIST_HEADER)
  1348. + BlkDespCnt * sizeof (EFI_SCSI_DISK_UNMAP_BLOCK_DESP));
  1349. UnmapParamList = AllocateZeroPool (UnmapParamListLen);
  1350. if (UnmapParamList == NULL) {
  1351. ReturnStatus = EFI_DEVICE_ERROR;
  1352. goto Done;
  1353. }
  1354. *((UINT16 *)UnmapParamList) = SwapBytes16 (UnmapParamListLen - 2);
  1355. *((UINT16 *)UnmapParamList + 1) = SwapBytes16 (UnmapParamListLen - sizeof (EFI_SCSI_DISK_UNMAP_PARAM_LIST_HEADER));
  1356. BlkDespPtr = (EFI_SCSI_DISK_UNMAP_BLOCK_DESP *)((UINT8 *)UnmapParamList + sizeof (EFI_SCSI_DISK_UNMAP_PARAM_LIST_HEADER));
  1357. while (Blocks > 0) {
  1358. if (Blocks > MaxLbaCnt) {
  1359. *(UINT64 *)(&BlkDespPtr->Lba) = SwapBytes64 (Lba);
  1360. *(UINT32 *)(&BlkDespPtr->BlockNum) = SwapBytes32 (MaxLbaCnt);
  1361. Blocks -= MaxLbaCnt;
  1362. Lba += MaxLbaCnt;
  1363. } else {
  1364. *(UINT64 *)(&BlkDespPtr->Lba) = SwapBytes64 (Lba);
  1365. *(UINT32 *)(&BlkDespPtr->BlockNum) = SwapBytes32 ((UINT32)Blocks);
  1366. Blocks = 0;
  1367. }
  1368. BlkDespPtr++;
  1369. }
  1370. CommandPacket = &EraseBlkReq->CommandPacket;
  1371. CommandPacket->Timeout = SCSI_DISK_TIMEOUT;
  1372. CommandPacket->OutDataBuffer = UnmapParamList;
  1373. CommandPacket->OutTransferLength = UnmapParamListLen;
  1374. CommandPacket->CdbLength = 0xA;
  1375. CommandPacket->DataDirection = EFI_SCSI_DATA_OUT;
  1376. //
  1377. // Fill Cdb for UNMAP Command
  1378. //
  1379. Cdb = CommandPacket->Cdb;
  1380. Cdb[0] = EFI_SCSI_OP_UNMAP;
  1381. WriteUnaligned16 ((UINT16 *)&Cdb[7], SwapBytes16 (UnmapParamListLen));
  1382. if ((Token != NULL) && (Token->Event != NULL)) {
  1383. //
  1384. // Non-blocking UNMAP request
  1385. //
  1386. Status = gBS->CreateEvent (
  1387. EVT_NOTIFY_SIGNAL,
  1388. TPL_NOTIFY,
  1389. ScsiDiskAsyncUnmapNotify,
  1390. EraseBlkReq,
  1391. &AsyncUnmapEvent
  1392. );
  1393. if (EFI_ERROR (Status)) {
  1394. ReturnStatus = EFI_DEVICE_ERROR;
  1395. goto Done;
  1396. }
  1397. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  1398. InsertTailList (&ScsiDiskDevice->AsyncTaskQueue, &EraseBlkReq->Link);
  1399. gBS->RestoreTPL (OldTpl);
  1400. EraseBlkReq->Token = Token;
  1401. Status = ScsiIo->ExecuteScsiCommand (
  1402. ScsiIo,
  1403. CommandPacket,
  1404. AsyncUnmapEvent
  1405. );
  1406. if (EFI_ERROR (Status)) {
  1407. ReturnStatus = EFI_DEVICE_ERROR;
  1408. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  1409. RemoveEntryList (&EraseBlkReq->Link);
  1410. gBS->RestoreTPL (OldTpl);
  1411. goto Done;
  1412. } else {
  1413. //
  1414. // Directly return if the non-blocking UNMAP request is queued.
  1415. //
  1416. return EFI_SUCCESS;
  1417. }
  1418. } else {
  1419. //
  1420. // Blocking UNMAP request
  1421. //
  1422. Status = ScsiIo->ExecuteScsiCommand (
  1423. ScsiIo,
  1424. CommandPacket,
  1425. NULL
  1426. );
  1427. if (EFI_ERROR (Status)) {
  1428. ReturnStatus = EFI_DEVICE_ERROR;
  1429. goto Done;
  1430. }
  1431. }
  1432. //
  1433. // Only blocking UNMAP request will reach here.
  1434. //
  1435. Status = CheckHostAdapterStatus (CommandPacket->HostAdapterStatus);
  1436. if (EFI_ERROR (Status)) {
  1437. DEBUG ((
  1438. DEBUG_ERROR,
  1439. "ScsiDiskUnmap: Host adapter indicating error status 0x%x.\n",
  1440. CommandPacket->HostAdapterStatus
  1441. ));
  1442. ReturnStatus = EFI_DEVICE_ERROR;
  1443. goto Done;
  1444. }
  1445. Status = CheckTargetStatus (CommandPacket->TargetStatus);
  1446. if (EFI_ERROR (Status)) {
  1447. DEBUG ((
  1448. DEBUG_ERROR,
  1449. "ScsiDiskUnmap: Target indicating error status 0x%x.\n",
  1450. CommandPacket->HostAdapterStatus
  1451. ));
  1452. ReturnStatus = EFI_DEVICE_ERROR;
  1453. goto Done;
  1454. }
  1455. Done:
  1456. if (EraseBlkReq != NULL) {
  1457. if (EraseBlkReq->CommandPacket.Cdb != NULL) {
  1458. FreePool (EraseBlkReq->CommandPacket.Cdb);
  1459. }
  1460. FreePool (EraseBlkReq);
  1461. }
  1462. if (UnmapParamList != NULL) {
  1463. FreePool (UnmapParamList);
  1464. }
  1465. if (AsyncUnmapEvent != NULL) {
  1466. gBS->CloseEvent (AsyncUnmapEvent);
  1467. }
  1468. return ReturnStatus;
  1469. }
  1470. /**
  1471. Erase a specified number of device blocks.
  1472. @param[in] This Indicates a pointer to the calling context.
  1473. @param[in] MediaId The media ID that the erase request is for.
  1474. @param[in] Lba The starting logical block address to be
  1475. erased. The caller is responsible for erasing
  1476. only legitimate locations.
  1477. @param[in, out] Token A pointer to the token associated with the
  1478. transaction.
  1479. @param[in] Size The size in bytes to be erased. This must be
  1480. a multiple of the physical block size of the
  1481. device.
  1482. @retval EFI_SUCCESS The erase request was queued if Event is not
  1483. NULL. The data was erased correctly to the
  1484. device if the Event is NULL.to the device.
  1485. @retval EFI_WRITE_PROTECTED The device cannot be erased due to write
  1486. protection.
  1487. @retval EFI_DEVICE_ERROR The device reported an error while attempting
  1488. to perform the erase operation.
  1489. @retval EFI_INVALID_PARAMETER The erase request contains LBAs that are not
  1490. valid.
  1491. @retval EFI_NO_MEDIA There is no media in the device.
  1492. @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
  1493. **/
  1494. EFI_STATUS
  1495. EFIAPI
  1496. ScsiDiskEraseBlocks (
  1497. IN EFI_ERASE_BLOCK_PROTOCOL *This,
  1498. IN UINT32 MediaId,
  1499. IN EFI_LBA Lba,
  1500. IN OUT EFI_ERASE_BLOCK_TOKEN *Token,
  1501. IN UINTN Size
  1502. )
  1503. {
  1504. SCSI_DISK_DEV *ScsiDiskDevice;
  1505. EFI_BLOCK_IO_MEDIA *Media;
  1506. EFI_STATUS Status;
  1507. UINTN BlockSize;
  1508. UINTN NumberOfBlocks;
  1509. BOOLEAN MediaChange;
  1510. EFI_TPL OldTpl;
  1511. MediaChange = FALSE;
  1512. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  1513. ScsiDiskDevice = SCSI_DISK_DEV_FROM_ERASEBLK (This);
  1514. if (!IS_DEVICE_FIXED (ScsiDiskDevice)) {
  1515. Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange);
  1516. if (EFI_ERROR (Status)) {
  1517. Status = EFI_DEVICE_ERROR;
  1518. goto Done;
  1519. }
  1520. if (MediaChange) {
  1521. gBS->ReinstallProtocolInterface (
  1522. ScsiDiskDevice->Handle,
  1523. &gEfiBlockIoProtocolGuid,
  1524. &ScsiDiskDevice->BlkIo,
  1525. &ScsiDiskDevice->BlkIo
  1526. );
  1527. gBS->ReinstallProtocolInterface (
  1528. ScsiDiskDevice->Handle,
  1529. &gEfiBlockIo2ProtocolGuid,
  1530. &ScsiDiskDevice->BlkIo2,
  1531. &ScsiDiskDevice->BlkIo2
  1532. );
  1533. if (DetermineInstallEraseBlock (ScsiDiskDevice, ScsiDiskDevice->Handle)) {
  1534. gBS->ReinstallProtocolInterface (
  1535. ScsiDiskDevice->Handle,
  1536. &gEfiEraseBlockProtocolGuid,
  1537. &ScsiDiskDevice->EraseBlock,
  1538. &ScsiDiskDevice->EraseBlock
  1539. );
  1540. }
  1541. if (DetermineInstallStorageSecurity (ScsiDiskDevice, ScsiDiskDevice->Handle)) {
  1542. gBS->ReinstallProtocolInterface (
  1543. ScsiDiskDevice->Handle,
  1544. &gEfiStorageSecurityCommandProtocolGuid,
  1545. &ScsiDiskDevice->StorageSecurity,
  1546. &ScsiDiskDevice->StorageSecurity
  1547. );
  1548. }
  1549. Status = EFI_MEDIA_CHANGED;
  1550. goto Done;
  1551. }
  1552. }
  1553. //
  1554. // Get the intrinsic block size
  1555. //
  1556. Media = ScsiDiskDevice->BlkIo.Media;
  1557. if (!(Media->MediaPresent)) {
  1558. Status = EFI_NO_MEDIA;
  1559. goto Done;
  1560. }
  1561. if (MediaId != Media->MediaId) {
  1562. Status = EFI_MEDIA_CHANGED;
  1563. goto Done;
  1564. }
  1565. if (Media->ReadOnly) {
  1566. Status = EFI_WRITE_PROTECTED;
  1567. goto Done;
  1568. }
  1569. if (Size == 0) {
  1570. if ((Token != NULL) && (Token->Event != NULL)) {
  1571. Token->TransactionStatus = EFI_SUCCESS;
  1572. gBS->SignalEvent (Token->Event);
  1573. }
  1574. Status = EFI_SUCCESS;
  1575. goto Done;
  1576. }
  1577. BlockSize = Media->BlockSize;
  1578. if ((Size % BlockSize) != 0) {
  1579. Status = EFI_INVALID_PARAMETER;
  1580. goto Done;
  1581. }
  1582. NumberOfBlocks = Size / BlockSize;
  1583. if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {
  1584. Status = EFI_INVALID_PARAMETER;
  1585. goto Done;
  1586. }
  1587. if ((Token != NULL) && (Token->Event != NULL)) {
  1588. Status = ScsiDiskUnmap (ScsiDiskDevice, Lba, NumberOfBlocks, Token);
  1589. } else {
  1590. Status = ScsiDiskUnmap (ScsiDiskDevice, Lba, NumberOfBlocks, NULL);
  1591. }
  1592. Done:
  1593. gBS->RestoreTPL (OldTpl);
  1594. return Status;
  1595. }
  1596. /**
  1597. Send a security protocol command to a device that receives data and/or the result
  1598. of one or more commands sent by SendData.
  1599. The ReceiveData function sends a security protocol command to the given MediaId.
  1600. The security protocol command sent is defined by SecurityProtocolId and contains
  1601. the security protocol specific data SecurityProtocolSpecificData. The function
  1602. returns the data from the security protocol command in PayloadBuffer.
  1603. For devices supporting the SCSI command set, the security protocol command is sent
  1604. using the SECURITY PROTOCOL IN command defined in SPC-4.
  1605. If PayloadBufferSize is too small to store the available data from the security
  1606. protocol command, the function shall copy PayloadBufferSize bytes into the
  1607. PayloadBuffer and return EFI_WARN_BUFFER_TOO_SMALL.
  1608. If PayloadBuffer or PayloadTransferSize is NULL and PayloadBufferSize is non-zero,
  1609. the function shall return EFI_INVALID_PARAMETER.
  1610. If the given MediaId does not support security protocol commands, the function shall
  1611. return EFI_UNSUPPORTED. If there is no media in the device, the function returns
  1612. EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the device,
  1613. the function returns EFI_MEDIA_CHANGED.
  1614. If the security protocol fails to complete within the Timeout period, the function
  1615. shall return EFI_TIMEOUT.
  1616. If the security protocol command completes without an error, the function shall
  1617. return EFI_SUCCESS. If the security protocol command completes with an error, the
  1618. function shall return EFI_DEVICE_ERROR.
  1619. @param This Indicates a pointer to the calling context.
  1620. @param MediaId ID of the medium to receive data from.
  1621. @param Timeout The timeout, in 100ns units, to use for the execution
  1622. of the security protocol command. A Timeout value of 0
  1623. means that this function will wait indefinitely for the
  1624. security protocol command to execute. If Timeout is greater
  1625. than zero, then this function will return EFI_TIMEOUT if the
  1626. time required to execute the receive data command is greater than Timeout.
  1627. @param SecurityProtocolId The value of the "Security Protocol" parameter of
  1628. the security protocol command to be sent.
  1629. @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
  1630. of the security protocol command to be sent.
  1631. @param PayloadBufferSize Size in bytes of the payload data buffer.
  1632. @param PayloadBuffer A pointer to a destination buffer to store the security
  1633. protocol command specific payload data for the security
  1634. protocol command. The caller is responsible for having
  1635. either implicit or explicit ownership of the buffer.
  1636. @param PayloadTransferSize A pointer to a buffer to store the size in bytes of the
  1637. data written to the payload data buffer.
  1638. @retval EFI_SUCCESS The security protocol command completed successfully.
  1639. @retval EFI_WARN_BUFFER_TOO_SMALL The PayloadBufferSize was too small to store the available
  1640. data from the device. The PayloadBuffer contains the truncated data.
  1641. @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.
  1642. @retval EFI_DEVICE_ERROR The security protocol command completed with an error.
  1643. @retval EFI_NO_MEDIA There is no media in the device.
  1644. @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
  1645. @retval EFI_INVALID_PARAMETER The PayloadBuffer or PayloadTransferSize is NULL and
  1646. PayloadBufferSize is non-zero.
  1647. @retval EFI_TIMEOUT A timeout occurred while waiting for the security
  1648. protocol command to execute.
  1649. **/
  1650. EFI_STATUS
  1651. EFIAPI
  1652. ScsiDiskReceiveData (
  1653. IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,
  1654. IN UINT32 MediaId OPTIONAL,
  1655. IN UINT64 Timeout,
  1656. IN UINT8 SecurityProtocolId,
  1657. IN UINT16 SecurityProtocolSpecificData,
  1658. IN UINTN PayloadBufferSize,
  1659. OUT VOID *PayloadBuffer,
  1660. OUT UINTN *PayloadTransferSize
  1661. )
  1662. {
  1663. SCSI_DISK_DEV *ScsiDiskDevice;
  1664. EFI_BLOCK_IO_MEDIA *Media;
  1665. EFI_STATUS Status;
  1666. BOOLEAN MediaChange;
  1667. EFI_TPL OldTpl;
  1668. UINT8 SenseDataLength;
  1669. UINT8 HostAdapterStatus;
  1670. UINT8 TargetStatus;
  1671. VOID *AlignedBuffer;
  1672. BOOLEAN AlignedBufferAllocated;
  1673. AlignedBuffer = NULL;
  1674. MediaChange = FALSE;
  1675. AlignedBufferAllocated = FALSE;
  1676. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  1677. ScsiDiskDevice = SCSI_DISK_DEV_FROM_STORSEC (This);
  1678. Media = ScsiDiskDevice->BlkIo.Media;
  1679. SenseDataLength = (UINT8)(ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA));
  1680. if (!IS_DEVICE_FIXED (ScsiDiskDevice)) {
  1681. Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange);
  1682. if (EFI_ERROR (Status)) {
  1683. Status = EFI_DEVICE_ERROR;
  1684. goto Done;
  1685. }
  1686. if (MediaChange) {
  1687. gBS->ReinstallProtocolInterface (
  1688. ScsiDiskDevice->Handle,
  1689. &gEfiBlockIoProtocolGuid,
  1690. &ScsiDiskDevice->BlkIo,
  1691. &ScsiDiskDevice->BlkIo
  1692. );
  1693. gBS->ReinstallProtocolInterface (
  1694. ScsiDiskDevice->Handle,
  1695. &gEfiBlockIo2ProtocolGuid,
  1696. &ScsiDiskDevice->BlkIo2,
  1697. &ScsiDiskDevice->BlkIo2
  1698. );
  1699. if (DetermineInstallEraseBlock (ScsiDiskDevice, ScsiDiskDevice->Handle)) {
  1700. gBS->ReinstallProtocolInterface (
  1701. ScsiDiskDevice->Handle,
  1702. &gEfiEraseBlockProtocolGuid,
  1703. &ScsiDiskDevice->EraseBlock,
  1704. &ScsiDiskDevice->EraseBlock
  1705. );
  1706. }
  1707. if (DetermineInstallStorageSecurity (ScsiDiskDevice, ScsiDiskDevice->Handle)) {
  1708. gBS->ReinstallProtocolInterface (
  1709. ScsiDiskDevice->Handle,
  1710. &gEfiStorageSecurityCommandProtocolGuid,
  1711. &ScsiDiskDevice->StorageSecurity,
  1712. &ScsiDiskDevice->StorageSecurity
  1713. );
  1714. }
  1715. if (Media->MediaPresent) {
  1716. Status = EFI_MEDIA_CHANGED;
  1717. } else {
  1718. Status = EFI_NO_MEDIA;
  1719. }
  1720. goto Done;
  1721. }
  1722. }
  1723. //
  1724. // Validate Media
  1725. //
  1726. if (!(Media->MediaPresent)) {
  1727. Status = EFI_NO_MEDIA;
  1728. goto Done;
  1729. }
  1730. if ((MediaId != 0) && (MediaId != Media->MediaId)) {
  1731. Status = EFI_MEDIA_CHANGED;
  1732. goto Done;
  1733. }
  1734. if (PayloadBufferSize != 0) {
  1735. if ((PayloadBuffer == NULL) || (PayloadTransferSize == NULL)) {
  1736. Status = EFI_INVALID_PARAMETER;
  1737. goto Done;
  1738. }
  1739. if ((ScsiDiskDevice->ScsiIo->IoAlign > 1) && !IS_ALIGNED (PayloadBuffer, ScsiDiskDevice->ScsiIo->IoAlign)) {
  1740. AlignedBuffer = AllocateAlignedBuffer (ScsiDiskDevice, PayloadBufferSize);
  1741. if (AlignedBuffer == NULL) {
  1742. Status = EFI_OUT_OF_RESOURCES;
  1743. goto Done;
  1744. }
  1745. ZeroMem (AlignedBuffer, PayloadBufferSize);
  1746. AlignedBufferAllocated = TRUE;
  1747. } else {
  1748. AlignedBuffer = PayloadBuffer;
  1749. }
  1750. }
  1751. Status = ScsiSecurityProtocolInCommand (
  1752. ScsiDiskDevice->ScsiIo,
  1753. Timeout,
  1754. ScsiDiskDevice->SenseData,
  1755. &SenseDataLength,
  1756. &HostAdapterStatus,
  1757. &TargetStatus,
  1758. SecurityProtocolId,
  1759. SecurityProtocolSpecificData,
  1760. FALSE,
  1761. PayloadBufferSize,
  1762. AlignedBuffer,
  1763. PayloadTransferSize
  1764. );
  1765. if (EFI_ERROR (Status)) {
  1766. goto Done;
  1767. }
  1768. if (AlignedBufferAllocated) {
  1769. CopyMem (PayloadBuffer, AlignedBuffer, PayloadBufferSize);
  1770. }
  1771. if (PayloadBufferSize < *PayloadTransferSize) {
  1772. Status = EFI_WARN_BUFFER_TOO_SMALL;
  1773. goto Done;
  1774. }
  1775. Status = CheckHostAdapterStatus (HostAdapterStatus);
  1776. if (EFI_ERROR (Status)) {
  1777. goto Done;
  1778. }
  1779. Status = CheckTargetStatus (TargetStatus);
  1780. if (EFI_ERROR (Status)) {
  1781. goto Done;
  1782. }
  1783. Done:
  1784. if (AlignedBufferAllocated) {
  1785. ZeroMem (AlignedBuffer, PayloadBufferSize);
  1786. FreeAlignedBuffer (AlignedBuffer, PayloadBufferSize);
  1787. }
  1788. gBS->RestoreTPL (OldTpl);
  1789. return Status;
  1790. }
  1791. /**
  1792. Send a security protocol command to a device.
  1793. The SendData function sends a security protocol command containing the payload
  1794. PayloadBuffer to the given MediaId. The security protocol command sent is
  1795. defined by SecurityProtocolId and contains the security protocol specific data
  1796. SecurityProtocolSpecificData. If the underlying protocol command requires a
  1797. specific padding for the command payload, the SendData function shall add padding
  1798. bytes to the command payload to satisfy the padding requirements.
  1799. For devices supporting the SCSI command set, the security protocol command is sent
  1800. using the SECURITY PROTOCOL OUT command defined in SPC-4.
  1801. If PayloadBuffer is NULL and PayloadBufferSize is non-zero, the function shall
  1802. return EFI_INVALID_PARAMETER.
  1803. If the given MediaId does not support security protocol commands, the function
  1804. shall return EFI_UNSUPPORTED. If there is no media in the device, the function
  1805. returns EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the
  1806. device, the function returns EFI_MEDIA_CHANGED.
  1807. If the security protocol fails to complete within the Timeout period, the function
  1808. shall return EFI_TIMEOUT.
  1809. If the security protocol command completes without an error, the function shall return
  1810. EFI_SUCCESS. If the security protocol command completes with an error, the function
  1811. shall return EFI_DEVICE_ERROR.
  1812. @param This Indicates a pointer to the calling context.
  1813. @param MediaId ID of the medium to receive data from.
  1814. @param Timeout The timeout, in 100ns units, to use for the execution
  1815. of the security protocol command. A Timeout value of 0
  1816. means that this function will wait indefinitely for the
  1817. security protocol command to execute. If Timeout is greater
  1818. than zero, then this function will return EFI_TIMEOUT if the
  1819. time required to execute the receive data command is greater than Timeout.
  1820. @param SecurityProtocolId The value of the "Security Protocol" parameter of
  1821. the security protocol command to be sent.
  1822. @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
  1823. of the security protocol command to be sent.
  1824. @param PayloadBufferSize Size in bytes of the payload data buffer.
  1825. @param PayloadBuffer A pointer to a destination buffer to store the security
  1826. protocol command specific payload data for the security
  1827. protocol command.
  1828. @retval EFI_SUCCESS The security protocol command completed successfully.
  1829. @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.
  1830. @retval EFI_DEVICE_ERROR The security protocol command completed with an error.
  1831. @retval EFI_NO_MEDIA There is no media in the device.
  1832. @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
  1833. @retval EFI_INVALID_PARAMETER The PayloadBuffer is NULL and PayloadBufferSize is non-zero.
  1834. @retval EFI_TIMEOUT A timeout occurred while waiting for the security
  1835. protocol command to execute.
  1836. **/
  1837. EFI_STATUS
  1838. EFIAPI
  1839. ScsiDiskSendData (
  1840. IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,
  1841. IN UINT32 MediaId OPTIONAL,
  1842. IN UINT64 Timeout,
  1843. IN UINT8 SecurityProtocolId,
  1844. IN UINT16 SecurityProtocolSpecificData,
  1845. IN UINTN PayloadBufferSize,
  1846. OUT VOID *PayloadBuffer
  1847. )
  1848. {
  1849. SCSI_DISK_DEV *ScsiDiskDevice;
  1850. EFI_BLOCK_IO_MEDIA *Media;
  1851. EFI_STATUS Status;
  1852. BOOLEAN MediaChange;
  1853. EFI_TPL OldTpl;
  1854. UINT8 SenseDataLength;
  1855. UINT8 HostAdapterStatus;
  1856. UINT8 TargetStatus;
  1857. VOID *AlignedBuffer;
  1858. BOOLEAN AlignedBufferAllocated;
  1859. AlignedBuffer = NULL;
  1860. MediaChange = FALSE;
  1861. AlignedBufferAllocated = FALSE;
  1862. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  1863. ScsiDiskDevice = SCSI_DISK_DEV_FROM_STORSEC (This);
  1864. Media = ScsiDiskDevice->BlkIo.Media;
  1865. SenseDataLength = (UINT8)(ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA));
  1866. if (!IS_DEVICE_FIXED (ScsiDiskDevice)) {
  1867. Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange);
  1868. if (EFI_ERROR (Status)) {
  1869. Status = EFI_DEVICE_ERROR;
  1870. goto Done;
  1871. }
  1872. if (MediaChange) {
  1873. gBS->ReinstallProtocolInterface (
  1874. ScsiDiskDevice->Handle,
  1875. &gEfiBlockIoProtocolGuid,
  1876. &ScsiDiskDevice->BlkIo,
  1877. &ScsiDiskDevice->BlkIo
  1878. );
  1879. gBS->ReinstallProtocolInterface (
  1880. ScsiDiskDevice->Handle,
  1881. &gEfiBlockIo2ProtocolGuid,
  1882. &ScsiDiskDevice->BlkIo2,
  1883. &ScsiDiskDevice->BlkIo2
  1884. );
  1885. if (DetermineInstallEraseBlock (ScsiDiskDevice, ScsiDiskDevice->Handle)) {
  1886. gBS->ReinstallProtocolInterface (
  1887. ScsiDiskDevice->Handle,
  1888. &gEfiEraseBlockProtocolGuid,
  1889. &ScsiDiskDevice->EraseBlock,
  1890. &ScsiDiskDevice->EraseBlock
  1891. );
  1892. }
  1893. if (DetermineInstallStorageSecurity (ScsiDiskDevice, ScsiDiskDevice->Handle)) {
  1894. gBS->ReinstallProtocolInterface (
  1895. ScsiDiskDevice->Handle,
  1896. &gEfiStorageSecurityCommandProtocolGuid,
  1897. &ScsiDiskDevice->StorageSecurity,
  1898. &ScsiDiskDevice->StorageSecurity
  1899. );
  1900. }
  1901. if (Media->MediaPresent) {
  1902. Status = EFI_MEDIA_CHANGED;
  1903. } else {
  1904. Status = EFI_NO_MEDIA;
  1905. }
  1906. goto Done;
  1907. }
  1908. }
  1909. //
  1910. // Validate Media
  1911. //
  1912. if (!(Media->MediaPresent)) {
  1913. Status = EFI_NO_MEDIA;
  1914. goto Done;
  1915. }
  1916. if ((MediaId != 0) && (MediaId != Media->MediaId)) {
  1917. Status = EFI_MEDIA_CHANGED;
  1918. goto Done;
  1919. }
  1920. if (Media->ReadOnly) {
  1921. Status = EFI_WRITE_PROTECTED;
  1922. goto Done;
  1923. }
  1924. if (PayloadBufferSize != 0) {
  1925. if (PayloadBuffer == NULL) {
  1926. Status = EFI_INVALID_PARAMETER;
  1927. goto Done;
  1928. }
  1929. if ((ScsiDiskDevice->ScsiIo->IoAlign > 1) && !IS_ALIGNED (PayloadBuffer, ScsiDiskDevice->ScsiIo->IoAlign)) {
  1930. AlignedBuffer = AllocateAlignedBuffer (ScsiDiskDevice, PayloadBufferSize);
  1931. if (AlignedBuffer == NULL) {
  1932. Status = EFI_OUT_OF_RESOURCES;
  1933. goto Done;
  1934. }
  1935. CopyMem (AlignedBuffer, PayloadBuffer, PayloadBufferSize);
  1936. AlignedBufferAllocated = TRUE;
  1937. } else {
  1938. AlignedBuffer = PayloadBuffer;
  1939. }
  1940. }
  1941. Status = ScsiSecurityProtocolOutCommand (
  1942. ScsiDiskDevice->ScsiIo,
  1943. Timeout,
  1944. ScsiDiskDevice->SenseData,
  1945. &SenseDataLength,
  1946. &HostAdapterStatus,
  1947. &TargetStatus,
  1948. SecurityProtocolId,
  1949. SecurityProtocolSpecificData,
  1950. FALSE,
  1951. PayloadBufferSize,
  1952. AlignedBuffer
  1953. );
  1954. if (EFI_ERROR (Status)) {
  1955. goto Done;
  1956. }
  1957. Status = CheckHostAdapterStatus (HostAdapterStatus);
  1958. if (EFI_ERROR (Status)) {
  1959. goto Done;
  1960. }
  1961. Status = CheckTargetStatus (TargetStatus);
  1962. if (EFI_ERROR (Status)) {
  1963. goto Done;
  1964. }
  1965. Done:
  1966. if (AlignedBufferAllocated) {
  1967. ZeroMem (AlignedBuffer, PayloadBufferSize);
  1968. FreeAlignedBuffer (AlignedBuffer, PayloadBufferSize);
  1969. }
  1970. gBS->RestoreTPL (OldTpl);
  1971. return Status;
  1972. }
  1973. /**
  1974. Detect Device and read out capacity ,if error occurs, parse the sense key.
  1975. @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
  1976. @param MustReadCapacity The flag about reading device capacity
  1977. @param MediaChange The pointer of flag indicates if media has changed
  1978. @retval EFI_DEVICE_ERROR Indicates that error occurs
  1979. @retval EFI_SUCCESS Successfully to detect media
  1980. **/
  1981. EFI_STATUS
  1982. ScsiDiskDetectMedia (
  1983. IN SCSI_DISK_DEV *ScsiDiskDevice,
  1984. IN BOOLEAN MustReadCapacity,
  1985. OUT BOOLEAN *MediaChange
  1986. )
  1987. {
  1988. EFI_STATUS Status;
  1989. EFI_SCSI_SENSE_DATA *SenseData;
  1990. UINTN NumberOfSenseKeys;
  1991. BOOLEAN NeedRetry;
  1992. BOOLEAN NeedReadCapacity;
  1993. UINT8 Retry;
  1994. UINT8 MaxRetry;
  1995. EFI_BLOCK_IO_MEDIA OldMedia;
  1996. UINTN Action;
  1997. EFI_EVENT TimeoutEvt;
  1998. Status = EFI_SUCCESS;
  1999. SenseData = NULL;
  2000. NumberOfSenseKeys = 0;
  2001. Retry = 0;
  2002. MaxRetry = 3;
  2003. Action = ACTION_NO_ACTION;
  2004. NeedReadCapacity = FALSE;
  2005. *MediaChange = FALSE;
  2006. TimeoutEvt = NULL;
  2007. CopyMem (&OldMedia, ScsiDiskDevice->BlkIo.Media, sizeof (OldMedia));
  2008. Status = gBS->CreateEvent (
  2009. EVT_TIMER,
  2010. TPL_CALLBACK,
  2011. NULL,
  2012. NULL,
  2013. &TimeoutEvt
  2014. );
  2015. if (EFI_ERROR (Status)) {
  2016. return Status;
  2017. }
  2018. Status = gBS->SetTimer (TimeoutEvt, TimerRelative, EFI_TIMER_PERIOD_SECONDS (120));
  2019. if (EFI_ERROR (Status)) {
  2020. goto EXIT;
  2021. }
  2022. //
  2023. // Sending Test_Unit cmd to poll device status.
  2024. // If the sense data shows the drive is not ready or reset before, we need poll the device status again.
  2025. // We limit the upper boundary to 120 seconds.
  2026. //
  2027. while (EFI_ERROR (gBS->CheckEvent (TimeoutEvt))) {
  2028. Status = ScsiDiskTestUnitReady (
  2029. ScsiDiskDevice,
  2030. &NeedRetry,
  2031. &SenseData,
  2032. &NumberOfSenseKeys
  2033. );
  2034. if (!EFI_ERROR (Status)) {
  2035. Status = DetectMediaParsingSenseKeys (
  2036. ScsiDiskDevice,
  2037. SenseData,
  2038. NumberOfSenseKeys,
  2039. &Action
  2040. );
  2041. if (EFI_ERROR (Status)) {
  2042. goto EXIT;
  2043. } else if (Action == ACTION_RETRY_COMMAND_LATER) {
  2044. continue;
  2045. } else {
  2046. break;
  2047. }
  2048. } else {
  2049. Retry++;
  2050. if (!NeedRetry || (Retry >= MaxRetry)) {
  2051. goto EXIT;
  2052. }
  2053. }
  2054. }
  2055. if (EFI_ERROR (Status)) {
  2056. goto EXIT;
  2057. }
  2058. //
  2059. // ACTION_NO_ACTION: need not read capacity
  2060. // other action code: need read capacity
  2061. //
  2062. if (Action == ACTION_READ_CAPACITY) {
  2063. NeedReadCapacity = TRUE;
  2064. }
  2065. //
  2066. // READ_CAPACITY command is not supported by any of the UFS WLUNs.
  2067. //
  2068. if (ScsiDiskDevice->DeviceType == EFI_SCSI_TYPE_WLUN) {
  2069. NeedReadCapacity = FALSE;
  2070. MustReadCapacity = FALSE;
  2071. ScsiDiskDevice->BlkIo.Media->MediaPresent = TRUE;
  2072. }
  2073. //
  2074. // either NeedReadCapacity is TRUE, or MustReadCapacity is TRUE,
  2075. // retrieve capacity via Read Capacity command
  2076. //
  2077. if (NeedReadCapacity || MustReadCapacity) {
  2078. //
  2079. // retrieve media information
  2080. //
  2081. for (Retry = 0; Retry < MaxRetry; Retry++) {
  2082. Status = ScsiDiskReadCapacity (
  2083. ScsiDiskDevice,
  2084. &NeedRetry,
  2085. &SenseData,
  2086. &NumberOfSenseKeys
  2087. );
  2088. if (!EFI_ERROR (Status)) {
  2089. //
  2090. // analyze sense key to action
  2091. //
  2092. Status = DetectMediaParsingSenseKeys (
  2093. ScsiDiskDevice,
  2094. SenseData,
  2095. NumberOfSenseKeys,
  2096. &Action
  2097. );
  2098. if (EFI_ERROR (Status)) {
  2099. //
  2100. // if Status is error, it may indicate crisis error,
  2101. // so return without retry.
  2102. //
  2103. goto EXIT;
  2104. } else if (Action == ACTION_RETRY_COMMAND_LATER) {
  2105. Retry = 0;
  2106. continue;
  2107. } else {
  2108. break;
  2109. }
  2110. } else {
  2111. Retry++;
  2112. if (!NeedRetry || (Retry >= MaxRetry)) {
  2113. goto EXIT;
  2114. }
  2115. }
  2116. }
  2117. if (EFI_ERROR (Status)) {
  2118. goto EXIT;
  2119. }
  2120. }
  2121. if (ScsiDiskDevice->BlkIo.Media->MediaId != OldMedia.MediaId) {
  2122. //
  2123. // Media change information got from the device
  2124. //
  2125. *MediaChange = TRUE;
  2126. }
  2127. if (ScsiDiskDevice->BlkIo.Media->ReadOnly != OldMedia.ReadOnly) {
  2128. *MediaChange = TRUE;
  2129. ScsiDiskDevice->BlkIo.Media->MediaId += 1;
  2130. }
  2131. if (ScsiDiskDevice->BlkIo.Media->BlockSize != OldMedia.BlockSize) {
  2132. *MediaChange = TRUE;
  2133. ScsiDiskDevice->BlkIo.Media->MediaId += 1;
  2134. }
  2135. if (ScsiDiskDevice->BlkIo.Media->LastBlock != OldMedia.LastBlock) {
  2136. *MediaChange = TRUE;
  2137. ScsiDiskDevice->BlkIo.Media->MediaId += 1;
  2138. }
  2139. if (ScsiDiskDevice->BlkIo.Media->MediaPresent != OldMedia.MediaPresent) {
  2140. if (ScsiDiskDevice->BlkIo.Media->MediaPresent) {
  2141. //
  2142. // when change from no media to media present, reset the MediaId to 1.
  2143. //
  2144. ScsiDiskDevice->BlkIo.Media->MediaId = 1;
  2145. } else {
  2146. //
  2147. // when no media, reset the MediaId to zero.
  2148. //
  2149. ScsiDiskDevice->BlkIo.Media->MediaId = 0;
  2150. }
  2151. *MediaChange = TRUE;
  2152. }
  2153. EXIT:
  2154. if (TimeoutEvt != NULL) {
  2155. gBS->CloseEvent (TimeoutEvt);
  2156. }
  2157. return Status;
  2158. }
  2159. /**
  2160. Send out Inquiry command to Device.
  2161. @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
  2162. @param NeedRetry Indicates if needs try again when error happens
  2163. @retval EFI_DEVICE_ERROR Indicates that error occurs
  2164. @retval EFI_SUCCESS Successfully to detect media
  2165. **/
  2166. EFI_STATUS
  2167. ScsiDiskInquiryDevice (
  2168. IN OUT SCSI_DISK_DEV *ScsiDiskDevice,
  2169. OUT BOOLEAN *NeedRetry
  2170. )
  2171. {
  2172. UINT32 InquiryDataLength;
  2173. UINT8 SenseDataLength;
  2174. UINT8 HostAdapterStatus;
  2175. UINT8 TargetStatus;
  2176. EFI_SCSI_SENSE_DATA *SenseDataArray;
  2177. UINTN NumberOfSenseKeys;
  2178. EFI_STATUS Status;
  2179. UINT8 MaxRetry;
  2180. UINT8 Index;
  2181. EFI_SCSI_SUPPORTED_VPD_PAGES_VPD_PAGE *SupportedVpdPages;
  2182. EFI_SCSI_BLOCK_LIMITS_VPD_PAGE *BlockLimits;
  2183. UINTN PageLength;
  2184. InquiryDataLength = sizeof (EFI_SCSI_INQUIRY_DATA);
  2185. SenseDataLength = 0;
  2186. Status = ScsiInquiryCommand (
  2187. ScsiDiskDevice->ScsiIo,
  2188. SCSI_DISK_TIMEOUT,
  2189. NULL,
  2190. &SenseDataLength,
  2191. &HostAdapterStatus,
  2192. &TargetStatus,
  2193. (VOID *)&(ScsiDiskDevice->InquiryData),
  2194. &InquiryDataLength,
  2195. FALSE
  2196. );
  2197. //
  2198. // no need to check HostAdapterStatus and TargetStatus
  2199. //
  2200. if ((Status == EFI_SUCCESS) || (Status == EFI_WARN_BUFFER_TOO_SMALL)) {
  2201. ParseInquiryData (ScsiDiskDevice);
  2202. if (ScsiDiskDevice->DeviceType == EFI_SCSI_TYPE_DISK) {
  2203. //
  2204. // Check whether the device supports Block Limits VPD page (0xB0)
  2205. //
  2206. SupportedVpdPages = AllocateAlignedBuffer (ScsiDiskDevice, sizeof (EFI_SCSI_SUPPORTED_VPD_PAGES_VPD_PAGE));
  2207. if (SupportedVpdPages == NULL) {
  2208. *NeedRetry = FALSE;
  2209. return EFI_DEVICE_ERROR;
  2210. }
  2211. ZeroMem (SupportedVpdPages, sizeof (EFI_SCSI_SUPPORTED_VPD_PAGES_VPD_PAGE));
  2212. InquiryDataLength = sizeof (EFI_SCSI_SUPPORTED_VPD_PAGES_VPD_PAGE);
  2213. SenseDataLength = 0;
  2214. Status = ScsiInquiryCommandEx (
  2215. ScsiDiskDevice->ScsiIo,
  2216. SCSI_DISK_TIMEOUT,
  2217. NULL,
  2218. &SenseDataLength,
  2219. &HostAdapterStatus,
  2220. &TargetStatus,
  2221. (VOID *)SupportedVpdPages,
  2222. &InquiryDataLength,
  2223. TRUE,
  2224. EFI_SCSI_PAGE_CODE_SUPPORTED_VPD
  2225. );
  2226. if (!EFI_ERROR (Status)) {
  2227. PageLength = (SupportedVpdPages->PageLength2 << 8)
  2228. | SupportedVpdPages->PageLength1;
  2229. //
  2230. // Sanity checks for coping with broken devices
  2231. //
  2232. if (PageLength > sizeof SupportedVpdPages->SupportedVpdPageList) {
  2233. DEBUG ((
  2234. DEBUG_WARN,
  2235. "%a: invalid PageLength (%u) in Supported VPD Pages page\n",
  2236. __FUNCTION__,
  2237. (UINT32)PageLength
  2238. ));
  2239. PageLength = 0;
  2240. }
  2241. if ((PageLength > 0) &&
  2242. (SupportedVpdPages->SupportedVpdPageList[0] !=
  2243. EFI_SCSI_PAGE_CODE_SUPPORTED_VPD))
  2244. {
  2245. DEBUG ((
  2246. DEBUG_WARN,
  2247. "%a: Supported VPD Pages page doesn't start with code 0x%02x\n",
  2248. __FUNCTION__,
  2249. EFI_SCSI_PAGE_CODE_SUPPORTED_VPD
  2250. ));
  2251. PageLength = 0;
  2252. }
  2253. //
  2254. // Locate the code for the Block Limits VPD page
  2255. //
  2256. for (Index = 0; Index < PageLength; Index++) {
  2257. //
  2258. // Sanity check
  2259. //
  2260. if ((Index > 0) &&
  2261. (SupportedVpdPages->SupportedVpdPageList[Index] <=
  2262. SupportedVpdPages->SupportedVpdPageList[Index - 1]))
  2263. {
  2264. DEBUG ((
  2265. DEBUG_WARN,
  2266. "%a: non-ascending code in Supported VPD Pages page @ %u\n",
  2267. __FUNCTION__,
  2268. Index
  2269. ));
  2270. Index = 0;
  2271. PageLength = 0;
  2272. break;
  2273. }
  2274. if (SupportedVpdPages->SupportedVpdPageList[Index] == EFI_SCSI_PAGE_CODE_BLOCK_LIMITS_VPD) {
  2275. break;
  2276. }
  2277. }
  2278. //
  2279. // Query the Block Limits VPD page
  2280. //
  2281. if (Index < PageLength) {
  2282. BlockLimits = AllocateAlignedBuffer (ScsiDiskDevice, sizeof (EFI_SCSI_BLOCK_LIMITS_VPD_PAGE));
  2283. if (BlockLimits == NULL) {
  2284. FreeAlignedBuffer (SupportedVpdPages, sizeof (EFI_SCSI_SUPPORTED_VPD_PAGES_VPD_PAGE));
  2285. *NeedRetry = FALSE;
  2286. return EFI_DEVICE_ERROR;
  2287. }
  2288. ZeroMem (BlockLimits, sizeof (EFI_SCSI_BLOCK_LIMITS_VPD_PAGE));
  2289. InquiryDataLength = sizeof (EFI_SCSI_BLOCK_LIMITS_VPD_PAGE);
  2290. SenseDataLength = 0;
  2291. Status = ScsiInquiryCommandEx (
  2292. ScsiDiskDevice->ScsiIo,
  2293. SCSI_DISK_TIMEOUT,
  2294. NULL,
  2295. &SenseDataLength,
  2296. &HostAdapterStatus,
  2297. &TargetStatus,
  2298. (VOID *)BlockLimits,
  2299. &InquiryDataLength,
  2300. TRUE,
  2301. EFI_SCSI_PAGE_CODE_BLOCK_LIMITS_VPD
  2302. );
  2303. if (!EFI_ERROR (Status)) {
  2304. ScsiDiskDevice->BlkIo.Media->OptimalTransferLengthGranularity =
  2305. (BlockLimits->OptimalTransferLengthGranularity2 << 8) |
  2306. BlockLimits->OptimalTransferLengthGranularity1;
  2307. ScsiDiskDevice->UnmapInfo.MaxLbaCnt =
  2308. (BlockLimits->MaximumUnmapLbaCount4 << 24) |
  2309. (BlockLimits->MaximumUnmapLbaCount3 << 16) |
  2310. (BlockLimits->MaximumUnmapLbaCount2 << 8) |
  2311. BlockLimits->MaximumUnmapLbaCount1;
  2312. ScsiDiskDevice->UnmapInfo.MaxBlkDespCnt =
  2313. (BlockLimits->MaximumUnmapBlockDescriptorCount4 << 24) |
  2314. (BlockLimits->MaximumUnmapBlockDescriptorCount3 << 16) |
  2315. (BlockLimits->MaximumUnmapBlockDescriptorCount2 << 8) |
  2316. BlockLimits->MaximumUnmapBlockDescriptorCount1;
  2317. ScsiDiskDevice->EraseBlock.EraseLengthGranularity =
  2318. (BlockLimits->OptimalUnmapGranularity4 << 24) |
  2319. (BlockLimits->OptimalUnmapGranularity3 << 16) |
  2320. (BlockLimits->OptimalUnmapGranularity2 << 8) |
  2321. BlockLimits->OptimalUnmapGranularity1;
  2322. if (BlockLimits->UnmapGranularityAlignmentValid != 0) {
  2323. ScsiDiskDevice->UnmapInfo.GranularityAlignment =
  2324. (BlockLimits->UnmapGranularityAlignment4 << 24) |
  2325. (BlockLimits->UnmapGranularityAlignment3 << 16) |
  2326. (BlockLimits->UnmapGranularityAlignment2 << 8) |
  2327. BlockLimits->UnmapGranularityAlignment1;
  2328. }
  2329. if (ScsiDiskDevice->EraseBlock.EraseLengthGranularity == 0) {
  2330. //
  2331. // A value of 0 indicates that the optimal unmap granularity is
  2332. // not reported.
  2333. //
  2334. ScsiDiskDevice->EraseBlock.EraseLengthGranularity = 1;
  2335. }
  2336. ScsiDiskDevice->BlockLimitsVpdSupported = TRUE;
  2337. }
  2338. FreeAlignedBuffer (BlockLimits, sizeof (EFI_SCSI_BLOCK_LIMITS_VPD_PAGE));
  2339. }
  2340. }
  2341. FreeAlignedBuffer (SupportedVpdPages, sizeof (EFI_SCSI_SUPPORTED_VPD_PAGES_VPD_PAGE));
  2342. }
  2343. }
  2344. if (!EFI_ERROR (Status)) {
  2345. return EFI_SUCCESS;
  2346. } else if (Status == EFI_NOT_READY) {
  2347. *NeedRetry = TRUE;
  2348. return EFI_DEVICE_ERROR;
  2349. } else if ((Status == EFI_INVALID_PARAMETER) || (Status == EFI_UNSUPPORTED)) {
  2350. *NeedRetry = FALSE;
  2351. return EFI_DEVICE_ERROR;
  2352. }
  2353. //
  2354. // go ahead to check HostAdapterStatus and TargetStatus
  2355. // (EFI_TIMEOUT, EFI_DEVICE_ERROR)
  2356. //
  2357. Status = CheckHostAdapterStatus (HostAdapterStatus);
  2358. if ((Status == EFI_TIMEOUT) || (Status == EFI_NOT_READY)) {
  2359. *NeedRetry = TRUE;
  2360. return EFI_DEVICE_ERROR;
  2361. } else if (Status == EFI_DEVICE_ERROR) {
  2362. //
  2363. // reset the scsi channel
  2364. //
  2365. ScsiDiskDevice->ScsiIo->ResetBus (ScsiDiskDevice->ScsiIo);
  2366. *NeedRetry = FALSE;
  2367. return EFI_DEVICE_ERROR;
  2368. }
  2369. Status = CheckTargetStatus (TargetStatus);
  2370. if (Status == EFI_NOT_READY) {
  2371. //
  2372. // reset the scsi device
  2373. //
  2374. ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo);
  2375. *NeedRetry = TRUE;
  2376. return EFI_DEVICE_ERROR;
  2377. } else if (Status == EFI_DEVICE_ERROR) {
  2378. *NeedRetry = FALSE;
  2379. return EFI_DEVICE_ERROR;
  2380. }
  2381. //
  2382. // if goes here, meant ScsiInquiryCommand() failed.
  2383. // if ScsiDiskRequestSenseKeys() succeeds at last,
  2384. // better retry ScsiInquiryCommand(). (by setting *NeedRetry = TRUE)
  2385. //
  2386. MaxRetry = 3;
  2387. for (Index = 0; Index < MaxRetry; Index++) {
  2388. Status = ScsiDiskRequestSenseKeys (
  2389. ScsiDiskDevice,
  2390. NeedRetry,
  2391. &SenseDataArray,
  2392. &NumberOfSenseKeys,
  2393. TRUE
  2394. );
  2395. if (!EFI_ERROR (Status)) {
  2396. *NeedRetry = TRUE;
  2397. return EFI_DEVICE_ERROR;
  2398. }
  2399. if (!*NeedRetry) {
  2400. return EFI_DEVICE_ERROR;
  2401. }
  2402. }
  2403. //
  2404. // ScsiDiskRequestSenseKeys() failed after several rounds of retry.
  2405. // set *NeedRetry = FALSE to avoid the outside caller try again.
  2406. //
  2407. *NeedRetry = FALSE;
  2408. return EFI_DEVICE_ERROR;
  2409. }
  2410. /**
  2411. To test device.
  2412. When Test Unit Ready command succeeds, retrieve Sense Keys via Request Sense;
  2413. When Test Unit Ready command encounters any error caused by host adapter or
  2414. target, return error without retrieving Sense Keys.
  2415. @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
  2416. @param NeedRetry The pointer of flag indicates try again
  2417. @param SenseDataArray The pointer of an array of sense data
  2418. @param NumberOfSenseKeys The pointer of the number of sense data array
  2419. @retval EFI_DEVICE_ERROR Indicates that error occurs
  2420. @retval EFI_SUCCESS Successfully to test unit
  2421. **/
  2422. EFI_STATUS
  2423. ScsiDiskTestUnitReady (
  2424. IN SCSI_DISK_DEV *ScsiDiskDevice,
  2425. OUT BOOLEAN *NeedRetry,
  2426. OUT EFI_SCSI_SENSE_DATA **SenseDataArray,
  2427. OUT UINTN *NumberOfSenseKeys
  2428. )
  2429. {
  2430. EFI_STATUS Status;
  2431. UINT8 SenseDataLength;
  2432. UINT8 HostAdapterStatus;
  2433. UINT8 TargetStatus;
  2434. UINT8 Index;
  2435. UINT8 MaxRetry;
  2436. SenseDataLength = (UINT8)(ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA));
  2437. *NumberOfSenseKeys = 0;
  2438. //
  2439. // Parameter 3 and 4: do not require sense data, retrieve it when needed.
  2440. //
  2441. Status = ScsiTestUnitReadyCommand (
  2442. ScsiDiskDevice->ScsiIo,
  2443. SCSI_DISK_TIMEOUT,
  2444. ScsiDiskDevice->SenseData,
  2445. &SenseDataLength,
  2446. &HostAdapterStatus,
  2447. &TargetStatus
  2448. );
  2449. //
  2450. // no need to check HostAdapterStatus and TargetStatus
  2451. //
  2452. if (Status == EFI_NOT_READY) {
  2453. *NeedRetry = TRUE;
  2454. return EFI_DEVICE_ERROR;
  2455. } else if ((Status == EFI_INVALID_PARAMETER) || (Status == EFI_UNSUPPORTED)) {
  2456. *NeedRetry = FALSE;
  2457. return EFI_DEVICE_ERROR;
  2458. }
  2459. //
  2460. // go ahead to check HostAdapterStatus and TargetStatus(in case of EFI_DEVICE_ERROR)
  2461. //
  2462. Status = CheckHostAdapterStatus (HostAdapterStatus);
  2463. if ((Status == EFI_TIMEOUT) || (Status == EFI_NOT_READY)) {
  2464. *NeedRetry = TRUE;
  2465. return EFI_DEVICE_ERROR;
  2466. } else if (Status == EFI_DEVICE_ERROR) {
  2467. //
  2468. // reset the scsi channel
  2469. //
  2470. ScsiDiskDevice->ScsiIo->ResetBus (ScsiDiskDevice->ScsiIo);
  2471. *NeedRetry = FALSE;
  2472. return EFI_DEVICE_ERROR;
  2473. }
  2474. Status = CheckTargetStatus (TargetStatus);
  2475. if (Status == EFI_NOT_READY) {
  2476. //
  2477. // reset the scsi device
  2478. //
  2479. ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo);
  2480. *NeedRetry = TRUE;
  2481. return EFI_DEVICE_ERROR;
  2482. } else if (Status == EFI_DEVICE_ERROR) {
  2483. *NeedRetry = FALSE;
  2484. return EFI_DEVICE_ERROR;
  2485. }
  2486. if (SenseDataLength != 0) {
  2487. *NumberOfSenseKeys = SenseDataLength / sizeof (EFI_SCSI_SENSE_DATA);
  2488. *SenseDataArray = ScsiDiskDevice->SenseData;
  2489. return EFI_SUCCESS;
  2490. }
  2491. MaxRetry = 3;
  2492. for (Index = 0; Index < MaxRetry; Index++) {
  2493. Status = ScsiDiskRequestSenseKeys (
  2494. ScsiDiskDevice,
  2495. NeedRetry,
  2496. SenseDataArray,
  2497. NumberOfSenseKeys,
  2498. FALSE
  2499. );
  2500. if (!EFI_ERROR (Status)) {
  2501. return EFI_SUCCESS;
  2502. }
  2503. if (!*NeedRetry) {
  2504. return EFI_DEVICE_ERROR;
  2505. }
  2506. }
  2507. //
  2508. // ScsiDiskRequestSenseKeys() failed after several rounds of retry.
  2509. // set *NeedRetry = FALSE to avoid the outside caller try again.
  2510. //
  2511. *NeedRetry = FALSE;
  2512. return EFI_DEVICE_ERROR;
  2513. }
  2514. /**
  2515. Parsing Sense Keys which got from request sense command.
  2516. @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
  2517. @param SenseData The pointer of EFI_SCSI_SENSE_DATA
  2518. @param NumberOfSenseKeys The number of sense key
  2519. @param Action The pointer of action which indicates what is need to do next
  2520. @retval EFI_DEVICE_ERROR Indicates that error occurs
  2521. @retval EFI_SUCCESS Successfully to complete the parsing
  2522. **/
  2523. EFI_STATUS
  2524. DetectMediaParsingSenseKeys (
  2525. OUT SCSI_DISK_DEV *ScsiDiskDevice,
  2526. IN EFI_SCSI_SENSE_DATA *SenseData,
  2527. IN UINTN NumberOfSenseKeys,
  2528. OUT UINTN *Action
  2529. )
  2530. {
  2531. BOOLEAN RetryLater;
  2532. //
  2533. // Default is to read capacity, unless..
  2534. //
  2535. *Action = ACTION_READ_CAPACITY;
  2536. if (NumberOfSenseKeys == 0) {
  2537. if (ScsiDiskDevice->BlkIo.Media->MediaPresent == TRUE) {
  2538. *Action = ACTION_NO_ACTION;
  2539. }
  2540. return EFI_SUCCESS;
  2541. }
  2542. if (!ScsiDiskHaveSenseKey (SenseData, NumberOfSenseKeys)) {
  2543. //
  2544. // No Sense Key returned from last submitted command
  2545. //
  2546. if (ScsiDiskDevice->BlkIo.Media->MediaPresent == TRUE) {
  2547. *Action = ACTION_NO_ACTION;
  2548. }
  2549. return EFI_SUCCESS;
  2550. }
  2551. if (ScsiDiskIsNoMedia (SenseData, NumberOfSenseKeys)) {
  2552. ScsiDiskDevice->BlkIo.Media->MediaPresent = FALSE;
  2553. ScsiDiskDevice->BlkIo.Media->LastBlock = 0;
  2554. *Action = ACTION_NO_ACTION;
  2555. DEBUG ((DEBUG_VERBOSE, "ScsiDisk: ScsiDiskIsNoMedia\n"));
  2556. return EFI_SUCCESS;
  2557. }
  2558. if (ScsiDiskIsMediaChange (SenseData, NumberOfSenseKeys)) {
  2559. ScsiDiskDevice->BlkIo.Media->MediaId++;
  2560. DEBUG ((DEBUG_VERBOSE, "ScsiDisk: ScsiDiskIsMediaChange!\n"));
  2561. return EFI_SUCCESS;
  2562. }
  2563. if (ScsiDiskIsResetBefore (SenseData, NumberOfSenseKeys)) {
  2564. *Action = ACTION_RETRY_COMMAND_LATER;
  2565. DEBUG ((DEBUG_VERBOSE, "ScsiDisk: ScsiDiskIsResetBefore!\n"));
  2566. return EFI_SUCCESS;
  2567. }
  2568. if (ScsiDiskIsMediaError (SenseData, NumberOfSenseKeys)) {
  2569. DEBUG ((DEBUG_VERBOSE, "ScsiDisk: ScsiDiskIsMediaError\n"));
  2570. *Action = ACTION_RETRY_WITH_BACKOFF_ALGO;
  2571. return EFI_DEVICE_ERROR;
  2572. }
  2573. if (ScsiDiskIsHardwareError (SenseData, NumberOfSenseKeys)) {
  2574. DEBUG ((DEBUG_VERBOSE, "ScsiDisk: ScsiDiskIsHardwareError\n"));
  2575. *Action = ACTION_RETRY_WITH_BACKOFF_ALGO;
  2576. return EFI_DEVICE_ERROR;
  2577. }
  2578. if (!ScsiDiskIsDriveReady (SenseData, NumberOfSenseKeys, &RetryLater)) {
  2579. if (RetryLater) {
  2580. *Action = ACTION_RETRY_COMMAND_LATER;
  2581. DEBUG ((DEBUG_VERBOSE, "ScsiDisk: ScsiDiskDriveNotReady!\n"));
  2582. return EFI_SUCCESS;
  2583. }
  2584. *Action = ACTION_NO_ACTION;
  2585. return EFI_DEVICE_ERROR;
  2586. }
  2587. *Action = ACTION_RETRY_WITH_BACKOFF_ALGO;
  2588. DEBUG ((DEBUG_VERBOSE, "ScsiDisk: Sense Key = 0x%x ASC = 0x%x!\n", SenseData->Sense_Key, SenseData->Addnl_Sense_Code));
  2589. return EFI_SUCCESS;
  2590. }
  2591. /**
  2592. Send read capacity command to device and get the device parameter.
  2593. @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
  2594. @param NeedRetry The pointer of flag indicates if need a retry
  2595. @param SenseDataArray The pointer of an array of sense data
  2596. @param NumberOfSenseKeys The number of sense key
  2597. @retval EFI_DEVICE_ERROR Indicates that error occurs
  2598. @retval EFI_SUCCESS Successfully to read capacity or sense data is received.
  2599. **/
  2600. EFI_STATUS
  2601. ScsiDiskReadCapacity (
  2602. IN OUT SCSI_DISK_DEV *ScsiDiskDevice,
  2603. OUT BOOLEAN *NeedRetry,
  2604. OUT EFI_SCSI_SENSE_DATA **SenseDataArray,
  2605. OUT UINTN *NumberOfSenseKeys
  2606. )
  2607. {
  2608. UINT8 HostAdapterStatus;
  2609. UINT8 TargetStatus;
  2610. EFI_STATUS CommandStatus;
  2611. EFI_STATUS Status;
  2612. UINT8 Index;
  2613. UINT8 MaxRetry;
  2614. UINT8 SenseDataLength;
  2615. UINT32 DataLength10;
  2616. UINT32 DataLength16;
  2617. EFI_SCSI_DISK_CAPACITY_DATA *CapacityData10;
  2618. EFI_SCSI_DISK_CAPACITY_DATA16 *CapacityData16;
  2619. CapacityData10 = AllocateAlignedBuffer (ScsiDiskDevice, sizeof (EFI_SCSI_DISK_CAPACITY_DATA));
  2620. if (CapacityData10 == NULL) {
  2621. *NeedRetry = FALSE;
  2622. return EFI_DEVICE_ERROR;
  2623. }
  2624. CapacityData16 = AllocateAlignedBuffer (ScsiDiskDevice, sizeof (EFI_SCSI_DISK_CAPACITY_DATA16));
  2625. if (CapacityData16 == NULL) {
  2626. FreeAlignedBuffer (CapacityData10, sizeof (EFI_SCSI_DISK_CAPACITY_DATA));
  2627. *NeedRetry = FALSE;
  2628. return EFI_DEVICE_ERROR;
  2629. }
  2630. SenseDataLength = 0;
  2631. DataLength10 = sizeof (EFI_SCSI_DISK_CAPACITY_DATA);
  2632. DataLength16 = sizeof (EFI_SCSI_DISK_CAPACITY_DATA16);
  2633. ZeroMem (CapacityData10, sizeof (EFI_SCSI_DISK_CAPACITY_DATA));
  2634. ZeroMem (CapacityData16, sizeof (EFI_SCSI_DISK_CAPACITY_DATA16));
  2635. *NumberOfSenseKeys = 0;
  2636. *NeedRetry = FALSE;
  2637. //
  2638. // submit Read Capacity(10) Command. If it returns capacity of FFFFFFFFh,
  2639. // 16 byte command should be used to access large hard disk >2TB
  2640. //
  2641. CommandStatus = ScsiReadCapacityCommand (
  2642. ScsiDiskDevice->ScsiIo,
  2643. SCSI_DISK_TIMEOUT,
  2644. NULL,
  2645. &SenseDataLength,
  2646. &HostAdapterStatus,
  2647. &TargetStatus,
  2648. (VOID *)CapacityData10,
  2649. &DataLength10,
  2650. FALSE
  2651. );
  2652. ScsiDiskDevice->Cdb16Byte = FALSE;
  2653. if ((!EFI_ERROR (CommandStatus)) && (CapacityData10->LastLba3 == 0xff) && (CapacityData10->LastLba2 == 0xff) &&
  2654. (CapacityData10->LastLba1 == 0xff) && (CapacityData10->LastLba0 == 0xff))
  2655. {
  2656. //
  2657. // use Read Capacity (16), Read (16) and Write (16) next when hard disk size > 2TB
  2658. //
  2659. ScsiDiskDevice->Cdb16Byte = TRUE;
  2660. //
  2661. // submit Read Capacity(16) Command to get parameter LogicalBlocksPerPhysicalBlock
  2662. // and LowestAlignedLba
  2663. //
  2664. CommandStatus = ScsiReadCapacity16Command (
  2665. ScsiDiskDevice->ScsiIo,
  2666. SCSI_DISK_TIMEOUT,
  2667. NULL,
  2668. &SenseDataLength,
  2669. &HostAdapterStatus,
  2670. &TargetStatus,
  2671. (VOID *)CapacityData16,
  2672. &DataLength16,
  2673. FALSE
  2674. );
  2675. }
  2676. //
  2677. // no need to check HostAdapterStatus and TargetStatus
  2678. //
  2679. if (CommandStatus == EFI_SUCCESS) {
  2680. GetMediaInfo (ScsiDiskDevice, CapacityData10, CapacityData16);
  2681. FreeAlignedBuffer (CapacityData10, sizeof (EFI_SCSI_DISK_CAPACITY_DATA));
  2682. FreeAlignedBuffer (CapacityData16, sizeof (EFI_SCSI_DISK_CAPACITY_DATA16));
  2683. return EFI_SUCCESS;
  2684. }
  2685. FreeAlignedBuffer (CapacityData10, sizeof (EFI_SCSI_DISK_CAPACITY_DATA));
  2686. FreeAlignedBuffer (CapacityData16, sizeof (EFI_SCSI_DISK_CAPACITY_DATA16));
  2687. if (CommandStatus == EFI_NOT_READY) {
  2688. *NeedRetry = TRUE;
  2689. return EFI_DEVICE_ERROR;
  2690. } else if ((CommandStatus == EFI_INVALID_PARAMETER) || (CommandStatus == EFI_UNSUPPORTED)) {
  2691. *NeedRetry = FALSE;
  2692. return EFI_DEVICE_ERROR;
  2693. }
  2694. //
  2695. // go ahead to check HostAdapterStatus and TargetStatus
  2696. // (EFI_TIMEOUT, EFI_DEVICE_ERROR, EFI_WARN_BUFFER_TOO_SMALL)
  2697. //
  2698. Status = CheckHostAdapterStatus (HostAdapterStatus);
  2699. if ((Status == EFI_TIMEOUT) || (Status == EFI_NOT_READY)) {
  2700. *NeedRetry = TRUE;
  2701. return EFI_DEVICE_ERROR;
  2702. } else if (Status == EFI_DEVICE_ERROR) {
  2703. //
  2704. // reset the scsi channel
  2705. //
  2706. ScsiDiskDevice->ScsiIo->ResetBus (ScsiDiskDevice->ScsiIo);
  2707. *NeedRetry = FALSE;
  2708. return EFI_DEVICE_ERROR;
  2709. }
  2710. Status = CheckTargetStatus (TargetStatus);
  2711. if (Status == EFI_NOT_READY) {
  2712. //
  2713. // reset the scsi device
  2714. //
  2715. ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo);
  2716. *NeedRetry = TRUE;
  2717. return EFI_DEVICE_ERROR;
  2718. } else if (Status == EFI_DEVICE_ERROR) {
  2719. *NeedRetry = FALSE;
  2720. return EFI_DEVICE_ERROR;
  2721. }
  2722. //
  2723. // if goes here, meant ScsiReadCapacityCommand() failed.
  2724. // if ScsiDiskRequestSenseKeys() succeeds at last,
  2725. // better retry ScsiReadCapacityCommand(). (by setting *NeedRetry = TRUE)
  2726. //
  2727. MaxRetry = 3;
  2728. for (Index = 0; Index < MaxRetry; Index++) {
  2729. Status = ScsiDiskRequestSenseKeys (
  2730. ScsiDiskDevice,
  2731. NeedRetry,
  2732. SenseDataArray,
  2733. NumberOfSenseKeys,
  2734. TRUE
  2735. );
  2736. if (!EFI_ERROR (Status)) {
  2737. return EFI_SUCCESS;
  2738. }
  2739. if (!*NeedRetry) {
  2740. return EFI_DEVICE_ERROR;
  2741. }
  2742. }
  2743. //
  2744. // ScsiDiskRequestSenseKeys() failed after several rounds of retry.
  2745. // set *NeedRetry = FALSE to avoid the outside caller try again.
  2746. //
  2747. *NeedRetry = FALSE;
  2748. return EFI_DEVICE_ERROR;
  2749. }
  2750. /**
  2751. Check the HostAdapter status and re-interpret it in EFI_STATUS.
  2752. @param HostAdapterStatus Host Adapter status
  2753. @retval EFI_SUCCESS Host adapter is OK.
  2754. @retval EFI_TIMEOUT Timeout.
  2755. @retval EFI_NOT_READY Adapter NOT ready.
  2756. @retval EFI_DEVICE_ERROR Adapter device error.
  2757. **/
  2758. EFI_STATUS
  2759. CheckHostAdapterStatus (
  2760. IN UINT8 HostAdapterStatus
  2761. )
  2762. {
  2763. switch (HostAdapterStatus) {
  2764. case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_OK:
  2765. return EFI_SUCCESS;
  2766. case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_SELECTION_TIMEOUT:
  2767. case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_TIMEOUT:
  2768. case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_TIMEOUT_COMMAND:
  2769. return EFI_TIMEOUT;
  2770. case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_MESSAGE_REJECT:
  2771. case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_PARITY_ERROR:
  2772. case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_REQUEST_SENSE_FAILED:
  2773. case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_DATA_OVERRUN_UNDERRUN:
  2774. case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_BUS_RESET:
  2775. return EFI_NOT_READY;
  2776. case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_BUS_FREE:
  2777. case EFI_EXT_SCSI_STATUS_HOST_ADAPTER_PHASE_ERROR:
  2778. return EFI_DEVICE_ERROR;
  2779. default:
  2780. return EFI_SUCCESS;
  2781. }
  2782. }
  2783. /**
  2784. Check the target status and re-interpret it in EFI_STATUS.
  2785. @param TargetStatus Target status
  2786. @retval EFI_NOT_READY Device is NOT ready.
  2787. @retval EFI_DEVICE_ERROR
  2788. @retval EFI_SUCCESS
  2789. **/
  2790. EFI_STATUS
  2791. CheckTargetStatus (
  2792. IN UINT8 TargetStatus
  2793. )
  2794. {
  2795. switch (TargetStatus) {
  2796. case EFI_EXT_SCSI_STATUS_TARGET_GOOD:
  2797. case EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION:
  2798. case EFI_EXT_SCSI_STATUS_TARGET_CONDITION_MET:
  2799. return EFI_SUCCESS;
  2800. case EFI_EXT_SCSI_STATUS_TARGET_INTERMEDIATE:
  2801. case EFI_EXT_SCSI_STATUS_TARGET_INTERMEDIATE_CONDITION_MET:
  2802. case EFI_EXT_SCSI_STATUS_TARGET_BUSY:
  2803. case EFI_EXT_SCSI_STATUS_TARGET_TASK_SET_FULL:
  2804. return EFI_NOT_READY;
  2805. case EFI_EXT_SCSI_STATUS_TARGET_RESERVATION_CONFLICT:
  2806. return EFI_DEVICE_ERROR;
  2807. default:
  2808. return EFI_SUCCESS;
  2809. }
  2810. }
  2811. /**
  2812. Retrieve all sense keys from the device.
  2813. When encountering error during the process, if retrieve sense keys before
  2814. error encountered, it returns the sense keys with return status set to EFI_SUCCESS,
  2815. and NeedRetry set to FALSE; otherwise, return the proper return status.
  2816. @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
  2817. @param NeedRetry The pointer of flag indicates if need a retry
  2818. @param SenseDataArray The pointer of an array of sense data
  2819. @param NumberOfSenseKeys The number of sense key
  2820. @param AskResetIfError The flag indicates if need reset when error occurs
  2821. @retval EFI_DEVICE_ERROR Indicates that error occurs
  2822. @retval EFI_SUCCESS Successfully to request sense key
  2823. **/
  2824. EFI_STATUS
  2825. ScsiDiskRequestSenseKeys (
  2826. IN OUT SCSI_DISK_DEV *ScsiDiskDevice,
  2827. OUT BOOLEAN *NeedRetry,
  2828. OUT EFI_SCSI_SENSE_DATA **SenseDataArray,
  2829. OUT UINTN *NumberOfSenseKeys,
  2830. IN BOOLEAN AskResetIfError
  2831. )
  2832. {
  2833. EFI_SCSI_SENSE_DATA *PtrSenseData;
  2834. UINT8 SenseDataLength;
  2835. BOOLEAN SenseReq;
  2836. EFI_STATUS Status;
  2837. EFI_STATUS FallStatus;
  2838. UINT8 HostAdapterStatus;
  2839. UINT8 TargetStatus;
  2840. FallStatus = EFI_SUCCESS;
  2841. SenseDataLength = (UINT8)sizeof (EFI_SCSI_SENSE_DATA);
  2842. ZeroMem (
  2843. ScsiDiskDevice->SenseData,
  2844. sizeof (EFI_SCSI_SENSE_DATA) * (ScsiDiskDevice->SenseDataNumber)
  2845. );
  2846. *NumberOfSenseKeys = 0;
  2847. *SenseDataArray = ScsiDiskDevice->SenseData;
  2848. Status = EFI_SUCCESS;
  2849. PtrSenseData = AllocateAlignedBuffer (ScsiDiskDevice, sizeof (EFI_SCSI_SENSE_DATA));
  2850. if (PtrSenseData == NULL) {
  2851. return EFI_DEVICE_ERROR;
  2852. }
  2853. for (SenseReq = TRUE; SenseReq;) {
  2854. ZeroMem (PtrSenseData, sizeof (EFI_SCSI_SENSE_DATA));
  2855. Status = ScsiRequestSenseCommand (
  2856. ScsiDiskDevice->ScsiIo,
  2857. SCSI_DISK_TIMEOUT,
  2858. PtrSenseData,
  2859. &SenseDataLength,
  2860. &HostAdapterStatus,
  2861. &TargetStatus
  2862. );
  2863. if ((Status == EFI_SUCCESS) || (Status == EFI_WARN_BUFFER_TOO_SMALL)) {
  2864. FallStatus = EFI_SUCCESS;
  2865. } else if ((Status == EFI_TIMEOUT) || (Status == EFI_NOT_READY)) {
  2866. *NeedRetry = TRUE;
  2867. FallStatus = EFI_DEVICE_ERROR;
  2868. } else if ((Status == EFI_INVALID_PARAMETER) || (Status == EFI_UNSUPPORTED)) {
  2869. *NeedRetry = FALSE;
  2870. FallStatus = EFI_DEVICE_ERROR;
  2871. } else if (Status == EFI_DEVICE_ERROR) {
  2872. if (AskResetIfError) {
  2873. ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo);
  2874. }
  2875. FallStatus = EFI_DEVICE_ERROR;
  2876. }
  2877. if (EFI_ERROR (FallStatus)) {
  2878. if (*NumberOfSenseKeys != 0) {
  2879. *NeedRetry = FALSE;
  2880. Status = EFI_SUCCESS;
  2881. goto EXIT;
  2882. } else {
  2883. Status = EFI_DEVICE_ERROR;
  2884. goto EXIT;
  2885. }
  2886. }
  2887. CopyMem (ScsiDiskDevice->SenseData + *NumberOfSenseKeys, PtrSenseData, SenseDataLength);
  2888. (*NumberOfSenseKeys) += 1;
  2889. //
  2890. // no more sense key or number of sense keys exceeds predefined,
  2891. // skip the loop.
  2892. //
  2893. if ((PtrSenseData->Sense_Key == EFI_SCSI_SK_NO_SENSE) ||
  2894. (*NumberOfSenseKeys == ScsiDiskDevice->SenseDataNumber))
  2895. {
  2896. SenseReq = FALSE;
  2897. }
  2898. }
  2899. EXIT:
  2900. FreeAlignedBuffer (PtrSenseData, sizeof (EFI_SCSI_SENSE_DATA));
  2901. return Status;
  2902. }
  2903. /**
  2904. Get information from media read capacity command.
  2905. @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
  2906. @param Capacity10 The pointer of EFI_SCSI_DISK_CAPACITY_DATA
  2907. @param Capacity16 The pointer of EFI_SCSI_DISK_CAPACITY_DATA16
  2908. **/
  2909. VOID
  2910. GetMediaInfo (
  2911. IN OUT SCSI_DISK_DEV *ScsiDiskDevice,
  2912. IN EFI_SCSI_DISK_CAPACITY_DATA *Capacity10,
  2913. IN EFI_SCSI_DISK_CAPACITY_DATA16 *Capacity16
  2914. )
  2915. {
  2916. UINT8 *Ptr;
  2917. if (!ScsiDiskDevice->Cdb16Byte) {
  2918. ScsiDiskDevice->BlkIo.Media->LastBlock = ((UINT32)Capacity10->LastLba3 << 24) |
  2919. (Capacity10->LastLba2 << 16) |
  2920. (Capacity10->LastLba1 << 8) |
  2921. Capacity10->LastLba0;
  2922. ScsiDiskDevice->BlkIo.Media->BlockSize = (Capacity10->BlockSize3 << 24) |
  2923. (Capacity10->BlockSize2 << 16) |
  2924. (Capacity10->BlockSize1 << 8) |
  2925. Capacity10->BlockSize0;
  2926. ScsiDiskDevice->BlkIo.Media->LowestAlignedLba = 0;
  2927. ScsiDiskDevice->BlkIo.Media->LogicalBlocksPerPhysicalBlock = 0;
  2928. if (!ScsiDiskDevice->BlockLimitsVpdSupported) {
  2929. ScsiDiskDevice->UnmapInfo.MaxLbaCnt = (UINT32)ScsiDiskDevice->BlkIo.Media->LastBlock;
  2930. }
  2931. } else {
  2932. Ptr = (UINT8 *)&ScsiDiskDevice->BlkIo.Media->LastBlock;
  2933. *Ptr++ = Capacity16->LastLba0;
  2934. *Ptr++ = Capacity16->LastLba1;
  2935. *Ptr++ = Capacity16->LastLba2;
  2936. *Ptr++ = Capacity16->LastLba3;
  2937. *Ptr++ = Capacity16->LastLba4;
  2938. *Ptr++ = Capacity16->LastLba5;
  2939. *Ptr++ = Capacity16->LastLba6;
  2940. *Ptr = Capacity16->LastLba7;
  2941. ScsiDiskDevice->BlkIo.Media->BlockSize = (Capacity16->BlockSize3 << 24) |
  2942. (Capacity16->BlockSize2 << 16) |
  2943. (Capacity16->BlockSize1 << 8) |
  2944. Capacity16->BlockSize0;
  2945. ScsiDiskDevice->BlkIo.Media->LowestAlignedLba = (Capacity16->LowestAlignLogic2 << 8) |
  2946. Capacity16->LowestAlignLogic1;
  2947. ScsiDiskDevice->BlkIo.Media->LogicalBlocksPerPhysicalBlock = (1 << Capacity16->LogicPerPhysical);
  2948. if (!ScsiDiskDevice->BlockLimitsVpdSupported) {
  2949. if (ScsiDiskDevice->BlkIo.Media->LastBlock > (UINT32)-1) {
  2950. ScsiDiskDevice->UnmapInfo.MaxLbaCnt = (UINT32)-1;
  2951. } else {
  2952. ScsiDiskDevice->UnmapInfo.MaxLbaCnt = (UINT32)ScsiDiskDevice->BlkIo.Media->LastBlock;
  2953. }
  2954. }
  2955. }
  2956. ScsiDiskDevice->BlkIo.Media->MediaPresent = TRUE;
  2957. }
  2958. /**
  2959. Parse Inquiry data.
  2960. @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
  2961. **/
  2962. VOID
  2963. ParseInquiryData (
  2964. IN OUT SCSI_DISK_DEV *ScsiDiskDevice
  2965. )
  2966. {
  2967. ScsiDiskDevice->FixedDevice = (BOOLEAN)((ScsiDiskDevice->InquiryData.Rmb == 1) ? 0 : 1);
  2968. ScsiDiskDevice->BlkIoMedia.RemovableMedia = (BOOLEAN)(!ScsiDiskDevice->FixedDevice);
  2969. }
  2970. /**
  2971. Read sector from SCSI Disk.
  2972. @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
  2973. @param Buffer The buffer to fill in the read out data
  2974. @param Lba Logic block address
  2975. @param NumberOfBlocks The number of blocks to read
  2976. @retval EFI_DEVICE_ERROR Indicates a device error.
  2977. @retval EFI_SUCCESS Operation is successful.
  2978. **/
  2979. EFI_STATUS
  2980. ScsiDiskReadSectors (
  2981. IN SCSI_DISK_DEV *ScsiDiskDevice,
  2982. OUT VOID *Buffer,
  2983. IN EFI_LBA Lba,
  2984. IN UINTN NumberOfBlocks
  2985. )
  2986. {
  2987. UINTN BlocksRemaining;
  2988. UINT8 *PtrBuffer;
  2989. UINT32 BlockSize;
  2990. UINT32 ByteCount;
  2991. UINT32 MaxBlock;
  2992. UINT32 SectorCount;
  2993. UINT32 NextSectorCount;
  2994. UINT64 Timeout;
  2995. EFI_STATUS Status;
  2996. UINT8 Index;
  2997. UINT8 MaxRetry;
  2998. BOOLEAN NeedRetry;
  2999. Status = EFI_SUCCESS;
  3000. BlocksRemaining = NumberOfBlocks;
  3001. BlockSize = ScsiDiskDevice->BlkIo.Media->BlockSize;
  3002. //
  3003. // limit the data bytes that can be transferred by one Read(10) or Read(16) Command
  3004. //
  3005. if (!ScsiDiskDevice->Cdb16Byte) {
  3006. MaxBlock = 0xFFFF;
  3007. } else {
  3008. MaxBlock = 0xFFFFFFFF;
  3009. }
  3010. PtrBuffer = Buffer;
  3011. while (BlocksRemaining > 0) {
  3012. if (BlocksRemaining <= MaxBlock) {
  3013. if (!ScsiDiskDevice->Cdb16Byte) {
  3014. SectorCount = (UINT16)BlocksRemaining;
  3015. } else {
  3016. SectorCount = (UINT32)BlocksRemaining;
  3017. }
  3018. } else {
  3019. SectorCount = MaxBlock;
  3020. }
  3021. ByteCount = SectorCount * BlockSize;
  3022. //
  3023. // |------------------------|-----------------|------------------|-----------------|
  3024. // | ATA Transfer Mode | Transfer Rate | SCSI Interface | Transfer Rate |
  3025. // |------------------------|-----------------|------------------|-----------------|
  3026. // | PIO Mode 0 | 3.3Mbytes/sec | SCSI-1 | 5Mbytes/sec |
  3027. // |------------------------|-----------------|------------------|-----------------|
  3028. // | PIO Mode 1 | 5.2Mbytes/sec | Fast SCSI | 10Mbytes/sec |
  3029. // |------------------------|-----------------|------------------|-----------------|
  3030. // | PIO Mode 2 | 8.3Mbytes/sec | Fast-Wide SCSI | 20Mbytes/sec |
  3031. // |------------------------|-----------------|------------------|-----------------|
  3032. // | PIO Mode 3 | 11.1Mbytes/sec | Ultra SCSI | 20Mbytes/sec |
  3033. // |------------------------|-----------------|------------------|-----------------|
  3034. // | PIO Mode 4 | 16.6Mbytes/sec | Ultra Wide SCSI | 40Mbytes/sec |
  3035. // |------------------------|-----------------|------------------|-----------------|
  3036. // | Single-word DMA Mode 0 | 2.1Mbytes/sec | Ultra2 SCSI | 40Mbytes/sec |
  3037. // |------------------------|-----------------|------------------|-----------------|
  3038. // | Single-word DMA Mode 1 | 4.2Mbytes/sec | Ultra2 Wide SCSI | 80Mbytes/sec |
  3039. // |------------------------|-----------------|------------------|-----------------|
  3040. // | Single-word DMA Mode 2 | 8.4Mbytes/sec | Ultra3 SCSI | 160Mbytes/sec |
  3041. // |------------------------|-----------------|------------------|-----------------|
  3042. // | Multi-word DMA Mode 0 | 4.2Mbytes/sec | Ultra-320 SCSI | 320Mbytes/sec |
  3043. // |------------------------|-----------------|------------------|-----------------|
  3044. // | Multi-word DMA Mode 1 | 13.3Mbytes/sec | Ultra-640 SCSI | 640Mbytes/sec |
  3045. // |------------------------|-----------------|------------------|-----------------|
  3046. //
  3047. // As ScsiDisk and ScsiBus driver are used to manage SCSI or ATAPI devices, we have to use
  3048. // the lowest transfer rate to calculate the possible maximum timeout value for each operation.
  3049. // From the above table, we could know 2.1Mbytes per second is lowest one.
  3050. // The timeout value is rounded up to nearest integer and here an additional 30s is added
  3051. // to follow ATA spec in which it mentioned that the device may take up to 30s to respond
  3052. // commands in the Standby/Idle mode.
  3053. //
  3054. Timeout = EFI_TIMER_PERIOD_SECONDS (ByteCount / 2100000 + 31);
  3055. MaxRetry = 2;
  3056. for (Index = 0; Index < MaxRetry; Index++) {
  3057. if (!ScsiDiskDevice->Cdb16Byte) {
  3058. Status = ScsiDiskRead10 (
  3059. ScsiDiskDevice,
  3060. &NeedRetry,
  3061. Timeout,
  3062. PtrBuffer,
  3063. &ByteCount,
  3064. (UINT32)Lba,
  3065. SectorCount
  3066. );
  3067. } else {
  3068. Status = ScsiDiskRead16 (
  3069. ScsiDiskDevice,
  3070. &NeedRetry,
  3071. Timeout,
  3072. PtrBuffer,
  3073. &ByteCount,
  3074. Lba,
  3075. SectorCount
  3076. );
  3077. }
  3078. if (!EFI_ERROR (Status)) {
  3079. break;
  3080. }
  3081. if (!NeedRetry) {
  3082. return EFI_DEVICE_ERROR;
  3083. }
  3084. //
  3085. // We need to retry. However, if ScsiDiskRead10() or ScsiDiskRead16() has
  3086. // lowered ByteCount on output, we must make sure that we lower
  3087. // SectorCount accordingly. SectorCount will be encoded in the CDB, and
  3088. // it is invalid to request more sectors in the CDB than the entire
  3089. // transfer (ie. ByteCount) can carry.
  3090. //
  3091. // In addition, ByteCount is only expected to go down, or stay unchanged.
  3092. // Therefore we don't need to update Timeout: the original timeout should
  3093. // accommodate shorter transfers too.
  3094. //
  3095. NextSectorCount = ByteCount / BlockSize;
  3096. if (NextSectorCount < SectorCount) {
  3097. SectorCount = NextSectorCount;
  3098. //
  3099. // Account for any rounding down.
  3100. //
  3101. ByteCount = SectorCount * BlockSize;
  3102. }
  3103. }
  3104. if ((Index == MaxRetry) && (Status != EFI_SUCCESS)) {
  3105. return EFI_DEVICE_ERROR;
  3106. }
  3107. //
  3108. // actual transferred sectors
  3109. //
  3110. SectorCount = ByteCount / BlockSize;
  3111. Lba += SectorCount;
  3112. PtrBuffer = PtrBuffer + SectorCount * BlockSize;
  3113. BlocksRemaining -= SectorCount;
  3114. }
  3115. return EFI_SUCCESS;
  3116. }
  3117. /**
  3118. Write sector to SCSI Disk.
  3119. @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
  3120. @param Buffer The buffer of data to be written into SCSI Disk
  3121. @param Lba Logic block address
  3122. @param NumberOfBlocks The number of blocks to read
  3123. @retval EFI_DEVICE_ERROR Indicates a device error.
  3124. @retval EFI_SUCCESS Operation is successful.
  3125. **/
  3126. EFI_STATUS
  3127. ScsiDiskWriteSectors (
  3128. IN SCSI_DISK_DEV *ScsiDiskDevice,
  3129. IN VOID *Buffer,
  3130. IN EFI_LBA Lba,
  3131. IN UINTN NumberOfBlocks
  3132. )
  3133. {
  3134. UINTN BlocksRemaining;
  3135. UINT8 *PtrBuffer;
  3136. UINT32 BlockSize;
  3137. UINT32 ByteCount;
  3138. UINT32 MaxBlock;
  3139. UINT32 SectorCount;
  3140. UINT32 NextSectorCount;
  3141. UINT64 Timeout;
  3142. EFI_STATUS Status;
  3143. UINT8 Index;
  3144. UINT8 MaxRetry;
  3145. BOOLEAN NeedRetry;
  3146. Status = EFI_SUCCESS;
  3147. BlocksRemaining = NumberOfBlocks;
  3148. BlockSize = ScsiDiskDevice->BlkIo.Media->BlockSize;
  3149. //
  3150. // limit the data bytes that can be transferred by one Read(10) or Read(16) Command
  3151. //
  3152. if (!ScsiDiskDevice->Cdb16Byte) {
  3153. MaxBlock = 0xFFFF;
  3154. } else {
  3155. MaxBlock = 0xFFFFFFFF;
  3156. }
  3157. PtrBuffer = Buffer;
  3158. while (BlocksRemaining > 0) {
  3159. if (BlocksRemaining <= MaxBlock) {
  3160. if (!ScsiDiskDevice->Cdb16Byte) {
  3161. SectorCount = (UINT16)BlocksRemaining;
  3162. } else {
  3163. SectorCount = (UINT32)BlocksRemaining;
  3164. }
  3165. } else {
  3166. SectorCount = MaxBlock;
  3167. }
  3168. ByteCount = SectorCount * BlockSize;
  3169. //
  3170. // |------------------------|-----------------|------------------|-----------------|
  3171. // | ATA Transfer Mode | Transfer Rate | SCSI Interface | Transfer Rate |
  3172. // |------------------------|-----------------|------------------|-----------------|
  3173. // | PIO Mode 0 | 3.3Mbytes/sec | SCSI-1 | 5Mbytes/sec |
  3174. // |------------------------|-----------------|------------------|-----------------|
  3175. // | PIO Mode 1 | 5.2Mbytes/sec | Fast SCSI | 10Mbytes/sec |
  3176. // |------------------------|-----------------|------------------|-----------------|
  3177. // | PIO Mode 2 | 8.3Mbytes/sec | Fast-Wide SCSI | 20Mbytes/sec |
  3178. // |------------------------|-----------------|------------------|-----------------|
  3179. // | PIO Mode 3 | 11.1Mbytes/sec | Ultra SCSI | 20Mbytes/sec |
  3180. // |------------------------|-----------------|------------------|-----------------|
  3181. // | PIO Mode 4 | 16.6Mbytes/sec | Ultra Wide SCSI | 40Mbytes/sec |
  3182. // |------------------------|-----------------|------------------|-----------------|
  3183. // | Single-word DMA Mode 0 | 2.1Mbytes/sec | Ultra2 SCSI | 40Mbytes/sec |
  3184. // |------------------------|-----------------|------------------|-----------------|
  3185. // | Single-word DMA Mode 1 | 4.2Mbytes/sec | Ultra2 Wide SCSI | 80Mbytes/sec |
  3186. // |------------------------|-----------------|------------------|-----------------|
  3187. // | Single-word DMA Mode 2 | 8.4Mbytes/sec | Ultra3 SCSI | 160Mbytes/sec |
  3188. // |------------------------|-----------------|------------------|-----------------|
  3189. // | Multi-word DMA Mode 0 | 4.2Mbytes/sec | Ultra-320 SCSI | 320Mbytes/sec |
  3190. // |------------------------|-----------------|------------------|-----------------|
  3191. // | Multi-word DMA Mode 1 | 13.3Mbytes/sec | Ultra-640 SCSI | 640Mbytes/sec |
  3192. // |------------------------|-----------------|------------------|-----------------|
  3193. //
  3194. // As ScsiDisk and ScsiBus driver are used to manage SCSI or ATAPI devices, we have to use
  3195. // the lowest transfer rate to calculate the possible maximum timeout value for each operation.
  3196. // From the above table, we could know 2.1Mbytes per second is lowest one.
  3197. // The timeout value is rounded up to nearest integer and here an additional 30s is added
  3198. // to follow ATA spec in which it mentioned that the device may take up to 30s to respond
  3199. // commands in the Standby/Idle mode.
  3200. //
  3201. Timeout = EFI_TIMER_PERIOD_SECONDS (ByteCount / 2100000 + 31);
  3202. MaxRetry = 2;
  3203. for (Index = 0; Index < MaxRetry; Index++) {
  3204. if (!ScsiDiskDevice->Cdb16Byte) {
  3205. Status = ScsiDiskWrite10 (
  3206. ScsiDiskDevice,
  3207. &NeedRetry,
  3208. Timeout,
  3209. PtrBuffer,
  3210. &ByteCount,
  3211. (UINT32)Lba,
  3212. SectorCount
  3213. );
  3214. } else {
  3215. Status = ScsiDiskWrite16 (
  3216. ScsiDiskDevice,
  3217. &NeedRetry,
  3218. Timeout,
  3219. PtrBuffer,
  3220. &ByteCount,
  3221. Lba,
  3222. SectorCount
  3223. );
  3224. }
  3225. if (!EFI_ERROR (Status)) {
  3226. break;
  3227. }
  3228. if (!NeedRetry) {
  3229. return EFI_DEVICE_ERROR;
  3230. }
  3231. //
  3232. // We need to retry. However, if ScsiDiskWrite10() or ScsiDiskWrite16()
  3233. // has lowered ByteCount on output, we must make sure that we lower
  3234. // SectorCount accordingly. SectorCount will be encoded in the CDB, and
  3235. // it is invalid to request more sectors in the CDB than the entire
  3236. // transfer (ie. ByteCount) can carry.
  3237. //
  3238. // In addition, ByteCount is only expected to go down, or stay unchanged.
  3239. // Therefore we don't need to update Timeout: the original timeout should
  3240. // accommodate shorter transfers too.
  3241. //
  3242. NextSectorCount = ByteCount / BlockSize;
  3243. if (NextSectorCount < SectorCount) {
  3244. SectorCount = NextSectorCount;
  3245. //
  3246. // Account for any rounding down.
  3247. //
  3248. ByteCount = SectorCount * BlockSize;
  3249. }
  3250. }
  3251. if ((Index == MaxRetry) && (Status != EFI_SUCCESS)) {
  3252. return EFI_DEVICE_ERROR;
  3253. }
  3254. //
  3255. // actual transferred sectors
  3256. //
  3257. SectorCount = ByteCount / BlockSize;
  3258. Lba += SectorCount;
  3259. PtrBuffer = PtrBuffer + SectorCount * BlockSize;
  3260. BlocksRemaining -= SectorCount;
  3261. }
  3262. return EFI_SUCCESS;
  3263. }
  3264. /**
  3265. Asynchronously read sector from SCSI Disk.
  3266. @param ScsiDiskDevice The pointer of SCSI_DISK_DEV.
  3267. @param Buffer The buffer to fill in the read out data.
  3268. @param Lba Logic block address.
  3269. @param NumberOfBlocks The number of blocks to read.
  3270. @param Token A pointer to the token associated with the
  3271. non-blocking read request.
  3272. @retval EFI_INVALID_PARAMETER Token is NULL or Token->Event is NULL.
  3273. @retval EFI_DEVICE_ERROR Indicates a device error.
  3274. @retval EFI_SUCCESS Operation is successful.
  3275. **/
  3276. EFI_STATUS
  3277. ScsiDiskAsyncReadSectors (
  3278. IN SCSI_DISK_DEV *ScsiDiskDevice,
  3279. OUT VOID *Buffer,
  3280. IN EFI_LBA Lba,
  3281. IN UINTN NumberOfBlocks,
  3282. IN EFI_BLOCK_IO2_TOKEN *Token
  3283. )
  3284. {
  3285. UINTN BlocksRemaining;
  3286. UINT8 *PtrBuffer;
  3287. UINT32 BlockSize;
  3288. UINT32 ByteCount;
  3289. UINT32 MaxBlock;
  3290. UINT32 SectorCount;
  3291. UINT64 Timeout;
  3292. SCSI_BLKIO2_REQUEST *BlkIo2Req;
  3293. EFI_STATUS Status;
  3294. EFI_TPL OldTpl;
  3295. if ((Token == NULL) || (Token->Event == NULL)) {
  3296. return EFI_INVALID_PARAMETER;
  3297. }
  3298. BlkIo2Req = AllocateZeroPool (sizeof (SCSI_BLKIO2_REQUEST));
  3299. if (BlkIo2Req == NULL) {
  3300. return EFI_OUT_OF_RESOURCES;
  3301. }
  3302. BlkIo2Req->Token = Token;
  3303. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  3304. InsertTailList (&ScsiDiskDevice->AsyncTaskQueue, &BlkIo2Req->Link);
  3305. gBS->RestoreTPL (OldTpl);
  3306. InitializeListHead (&BlkIo2Req->ScsiRWQueue);
  3307. Status = EFI_SUCCESS;
  3308. BlocksRemaining = NumberOfBlocks;
  3309. BlockSize = ScsiDiskDevice->BlkIo.Media->BlockSize;
  3310. //
  3311. // Limit the data bytes that can be transferred by one Read(10) or Read(16)
  3312. // Command
  3313. //
  3314. if (!ScsiDiskDevice->Cdb16Byte) {
  3315. MaxBlock = 0xFFFF;
  3316. } else {
  3317. MaxBlock = 0xFFFFFFFF;
  3318. }
  3319. PtrBuffer = Buffer;
  3320. while (BlocksRemaining > 0) {
  3321. if (BlocksRemaining <= MaxBlock) {
  3322. if (!ScsiDiskDevice->Cdb16Byte) {
  3323. SectorCount = (UINT16)BlocksRemaining;
  3324. } else {
  3325. SectorCount = (UINT32)BlocksRemaining;
  3326. }
  3327. } else {
  3328. SectorCount = MaxBlock;
  3329. }
  3330. ByteCount = SectorCount * BlockSize;
  3331. //
  3332. // |------------------------|-----------------|------------------|-----------------|
  3333. // | ATA Transfer Mode | Transfer Rate | SCSI Interface | Transfer Rate |
  3334. // |------------------------|-----------------|------------------|-----------------|
  3335. // | PIO Mode 0 | 3.3Mbytes/sec | SCSI-1 | 5Mbytes/sec |
  3336. // |------------------------|-----------------|------------------|-----------------|
  3337. // | PIO Mode 1 | 5.2Mbytes/sec | Fast SCSI | 10Mbytes/sec |
  3338. // |------------------------|-----------------|------------------|-----------------|
  3339. // | PIO Mode 2 | 8.3Mbytes/sec | Fast-Wide SCSI | 20Mbytes/sec |
  3340. // |------------------------|-----------------|------------------|-----------------|
  3341. // | PIO Mode 3 | 11.1Mbytes/sec | Ultra SCSI | 20Mbytes/sec |
  3342. // |------------------------|-----------------|------------------|-----------------|
  3343. // | PIO Mode 4 | 16.6Mbytes/sec | Ultra Wide SCSI | 40Mbytes/sec |
  3344. // |------------------------|-----------------|------------------|-----------------|
  3345. // | Single-word DMA Mode 0 | 2.1Mbytes/sec | Ultra2 SCSI | 40Mbytes/sec |
  3346. // |------------------------|-----------------|------------------|-----------------|
  3347. // | Single-word DMA Mode 1 | 4.2Mbytes/sec | Ultra2 Wide SCSI | 80Mbytes/sec |
  3348. // |------------------------|-----------------|------------------|-----------------|
  3349. // | Single-word DMA Mode 2 | 8.4Mbytes/sec | Ultra3 SCSI | 160Mbytes/sec |
  3350. // |------------------------|-----------------|------------------|-----------------|
  3351. // | Multi-word DMA Mode 0 | 4.2Mbytes/sec | Ultra-320 SCSI | 320Mbytes/sec |
  3352. // |------------------------|-----------------|------------------|-----------------|
  3353. // | Multi-word DMA Mode 1 | 13.3Mbytes/sec | Ultra-640 SCSI | 640Mbytes/sec |
  3354. // |------------------------|-----------------|------------------|-----------------|
  3355. //
  3356. // As ScsiDisk and ScsiBus driver are used to manage SCSI or ATAPI devices,
  3357. // we have to use the lowest transfer rate to calculate the possible
  3358. // maximum timeout value for each operation.
  3359. // From the above table, we could know 2.1Mbytes per second is lowest one.
  3360. // The timeout value is rounded up to nearest integer and here an additional
  3361. // 30s is added to follow ATA spec in which it mentioned that the device
  3362. // may take up to 30s to respond commands in the Standby/Idle mode.
  3363. //
  3364. Timeout = EFI_TIMER_PERIOD_SECONDS (ByteCount / 2100000 + 31);
  3365. if (!ScsiDiskDevice->Cdb16Byte) {
  3366. Status = ScsiDiskAsyncRead10 (
  3367. ScsiDiskDevice,
  3368. Timeout,
  3369. 0,
  3370. PtrBuffer,
  3371. ByteCount,
  3372. (UINT32)Lba,
  3373. SectorCount,
  3374. BlkIo2Req,
  3375. Token
  3376. );
  3377. } else {
  3378. Status = ScsiDiskAsyncRead16 (
  3379. ScsiDiskDevice,
  3380. Timeout,
  3381. 0,
  3382. PtrBuffer,
  3383. ByteCount,
  3384. Lba,
  3385. SectorCount,
  3386. BlkIo2Req,
  3387. Token
  3388. );
  3389. }
  3390. if (EFI_ERROR (Status)) {
  3391. //
  3392. // Some devices will return EFI_DEVICE_ERROR or EFI_TIMEOUT when the data
  3393. // length of a SCSI I/O command is too large.
  3394. // In this case, we retry sending the SCSI command with a data length
  3395. // half of its previous value.
  3396. //
  3397. if ((Status == EFI_DEVICE_ERROR) || (Status == EFI_TIMEOUT)) {
  3398. if ((MaxBlock > 1) && (SectorCount > 1)) {
  3399. MaxBlock = MIN (MaxBlock, SectorCount) >> 1;
  3400. continue;
  3401. }
  3402. }
  3403. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  3404. if (IsListEmpty (&BlkIo2Req->ScsiRWQueue)) {
  3405. //
  3406. // Free the SCSI_BLKIO2_REQUEST structure only when there is no other
  3407. // SCSI sub-task running. Otherwise, it will be freed in the callback
  3408. // function ScsiDiskNotify().
  3409. //
  3410. RemoveEntryList (&BlkIo2Req->Link);
  3411. FreePool (BlkIo2Req);
  3412. BlkIo2Req = NULL;
  3413. gBS->RestoreTPL (OldTpl);
  3414. //
  3415. // It is safe to return error status to the caller, since there is no
  3416. // previous SCSI sub-task executing.
  3417. //
  3418. Status = EFI_DEVICE_ERROR;
  3419. goto Done;
  3420. } else {
  3421. gBS->RestoreTPL (OldTpl);
  3422. //
  3423. // There are previous SCSI commands still running, EFI_SUCCESS should
  3424. // be returned to make sure that the caller does not free resources
  3425. // still using by these SCSI commands.
  3426. //
  3427. Status = EFI_SUCCESS;
  3428. goto Done;
  3429. }
  3430. }
  3431. //
  3432. // Sectors submitted for transfer
  3433. //
  3434. SectorCount = ByteCount / BlockSize;
  3435. Lba += SectorCount;
  3436. PtrBuffer = PtrBuffer + SectorCount * BlockSize;
  3437. BlocksRemaining -= SectorCount;
  3438. }
  3439. Status = EFI_SUCCESS;
  3440. Done:
  3441. if (BlkIo2Req != NULL) {
  3442. BlkIo2Req->LastScsiRW = TRUE;
  3443. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  3444. if (IsListEmpty (&BlkIo2Req->ScsiRWQueue)) {
  3445. RemoveEntryList (&BlkIo2Req->Link);
  3446. FreePool (BlkIo2Req);
  3447. BlkIo2Req = NULL;
  3448. gBS->SignalEvent (Token->Event);
  3449. }
  3450. gBS->RestoreTPL (OldTpl);
  3451. }
  3452. return Status;
  3453. }
  3454. /**
  3455. Asynchronously write sector to SCSI Disk.
  3456. @param ScsiDiskDevice The pointer of SCSI_DISK_DEV.
  3457. @param Buffer The buffer of data to be written into SCSI Disk.
  3458. @param Lba Logic block address.
  3459. @param NumberOfBlocks The number of blocks to read.
  3460. @param Token A pointer to the token associated with the
  3461. non-blocking read request.
  3462. @retval EFI_INVALID_PARAMETER Token is NULL or Token->Event is NULL
  3463. @retval EFI_DEVICE_ERROR Indicates a device error.
  3464. @retval EFI_SUCCESS Operation is successful.
  3465. **/
  3466. EFI_STATUS
  3467. ScsiDiskAsyncWriteSectors (
  3468. IN SCSI_DISK_DEV *ScsiDiskDevice,
  3469. IN VOID *Buffer,
  3470. IN EFI_LBA Lba,
  3471. IN UINTN NumberOfBlocks,
  3472. IN EFI_BLOCK_IO2_TOKEN *Token
  3473. )
  3474. {
  3475. UINTN BlocksRemaining;
  3476. UINT8 *PtrBuffer;
  3477. UINT32 BlockSize;
  3478. UINT32 ByteCount;
  3479. UINT32 MaxBlock;
  3480. UINT32 SectorCount;
  3481. UINT64 Timeout;
  3482. SCSI_BLKIO2_REQUEST *BlkIo2Req;
  3483. EFI_STATUS Status;
  3484. EFI_TPL OldTpl;
  3485. if ((Token == NULL) || (Token->Event == NULL)) {
  3486. return EFI_INVALID_PARAMETER;
  3487. }
  3488. BlkIo2Req = AllocateZeroPool (sizeof (SCSI_BLKIO2_REQUEST));
  3489. if (BlkIo2Req == NULL) {
  3490. return EFI_OUT_OF_RESOURCES;
  3491. }
  3492. BlkIo2Req->Token = Token;
  3493. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  3494. InsertTailList (&ScsiDiskDevice->AsyncTaskQueue, &BlkIo2Req->Link);
  3495. gBS->RestoreTPL (OldTpl);
  3496. InitializeListHead (&BlkIo2Req->ScsiRWQueue);
  3497. Status = EFI_SUCCESS;
  3498. BlocksRemaining = NumberOfBlocks;
  3499. BlockSize = ScsiDiskDevice->BlkIo.Media->BlockSize;
  3500. //
  3501. // Limit the data bytes that can be transferred by one Read(10) or Read(16)
  3502. // Command
  3503. //
  3504. if (!ScsiDiskDevice->Cdb16Byte) {
  3505. MaxBlock = 0xFFFF;
  3506. } else {
  3507. MaxBlock = 0xFFFFFFFF;
  3508. }
  3509. PtrBuffer = Buffer;
  3510. while (BlocksRemaining > 0) {
  3511. if (BlocksRemaining <= MaxBlock) {
  3512. if (!ScsiDiskDevice->Cdb16Byte) {
  3513. SectorCount = (UINT16)BlocksRemaining;
  3514. } else {
  3515. SectorCount = (UINT32)BlocksRemaining;
  3516. }
  3517. } else {
  3518. SectorCount = MaxBlock;
  3519. }
  3520. ByteCount = SectorCount * BlockSize;
  3521. //
  3522. // |------------------------|-----------------|------------------|-----------------|
  3523. // | ATA Transfer Mode | Transfer Rate | SCSI Interface | Transfer Rate |
  3524. // |------------------------|-----------------|------------------|-----------------|
  3525. // | PIO Mode 0 | 3.3Mbytes/sec | SCSI-1 | 5Mbytes/sec |
  3526. // |------------------------|-----------------|------------------|-----------------|
  3527. // | PIO Mode 1 | 5.2Mbytes/sec | Fast SCSI | 10Mbytes/sec |
  3528. // |------------------------|-----------------|------------------|-----------------|
  3529. // | PIO Mode 2 | 8.3Mbytes/sec | Fast-Wide SCSI | 20Mbytes/sec |
  3530. // |------------------------|-----------------|------------------|-----------------|
  3531. // | PIO Mode 3 | 11.1Mbytes/sec | Ultra SCSI | 20Mbytes/sec |
  3532. // |------------------------|-----------------|------------------|-----------------|
  3533. // | PIO Mode 4 | 16.6Mbytes/sec | Ultra Wide SCSI | 40Mbytes/sec |
  3534. // |------------------------|-----------------|------------------|-----------------|
  3535. // | Single-word DMA Mode 0 | 2.1Mbytes/sec | Ultra2 SCSI | 40Mbytes/sec |
  3536. // |------------------------|-----------------|------------------|-----------------|
  3537. // | Single-word DMA Mode 1 | 4.2Mbytes/sec | Ultra2 Wide SCSI | 80Mbytes/sec |
  3538. // |------------------------|-----------------|------------------|-----------------|
  3539. // | Single-word DMA Mode 2 | 8.4Mbytes/sec | Ultra3 SCSI | 160Mbytes/sec |
  3540. // |------------------------|-----------------|------------------|-----------------|
  3541. // | Multi-word DMA Mode 0 | 4.2Mbytes/sec | Ultra-320 SCSI | 320Mbytes/sec |
  3542. // |------------------------|-----------------|------------------|-----------------|
  3543. // | Multi-word DMA Mode 1 | 13.3Mbytes/sec | Ultra-640 SCSI | 640Mbytes/sec |
  3544. // |------------------------|-----------------|------------------|-----------------|
  3545. //
  3546. // As ScsiDisk and ScsiBus driver are used to manage SCSI or ATAPI devices,
  3547. // we have to use the lowest transfer rate to calculate the possible
  3548. // maximum timeout value for each operation.
  3549. // From the above table, we could know 2.1Mbytes per second is lowest one.
  3550. // The timeout value is rounded up to nearest integer and here an additional
  3551. // 30s is added to follow ATA spec in which it mentioned that the device
  3552. // may take up to 30s to respond commands in the Standby/Idle mode.
  3553. //
  3554. Timeout = EFI_TIMER_PERIOD_SECONDS (ByteCount / 2100000 + 31);
  3555. if (!ScsiDiskDevice->Cdb16Byte) {
  3556. Status = ScsiDiskAsyncWrite10 (
  3557. ScsiDiskDevice,
  3558. Timeout,
  3559. 0,
  3560. PtrBuffer,
  3561. ByteCount,
  3562. (UINT32)Lba,
  3563. SectorCount,
  3564. BlkIo2Req,
  3565. Token
  3566. );
  3567. } else {
  3568. Status = ScsiDiskAsyncWrite16 (
  3569. ScsiDiskDevice,
  3570. Timeout,
  3571. 0,
  3572. PtrBuffer,
  3573. ByteCount,
  3574. Lba,
  3575. SectorCount,
  3576. BlkIo2Req,
  3577. Token
  3578. );
  3579. }
  3580. if (EFI_ERROR (Status)) {
  3581. //
  3582. // Some devices will return EFI_DEVICE_ERROR or EFI_TIMEOUT when the data
  3583. // length of a SCSI I/O command is too large.
  3584. // In this case, we retry sending the SCSI command with a data length
  3585. // half of its previous value.
  3586. //
  3587. if ((Status == EFI_DEVICE_ERROR) || (Status == EFI_TIMEOUT)) {
  3588. if ((MaxBlock > 1) && (SectorCount > 1)) {
  3589. MaxBlock = MIN (MaxBlock, SectorCount) >> 1;
  3590. continue;
  3591. }
  3592. }
  3593. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  3594. if (IsListEmpty (&BlkIo2Req->ScsiRWQueue)) {
  3595. //
  3596. // Free the SCSI_BLKIO2_REQUEST structure only when there is no other
  3597. // SCSI sub-task running. Otherwise, it will be freed in the callback
  3598. // function ScsiDiskNotify().
  3599. //
  3600. RemoveEntryList (&BlkIo2Req->Link);
  3601. FreePool (BlkIo2Req);
  3602. BlkIo2Req = NULL;
  3603. gBS->RestoreTPL (OldTpl);
  3604. //
  3605. // It is safe to return error status to the caller, since there is no
  3606. // previous SCSI sub-task executing.
  3607. //
  3608. Status = EFI_DEVICE_ERROR;
  3609. goto Done;
  3610. } else {
  3611. gBS->RestoreTPL (OldTpl);
  3612. //
  3613. // There are previous SCSI commands still running, EFI_SUCCESS should
  3614. // be returned to make sure that the caller does not free resources
  3615. // still using by these SCSI commands.
  3616. //
  3617. Status = EFI_SUCCESS;
  3618. goto Done;
  3619. }
  3620. }
  3621. //
  3622. // Sectors submitted for transfer
  3623. //
  3624. SectorCount = ByteCount / BlockSize;
  3625. Lba += SectorCount;
  3626. PtrBuffer = PtrBuffer + SectorCount * BlockSize;
  3627. BlocksRemaining -= SectorCount;
  3628. }
  3629. Status = EFI_SUCCESS;
  3630. Done:
  3631. if (BlkIo2Req != NULL) {
  3632. BlkIo2Req->LastScsiRW = TRUE;
  3633. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  3634. if (IsListEmpty (&BlkIo2Req->ScsiRWQueue)) {
  3635. RemoveEntryList (&BlkIo2Req->Link);
  3636. FreePool (BlkIo2Req);
  3637. BlkIo2Req = NULL;
  3638. gBS->SignalEvent (Token->Event);
  3639. }
  3640. gBS->RestoreTPL (OldTpl);
  3641. }
  3642. return Status;
  3643. }
  3644. /**
  3645. Submit Read(10) command.
  3646. @param ScsiDiskDevice The pointer of ScsiDiskDevice
  3647. @param NeedRetry The pointer of flag indicates if needs retry if error happens
  3648. @param Timeout The time to complete the command
  3649. @param DataBuffer The buffer to fill with the read out data
  3650. @param DataLength The length of buffer
  3651. @param StartLba The start logic block address
  3652. @param SectorCount The number of blocks to read
  3653. @return EFI_STATUS is returned by calling ScsiRead10Command().
  3654. **/
  3655. EFI_STATUS
  3656. ScsiDiskRead10 (
  3657. IN SCSI_DISK_DEV *ScsiDiskDevice,
  3658. OUT BOOLEAN *NeedRetry,
  3659. IN UINT64 Timeout,
  3660. OUT UINT8 *DataBuffer,
  3661. IN OUT UINT32 *DataLength,
  3662. IN UINT32 StartLba,
  3663. IN UINT32 SectorCount
  3664. )
  3665. {
  3666. UINT8 SenseDataLength;
  3667. EFI_STATUS Status;
  3668. EFI_STATUS ReturnStatus;
  3669. UINT8 HostAdapterStatus;
  3670. UINT8 TargetStatus;
  3671. UINTN Action;
  3672. //
  3673. // Implement a backoff algorithm to resolve some compatibility issues that
  3674. // some SCSI targets or ATAPI devices couldn't correctly response reading/writing
  3675. // big data in a single operation.
  3676. // This algorithm will at first try to execute original request. If the request fails
  3677. // with media error sense data or else, it will reduce the transfer length to half and
  3678. // try again till the operation succeeds or fails with one sector transfer length.
  3679. //
  3680. BackOff:
  3681. *NeedRetry = FALSE;
  3682. Action = ACTION_NO_ACTION;
  3683. SenseDataLength = (UINT8)(ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA));
  3684. ReturnStatus = ScsiRead10Command (
  3685. ScsiDiskDevice->ScsiIo,
  3686. Timeout,
  3687. ScsiDiskDevice->SenseData,
  3688. &SenseDataLength,
  3689. &HostAdapterStatus,
  3690. &TargetStatus,
  3691. DataBuffer,
  3692. DataLength,
  3693. StartLba,
  3694. SectorCount
  3695. );
  3696. if ((ReturnStatus == EFI_NOT_READY) || (ReturnStatus == EFI_BAD_BUFFER_SIZE)) {
  3697. *NeedRetry = TRUE;
  3698. return EFI_DEVICE_ERROR;
  3699. } else if ((ReturnStatus == EFI_INVALID_PARAMETER) || (ReturnStatus == EFI_UNSUPPORTED)) {
  3700. *NeedRetry = FALSE;
  3701. return ReturnStatus;
  3702. }
  3703. //
  3704. // go ahead to check HostAdapterStatus and TargetStatus
  3705. // (EFI_TIMEOUT, EFI_DEVICE_ERROR, EFI_WARN_BUFFER_TOO_SMALL)
  3706. //
  3707. Status = CheckHostAdapterStatus (HostAdapterStatus);
  3708. if ((Status == EFI_TIMEOUT) || (Status == EFI_NOT_READY)) {
  3709. *NeedRetry = TRUE;
  3710. return EFI_DEVICE_ERROR;
  3711. } else if (Status == EFI_DEVICE_ERROR) {
  3712. //
  3713. // reset the scsi channel
  3714. //
  3715. ScsiDiskDevice->ScsiIo->ResetBus (ScsiDiskDevice->ScsiIo);
  3716. *NeedRetry = FALSE;
  3717. return EFI_DEVICE_ERROR;
  3718. }
  3719. Status = CheckTargetStatus (TargetStatus);
  3720. if (Status == EFI_NOT_READY) {
  3721. //
  3722. // reset the scsi device
  3723. //
  3724. ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo);
  3725. *NeedRetry = TRUE;
  3726. return EFI_DEVICE_ERROR;
  3727. } else if (Status == EFI_DEVICE_ERROR) {
  3728. *NeedRetry = FALSE;
  3729. return EFI_DEVICE_ERROR;
  3730. }
  3731. if ((TargetStatus == EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION) || (EFI_ERROR (ReturnStatus))) {
  3732. DEBUG ((DEBUG_ERROR, "ScsiDiskRead10: Check Condition happened!\n"));
  3733. DetectMediaParsingSenseKeys (ScsiDiskDevice, ScsiDiskDevice->SenseData, SenseDataLength / sizeof (EFI_SCSI_SENSE_DATA), &Action);
  3734. if (Action == ACTION_RETRY_COMMAND_LATER) {
  3735. *NeedRetry = TRUE;
  3736. return EFI_DEVICE_ERROR;
  3737. } else if (Action == ACTION_RETRY_WITH_BACKOFF_ALGO) {
  3738. if (SectorCount <= 1) {
  3739. //
  3740. // Jump out if the operation still fails with one sector transfer length.
  3741. //
  3742. *NeedRetry = FALSE;
  3743. return EFI_DEVICE_ERROR;
  3744. }
  3745. //
  3746. // Try again with half length if the sense data shows we need to retry.
  3747. //
  3748. SectorCount >>= 1;
  3749. *DataLength = SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize;
  3750. goto BackOff;
  3751. } else {
  3752. *NeedRetry = FALSE;
  3753. return EFI_DEVICE_ERROR;
  3754. }
  3755. }
  3756. return ReturnStatus;
  3757. }
  3758. /**
  3759. Submit Write(10) Command.
  3760. @param ScsiDiskDevice The pointer of ScsiDiskDevice
  3761. @param NeedRetry The pointer of flag indicates if needs retry if error happens
  3762. @param Timeout The time to complete the command
  3763. @param DataBuffer The buffer to fill with the read out data
  3764. @param DataLength The length of buffer
  3765. @param StartLba The start logic block address
  3766. @param SectorCount The number of blocks to write
  3767. @return EFI_STATUS is returned by calling ScsiWrite10Command().
  3768. **/
  3769. EFI_STATUS
  3770. ScsiDiskWrite10 (
  3771. IN SCSI_DISK_DEV *ScsiDiskDevice,
  3772. OUT BOOLEAN *NeedRetry,
  3773. IN UINT64 Timeout,
  3774. IN UINT8 *DataBuffer,
  3775. IN OUT UINT32 *DataLength,
  3776. IN UINT32 StartLba,
  3777. IN UINT32 SectorCount
  3778. )
  3779. {
  3780. EFI_STATUS Status;
  3781. EFI_STATUS ReturnStatus;
  3782. UINT8 SenseDataLength;
  3783. UINT8 HostAdapterStatus;
  3784. UINT8 TargetStatus;
  3785. UINTN Action;
  3786. //
  3787. // Implement a backoff algorithm to resolve some compatibility issues that
  3788. // some SCSI targets or ATAPI devices couldn't correctly response reading/writing
  3789. // big data in a single operation.
  3790. // This algorithm will at first try to execute original request. If the request fails
  3791. // with media error sense data or else, it will reduce the transfer length to half and
  3792. // try again till the operation succeeds or fails with one sector transfer length.
  3793. //
  3794. BackOff:
  3795. *NeedRetry = FALSE;
  3796. Action = ACTION_NO_ACTION;
  3797. SenseDataLength = (UINT8)(ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA));
  3798. ReturnStatus = ScsiWrite10Command (
  3799. ScsiDiskDevice->ScsiIo,
  3800. Timeout,
  3801. ScsiDiskDevice->SenseData,
  3802. &SenseDataLength,
  3803. &HostAdapterStatus,
  3804. &TargetStatus,
  3805. DataBuffer,
  3806. DataLength,
  3807. StartLba,
  3808. SectorCount
  3809. );
  3810. if ((ReturnStatus == EFI_NOT_READY) || (ReturnStatus == EFI_BAD_BUFFER_SIZE)) {
  3811. *NeedRetry = TRUE;
  3812. return EFI_DEVICE_ERROR;
  3813. } else if ((ReturnStatus == EFI_INVALID_PARAMETER) || (ReturnStatus == EFI_UNSUPPORTED)) {
  3814. *NeedRetry = FALSE;
  3815. return ReturnStatus;
  3816. }
  3817. //
  3818. // go ahead to check HostAdapterStatus and TargetStatus
  3819. // (EFI_TIMEOUT, EFI_DEVICE_ERROR, EFI_WARN_BUFFER_TOO_SMALL)
  3820. //
  3821. Status = CheckHostAdapterStatus (HostAdapterStatus);
  3822. if ((Status == EFI_TIMEOUT) || (Status == EFI_NOT_READY)) {
  3823. *NeedRetry = TRUE;
  3824. return EFI_DEVICE_ERROR;
  3825. } else if (Status == EFI_DEVICE_ERROR) {
  3826. //
  3827. // reset the scsi channel
  3828. //
  3829. ScsiDiskDevice->ScsiIo->ResetBus (ScsiDiskDevice->ScsiIo);
  3830. *NeedRetry = FALSE;
  3831. return EFI_DEVICE_ERROR;
  3832. }
  3833. Status = CheckTargetStatus (TargetStatus);
  3834. if (Status == EFI_NOT_READY) {
  3835. //
  3836. // reset the scsi device
  3837. //
  3838. ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo);
  3839. *NeedRetry = TRUE;
  3840. return EFI_DEVICE_ERROR;
  3841. } else if (Status == EFI_DEVICE_ERROR) {
  3842. *NeedRetry = FALSE;
  3843. return EFI_DEVICE_ERROR;
  3844. }
  3845. if ((TargetStatus == EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION) || (EFI_ERROR (ReturnStatus))) {
  3846. DEBUG ((DEBUG_ERROR, "ScsiDiskWrite10: Check Condition happened!\n"));
  3847. DetectMediaParsingSenseKeys (ScsiDiskDevice, ScsiDiskDevice->SenseData, SenseDataLength / sizeof (EFI_SCSI_SENSE_DATA), &Action);
  3848. if (Action == ACTION_RETRY_COMMAND_LATER) {
  3849. *NeedRetry = TRUE;
  3850. return EFI_DEVICE_ERROR;
  3851. } else if (Action == ACTION_RETRY_WITH_BACKOFF_ALGO) {
  3852. if (SectorCount <= 1) {
  3853. //
  3854. // Jump out if the operation still fails with one sector transfer length.
  3855. //
  3856. *NeedRetry = FALSE;
  3857. return EFI_DEVICE_ERROR;
  3858. }
  3859. //
  3860. // Try again with half length if the sense data shows we need to retry.
  3861. //
  3862. SectorCount >>= 1;
  3863. *DataLength = SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize;
  3864. goto BackOff;
  3865. } else {
  3866. *NeedRetry = FALSE;
  3867. return EFI_DEVICE_ERROR;
  3868. }
  3869. }
  3870. return ReturnStatus;
  3871. }
  3872. /**
  3873. Submit Read(16) command.
  3874. @param ScsiDiskDevice The pointer of ScsiDiskDevice
  3875. @param NeedRetry The pointer of flag indicates if needs retry if error happens
  3876. @param Timeout The time to complete the command
  3877. @param DataBuffer The buffer to fill with the read out data
  3878. @param DataLength The length of buffer
  3879. @param StartLba The start logic block address
  3880. @param SectorCount The number of blocks to read
  3881. @return EFI_STATUS is returned by calling ScsiRead16Command().
  3882. **/
  3883. EFI_STATUS
  3884. ScsiDiskRead16 (
  3885. IN SCSI_DISK_DEV *ScsiDiskDevice,
  3886. OUT BOOLEAN *NeedRetry,
  3887. IN UINT64 Timeout,
  3888. OUT UINT8 *DataBuffer,
  3889. IN OUT UINT32 *DataLength,
  3890. IN UINT64 StartLba,
  3891. IN UINT32 SectorCount
  3892. )
  3893. {
  3894. UINT8 SenseDataLength;
  3895. EFI_STATUS Status;
  3896. EFI_STATUS ReturnStatus;
  3897. UINT8 HostAdapterStatus;
  3898. UINT8 TargetStatus;
  3899. UINTN Action;
  3900. //
  3901. // Implement a backoff algorithm to resolve some compatibility issues that
  3902. // some SCSI targets or ATAPI devices couldn't correctly response reading/writing
  3903. // big data in a single operation.
  3904. // This algorithm will at first try to execute original request. If the request fails
  3905. // with media error sense data or else, it will reduce the transfer length to half and
  3906. // try again till the operation succeeds or fails with one sector transfer length.
  3907. //
  3908. BackOff:
  3909. *NeedRetry = FALSE;
  3910. Action = ACTION_NO_ACTION;
  3911. SenseDataLength = (UINT8)(ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA));
  3912. ReturnStatus = ScsiRead16Command (
  3913. ScsiDiskDevice->ScsiIo,
  3914. Timeout,
  3915. ScsiDiskDevice->SenseData,
  3916. &SenseDataLength,
  3917. &HostAdapterStatus,
  3918. &TargetStatus,
  3919. DataBuffer,
  3920. DataLength,
  3921. StartLba,
  3922. SectorCount
  3923. );
  3924. if ((ReturnStatus == EFI_NOT_READY) || (ReturnStatus == EFI_BAD_BUFFER_SIZE)) {
  3925. *NeedRetry = TRUE;
  3926. return EFI_DEVICE_ERROR;
  3927. } else if ((ReturnStatus == EFI_INVALID_PARAMETER) || (ReturnStatus == EFI_UNSUPPORTED)) {
  3928. *NeedRetry = FALSE;
  3929. return ReturnStatus;
  3930. }
  3931. //
  3932. // go ahead to check HostAdapterStatus and TargetStatus
  3933. // (EFI_TIMEOUT, EFI_DEVICE_ERROR, EFI_WARN_BUFFER_TOO_SMALL)
  3934. //
  3935. Status = CheckHostAdapterStatus (HostAdapterStatus);
  3936. if ((Status == EFI_TIMEOUT) || (Status == EFI_NOT_READY)) {
  3937. *NeedRetry = TRUE;
  3938. return EFI_DEVICE_ERROR;
  3939. } else if (Status == EFI_DEVICE_ERROR) {
  3940. //
  3941. // reset the scsi channel
  3942. //
  3943. ScsiDiskDevice->ScsiIo->ResetBus (ScsiDiskDevice->ScsiIo);
  3944. *NeedRetry = FALSE;
  3945. return EFI_DEVICE_ERROR;
  3946. }
  3947. Status = CheckTargetStatus (TargetStatus);
  3948. if (Status == EFI_NOT_READY) {
  3949. //
  3950. // reset the scsi device
  3951. //
  3952. ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo);
  3953. *NeedRetry = TRUE;
  3954. return EFI_DEVICE_ERROR;
  3955. } else if (Status == EFI_DEVICE_ERROR) {
  3956. *NeedRetry = FALSE;
  3957. return EFI_DEVICE_ERROR;
  3958. }
  3959. if ((TargetStatus == EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION) || (EFI_ERROR (ReturnStatus))) {
  3960. DEBUG ((DEBUG_ERROR, "ScsiDiskRead16: Check Condition happened!\n"));
  3961. DetectMediaParsingSenseKeys (ScsiDiskDevice, ScsiDiskDevice->SenseData, SenseDataLength / sizeof (EFI_SCSI_SENSE_DATA), &Action);
  3962. if (Action == ACTION_RETRY_COMMAND_LATER) {
  3963. *NeedRetry = TRUE;
  3964. return EFI_DEVICE_ERROR;
  3965. } else if (Action == ACTION_RETRY_WITH_BACKOFF_ALGO) {
  3966. if (SectorCount <= 1) {
  3967. //
  3968. // Jump out if the operation still fails with one sector transfer length.
  3969. //
  3970. *NeedRetry = FALSE;
  3971. return EFI_DEVICE_ERROR;
  3972. }
  3973. //
  3974. // Try again with half length if the sense data shows we need to retry.
  3975. //
  3976. SectorCount >>= 1;
  3977. *DataLength = SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize;
  3978. goto BackOff;
  3979. } else {
  3980. *NeedRetry = FALSE;
  3981. return EFI_DEVICE_ERROR;
  3982. }
  3983. }
  3984. return ReturnStatus;
  3985. }
  3986. /**
  3987. Submit Write(16) Command.
  3988. @param ScsiDiskDevice The pointer of ScsiDiskDevice
  3989. @param NeedRetry The pointer of flag indicates if needs retry if error happens
  3990. @param Timeout The time to complete the command
  3991. @param DataBuffer The buffer to fill with the read out data
  3992. @param DataLength The length of buffer
  3993. @param StartLba The start logic block address
  3994. @param SectorCount The number of blocks to write
  3995. @return EFI_STATUS is returned by calling ScsiWrite16Command().
  3996. **/
  3997. EFI_STATUS
  3998. ScsiDiskWrite16 (
  3999. IN SCSI_DISK_DEV *ScsiDiskDevice,
  4000. OUT BOOLEAN *NeedRetry,
  4001. IN UINT64 Timeout,
  4002. IN UINT8 *DataBuffer,
  4003. IN OUT UINT32 *DataLength,
  4004. IN UINT64 StartLba,
  4005. IN UINT32 SectorCount
  4006. )
  4007. {
  4008. EFI_STATUS Status;
  4009. EFI_STATUS ReturnStatus;
  4010. UINT8 SenseDataLength;
  4011. UINT8 HostAdapterStatus;
  4012. UINT8 TargetStatus;
  4013. UINTN Action;
  4014. //
  4015. // Implement a backoff algorithm to resolve some compatibility issues that
  4016. // some SCSI targets or ATAPI devices couldn't correctly response reading/writing
  4017. // big data in a single operation.
  4018. // This algorithm will at first try to execute original request. If the request fails
  4019. // with media error sense data or else, it will reduce the transfer length to half and
  4020. // try again till the operation succeeds or fails with one sector transfer length.
  4021. //
  4022. BackOff:
  4023. *NeedRetry = FALSE;
  4024. Action = ACTION_NO_ACTION;
  4025. SenseDataLength = (UINT8)(ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA));
  4026. ReturnStatus = ScsiWrite16Command (
  4027. ScsiDiskDevice->ScsiIo,
  4028. Timeout,
  4029. ScsiDiskDevice->SenseData,
  4030. &SenseDataLength,
  4031. &HostAdapterStatus,
  4032. &TargetStatus,
  4033. DataBuffer,
  4034. DataLength,
  4035. StartLba,
  4036. SectorCount
  4037. );
  4038. if ((ReturnStatus == EFI_NOT_READY) || (ReturnStatus == EFI_BAD_BUFFER_SIZE)) {
  4039. *NeedRetry = TRUE;
  4040. return EFI_DEVICE_ERROR;
  4041. } else if ((ReturnStatus == EFI_INVALID_PARAMETER) || (ReturnStatus == EFI_UNSUPPORTED)) {
  4042. *NeedRetry = FALSE;
  4043. return ReturnStatus;
  4044. }
  4045. //
  4046. // go ahead to check HostAdapterStatus and TargetStatus
  4047. // (EFI_TIMEOUT, EFI_DEVICE_ERROR, EFI_WARN_BUFFER_TOO_SMALL)
  4048. //
  4049. Status = CheckHostAdapterStatus (HostAdapterStatus);
  4050. if ((Status == EFI_TIMEOUT) || (Status == EFI_NOT_READY)) {
  4051. *NeedRetry = TRUE;
  4052. return EFI_DEVICE_ERROR;
  4053. } else if (Status == EFI_DEVICE_ERROR) {
  4054. //
  4055. // reset the scsi channel
  4056. //
  4057. ScsiDiskDevice->ScsiIo->ResetBus (ScsiDiskDevice->ScsiIo);
  4058. *NeedRetry = FALSE;
  4059. return EFI_DEVICE_ERROR;
  4060. }
  4061. Status = CheckTargetStatus (TargetStatus);
  4062. if (Status == EFI_NOT_READY) {
  4063. //
  4064. // reset the scsi device
  4065. //
  4066. ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo);
  4067. *NeedRetry = TRUE;
  4068. return EFI_DEVICE_ERROR;
  4069. } else if (Status == EFI_DEVICE_ERROR) {
  4070. *NeedRetry = FALSE;
  4071. return EFI_DEVICE_ERROR;
  4072. }
  4073. if ((TargetStatus == EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION) || (EFI_ERROR (ReturnStatus))) {
  4074. DEBUG ((DEBUG_ERROR, "ScsiDiskWrite16: Check Condition happened!\n"));
  4075. DetectMediaParsingSenseKeys (ScsiDiskDevice, ScsiDiskDevice->SenseData, SenseDataLength / sizeof (EFI_SCSI_SENSE_DATA), &Action);
  4076. if (Action == ACTION_RETRY_COMMAND_LATER) {
  4077. *NeedRetry = TRUE;
  4078. return EFI_DEVICE_ERROR;
  4079. } else if (Action == ACTION_RETRY_WITH_BACKOFF_ALGO) {
  4080. if (SectorCount <= 1) {
  4081. //
  4082. // Jump out if the operation still fails with one sector transfer length.
  4083. //
  4084. *NeedRetry = FALSE;
  4085. return EFI_DEVICE_ERROR;
  4086. }
  4087. //
  4088. // Try again with half length if the sense data shows we need to retry.
  4089. //
  4090. SectorCount >>= 1;
  4091. *DataLength = SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize;
  4092. goto BackOff;
  4093. } else {
  4094. *NeedRetry = FALSE;
  4095. return EFI_DEVICE_ERROR;
  4096. }
  4097. }
  4098. return ReturnStatus;
  4099. }
  4100. /**
  4101. Internal helper notify function in which determine whether retry of a SCSI
  4102. Read/Write command is needed and signal the event passed from Block I/O(2) if
  4103. the SCSI I/O operation completes.
  4104. @param Event The instance of EFI_EVENT.
  4105. @param Context The parameter passed in.
  4106. **/
  4107. VOID
  4108. EFIAPI
  4109. ScsiDiskNotify (
  4110. IN EFI_EVENT Event,
  4111. IN VOID *Context
  4112. )
  4113. {
  4114. EFI_STATUS Status;
  4115. SCSI_ASYNC_RW_REQUEST *Request;
  4116. SCSI_DISK_DEV *ScsiDiskDevice;
  4117. EFI_BLOCK_IO2_TOKEN *Token;
  4118. UINTN Action;
  4119. UINT32 OldDataLength;
  4120. UINT32 OldSectorCount;
  4121. UINT8 MaxRetry;
  4122. gBS->CloseEvent (Event);
  4123. Request = (SCSI_ASYNC_RW_REQUEST *)Context;
  4124. ScsiDiskDevice = Request->ScsiDiskDevice;
  4125. Token = Request->BlkIo2Req->Token;
  4126. OldDataLength = Request->DataLength;
  4127. OldSectorCount = Request->SectorCount;
  4128. MaxRetry = 2;
  4129. //
  4130. // If previous sub-tasks already fails, no need to process this sub-task.
  4131. //
  4132. if (Token->TransactionStatus != EFI_SUCCESS) {
  4133. goto Exit;
  4134. }
  4135. //
  4136. // Check HostAdapterStatus and TargetStatus
  4137. // (EFI_TIMEOUT, EFI_DEVICE_ERROR, EFI_WARN_BUFFER_TOO_SMALL)
  4138. //
  4139. Status = CheckHostAdapterStatus (Request->HostAdapterStatus);
  4140. if ((Status == EFI_TIMEOUT) || (Status == EFI_NOT_READY)) {
  4141. if (++Request->TimesRetry > MaxRetry) {
  4142. Token->TransactionStatus = EFI_DEVICE_ERROR;
  4143. goto Exit;
  4144. } else {
  4145. goto Retry;
  4146. }
  4147. } else if (Status == EFI_DEVICE_ERROR) {
  4148. //
  4149. // reset the scsi channel
  4150. //
  4151. ScsiDiskDevice->ScsiIo->ResetBus (ScsiDiskDevice->ScsiIo);
  4152. Token->TransactionStatus = EFI_DEVICE_ERROR;
  4153. goto Exit;
  4154. }
  4155. Status = CheckTargetStatus (Request->TargetStatus);
  4156. if (Status == EFI_NOT_READY) {
  4157. //
  4158. // reset the scsi device
  4159. //
  4160. ScsiDiskDevice->ScsiIo->ResetDevice (ScsiDiskDevice->ScsiIo);
  4161. if (++Request->TimesRetry > MaxRetry) {
  4162. Token->TransactionStatus = EFI_DEVICE_ERROR;
  4163. goto Exit;
  4164. } else {
  4165. goto Retry;
  4166. }
  4167. } else if (Status == EFI_DEVICE_ERROR) {
  4168. Token->TransactionStatus = EFI_DEVICE_ERROR;
  4169. goto Exit;
  4170. }
  4171. if (Request->TargetStatus == EFI_EXT_SCSI_STATUS_TARGET_CHECK_CONDITION) {
  4172. DEBUG ((DEBUG_ERROR, "ScsiDiskNotify: Check Condition happened!\n"));
  4173. DetectMediaParsingSenseKeys (
  4174. ScsiDiskDevice,
  4175. Request->SenseData,
  4176. Request->SenseDataLength / sizeof (EFI_SCSI_SENSE_DATA),
  4177. &Action
  4178. );
  4179. if (Action == ACTION_RETRY_COMMAND_LATER) {
  4180. if (++Request->TimesRetry > MaxRetry) {
  4181. Token->TransactionStatus = EFI_DEVICE_ERROR;
  4182. goto Exit;
  4183. } else {
  4184. goto Retry;
  4185. }
  4186. } else if (Action == ACTION_RETRY_WITH_BACKOFF_ALGO) {
  4187. if (Request->SectorCount <= 1) {
  4188. //
  4189. // Jump out if the operation still fails with one sector transfer
  4190. // length.
  4191. //
  4192. Token->TransactionStatus = EFI_DEVICE_ERROR;
  4193. goto Exit;
  4194. }
  4195. //
  4196. // Try again with two half length request if the sense data shows we need
  4197. // to retry.
  4198. //
  4199. Request->SectorCount >>= 1;
  4200. Request->DataLength = Request->SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize;
  4201. Request->TimesRetry = 0;
  4202. goto Retry;
  4203. } else {
  4204. Token->TransactionStatus = EFI_DEVICE_ERROR;
  4205. goto Exit;
  4206. }
  4207. }
  4208. //
  4209. // This sub-task succeeds, no need to retry.
  4210. //
  4211. goto Exit;
  4212. Retry:
  4213. if (Request->InBuffer != NULL) {
  4214. //
  4215. // SCSI read command
  4216. //
  4217. if (!ScsiDiskDevice->Cdb16Byte) {
  4218. Status = ScsiDiskAsyncRead10 (
  4219. ScsiDiskDevice,
  4220. Request->Timeout,
  4221. Request->TimesRetry,
  4222. Request->InBuffer,
  4223. Request->DataLength,
  4224. (UINT32)Request->StartLba,
  4225. Request->SectorCount,
  4226. Request->BlkIo2Req,
  4227. Token
  4228. );
  4229. } else {
  4230. Status = ScsiDiskAsyncRead16 (
  4231. ScsiDiskDevice,
  4232. Request->Timeout,
  4233. Request->TimesRetry,
  4234. Request->InBuffer,
  4235. Request->DataLength,
  4236. Request->StartLba,
  4237. Request->SectorCount,
  4238. Request->BlkIo2Req,
  4239. Token
  4240. );
  4241. }
  4242. if (EFI_ERROR (Status)) {
  4243. Token->TransactionStatus = EFI_DEVICE_ERROR;
  4244. goto Exit;
  4245. } else if (OldSectorCount != Request->SectorCount) {
  4246. //
  4247. // Original sub-task will be split into two new sub-tasks with smaller
  4248. // DataLength
  4249. //
  4250. if (!ScsiDiskDevice->Cdb16Byte) {
  4251. Status = ScsiDiskAsyncRead10 (
  4252. ScsiDiskDevice,
  4253. Request->Timeout,
  4254. 0,
  4255. Request->InBuffer + Request->SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize,
  4256. OldDataLength - Request->DataLength,
  4257. (UINT32)Request->StartLba + Request->SectorCount,
  4258. OldSectorCount - Request->SectorCount,
  4259. Request->BlkIo2Req,
  4260. Token
  4261. );
  4262. } else {
  4263. Status = ScsiDiskAsyncRead16 (
  4264. ScsiDiskDevice,
  4265. Request->Timeout,
  4266. 0,
  4267. Request->InBuffer + Request->SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize,
  4268. OldDataLength - Request->DataLength,
  4269. Request->StartLba + Request->SectorCount,
  4270. OldSectorCount - Request->SectorCount,
  4271. Request->BlkIo2Req,
  4272. Token
  4273. );
  4274. }
  4275. if (EFI_ERROR (Status)) {
  4276. Token->TransactionStatus = EFI_DEVICE_ERROR;
  4277. goto Exit;
  4278. }
  4279. }
  4280. } else {
  4281. //
  4282. // SCSI write command
  4283. //
  4284. if (!ScsiDiskDevice->Cdb16Byte) {
  4285. Status = ScsiDiskAsyncWrite10 (
  4286. ScsiDiskDevice,
  4287. Request->Timeout,
  4288. Request->TimesRetry,
  4289. Request->OutBuffer,
  4290. Request->DataLength,
  4291. (UINT32)Request->StartLba,
  4292. Request->SectorCount,
  4293. Request->BlkIo2Req,
  4294. Token
  4295. );
  4296. } else {
  4297. Status = ScsiDiskAsyncWrite16 (
  4298. ScsiDiskDevice,
  4299. Request->Timeout,
  4300. Request->TimesRetry,
  4301. Request->OutBuffer,
  4302. Request->DataLength,
  4303. Request->StartLba,
  4304. Request->SectorCount,
  4305. Request->BlkIo2Req,
  4306. Token
  4307. );
  4308. }
  4309. if (EFI_ERROR (Status)) {
  4310. Token->TransactionStatus = EFI_DEVICE_ERROR;
  4311. goto Exit;
  4312. } else if (OldSectorCount != Request->SectorCount) {
  4313. //
  4314. // Original sub-task will be split into two new sub-tasks with smaller
  4315. // DataLength
  4316. //
  4317. if (!ScsiDiskDevice->Cdb16Byte) {
  4318. Status = ScsiDiskAsyncWrite10 (
  4319. ScsiDiskDevice,
  4320. Request->Timeout,
  4321. 0,
  4322. Request->OutBuffer + Request->SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize,
  4323. OldDataLength - Request->DataLength,
  4324. (UINT32)Request->StartLba + Request->SectorCount,
  4325. OldSectorCount - Request->SectorCount,
  4326. Request->BlkIo2Req,
  4327. Token
  4328. );
  4329. } else {
  4330. Status = ScsiDiskAsyncWrite16 (
  4331. ScsiDiskDevice,
  4332. Request->Timeout,
  4333. 0,
  4334. Request->OutBuffer + Request->SectorCount * ScsiDiskDevice->BlkIo.Media->BlockSize,
  4335. OldDataLength - Request->DataLength,
  4336. Request->StartLba + Request->SectorCount,
  4337. OldSectorCount - Request->SectorCount,
  4338. Request->BlkIo2Req,
  4339. Token
  4340. );
  4341. }
  4342. if (EFI_ERROR (Status)) {
  4343. Token->TransactionStatus = EFI_DEVICE_ERROR;
  4344. goto Exit;
  4345. }
  4346. }
  4347. }
  4348. Exit:
  4349. RemoveEntryList (&Request->Link);
  4350. if ((IsListEmpty (&Request->BlkIo2Req->ScsiRWQueue)) &&
  4351. (Request->BlkIo2Req->LastScsiRW))
  4352. {
  4353. //
  4354. // The last SCSI R/W command of a BlockIo2 request completes
  4355. //
  4356. RemoveEntryList (&Request->BlkIo2Req->Link);
  4357. FreePool (Request->BlkIo2Req); // Should be freed only once
  4358. gBS->SignalEvent (Token->Event);
  4359. }
  4360. FreePool (Request->SenseData);
  4361. FreePool (Request);
  4362. }
  4363. /**
  4364. Submit Async Read(10) command.
  4365. @param ScsiDiskDevice The pointer of ScsiDiskDevice.
  4366. @param Timeout The time to complete the command.
  4367. @param TimesRetry The number of times the command has been retried.
  4368. @param DataBuffer The buffer to fill with the read out data.
  4369. @param DataLength The length of buffer.
  4370. @param StartLba The start logic block address.
  4371. @param SectorCount The number of blocks to read.
  4372. @param BlkIo2Req The upstream BlockIo2 request.
  4373. @param Token The pointer to the token associated with the
  4374. non-blocking read request.
  4375. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
  4376. lack of resources.
  4377. @return others Status returned by calling
  4378. ScsiRead10CommandEx().
  4379. **/
  4380. EFI_STATUS
  4381. ScsiDiskAsyncRead10 (
  4382. IN SCSI_DISK_DEV *ScsiDiskDevice,
  4383. IN UINT64 Timeout,
  4384. IN UINT8 TimesRetry,
  4385. OUT UINT8 *DataBuffer,
  4386. IN UINT32 DataLength,
  4387. IN UINT32 StartLba,
  4388. IN UINT32 SectorCount,
  4389. IN OUT SCSI_BLKIO2_REQUEST *BlkIo2Req,
  4390. IN EFI_BLOCK_IO2_TOKEN *Token
  4391. )
  4392. {
  4393. EFI_STATUS Status;
  4394. SCSI_ASYNC_RW_REQUEST *Request;
  4395. EFI_EVENT AsyncIoEvent;
  4396. EFI_TPL OldTpl;
  4397. AsyncIoEvent = NULL;
  4398. Request = AllocateZeroPool (sizeof (SCSI_ASYNC_RW_REQUEST));
  4399. if (Request == NULL) {
  4400. return EFI_OUT_OF_RESOURCES;
  4401. }
  4402. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  4403. InsertTailList (&BlkIo2Req->ScsiRWQueue, &Request->Link);
  4404. gBS->RestoreTPL (OldTpl);
  4405. Request->SenseDataLength = (UINT8)(6 * sizeof (EFI_SCSI_SENSE_DATA));
  4406. Request->SenseData = AllocateZeroPool (Request->SenseDataLength);
  4407. if (Request->SenseData == NULL) {
  4408. Status = EFI_OUT_OF_RESOURCES;
  4409. goto ErrorExit;
  4410. }
  4411. Request->ScsiDiskDevice = ScsiDiskDevice;
  4412. Request->Timeout = Timeout;
  4413. Request->TimesRetry = TimesRetry;
  4414. Request->InBuffer = DataBuffer;
  4415. Request->DataLength = DataLength;
  4416. Request->StartLba = StartLba;
  4417. Request->SectorCount = SectorCount;
  4418. Request->BlkIo2Req = BlkIo2Req;
  4419. //
  4420. // Create Event
  4421. //
  4422. Status = gBS->CreateEvent (
  4423. EVT_NOTIFY_SIGNAL,
  4424. TPL_NOTIFY,
  4425. ScsiDiskNotify,
  4426. Request,
  4427. &AsyncIoEvent
  4428. );
  4429. if (EFI_ERROR (Status)) {
  4430. goto ErrorExit;
  4431. }
  4432. Status = ScsiRead10CommandEx (
  4433. ScsiDiskDevice->ScsiIo,
  4434. Request->Timeout,
  4435. Request->SenseData,
  4436. &Request->SenseDataLength,
  4437. &Request->HostAdapterStatus,
  4438. &Request->TargetStatus,
  4439. Request->InBuffer,
  4440. &Request->DataLength,
  4441. (UINT32)Request->StartLba,
  4442. Request->SectorCount,
  4443. AsyncIoEvent
  4444. );
  4445. if (EFI_ERROR (Status)) {
  4446. goto ErrorExit;
  4447. }
  4448. return EFI_SUCCESS;
  4449. ErrorExit:
  4450. if (AsyncIoEvent != NULL) {
  4451. gBS->CloseEvent (AsyncIoEvent);
  4452. }
  4453. if (Request != NULL) {
  4454. if (Request->SenseData != NULL) {
  4455. FreePool (Request->SenseData);
  4456. }
  4457. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  4458. RemoveEntryList (&Request->Link);
  4459. gBS->RestoreTPL (OldTpl);
  4460. FreePool (Request);
  4461. }
  4462. return Status;
  4463. }
  4464. /**
  4465. Submit Async Write(10) command.
  4466. @param ScsiDiskDevice The pointer of ScsiDiskDevice.
  4467. @param Timeout The time to complete the command.
  4468. @param TimesRetry The number of times the command has been retried.
  4469. @param DataBuffer The buffer contains the data to write.
  4470. @param DataLength The length of buffer.
  4471. @param StartLba The start logic block address.
  4472. @param SectorCount The number of blocks to write.
  4473. @param BlkIo2Req The upstream BlockIo2 request.
  4474. @param Token The pointer to the token associated with the
  4475. non-blocking read request.
  4476. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
  4477. lack of resources.
  4478. @return others Status returned by calling
  4479. ScsiWrite10CommandEx().
  4480. **/
  4481. EFI_STATUS
  4482. ScsiDiskAsyncWrite10 (
  4483. IN SCSI_DISK_DEV *ScsiDiskDevice,
  4484. IN UINT64 Timeout,
  4485. IN UINT8 TimesRetry,
  4486. IN UINT8 *DataBuffer,
  4487. IN UINT32 DataLength,
  4488. IN UINT32 StartLba,
  4489. IN UINT32 SectorCount,
  4490. IN OUT SCSI_BLKIO2_REQUEST *BlkIo2Req,
  4491. IN EFI_BLOCK_IO2_TOKEN *Token
  4492. )
  4493. {
  4494. EFI_STATUS Status;
  4495. SCSI_ASYNC_RW_REQUEST *Request;
  4496. EFI_EVENT AsyncIoEvent;
  4497. EFI_TPL OldTpl;
  4498. AsyncIoEvent = NULL;
  4499. Request = AllocateZeroPool (sizeof (SCSI_ASYNC_RW_REQUEST));
  4500. if (Request == NULL) {
  4501. return EFI_OUT_OF_RESOURCES;
  4502. }
  4503. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  4504. InsertTailList (&BlkIo2Req->ScsiRWQueue, &Request->Link);
  4505. gBS->RestoreTPL (OldTpl);
  4506. Request->SenseDataLength = (UINT8)(6 * sizeof (EFI_SCSI_SENSE_DATA));
  4507. Request->SenseData = AllocateZeroPool (Request->SenseDataLength);
  4508. if (Request->SenseData == NULL) {
  4509. Status = EFI_OUT_OF_RESOURCES;
  4510. goto ErrorExit;
  4511. }
  4512. Request->ScsiDiskDevice = ScsiDiskDevice;
  4513. Request->Timeout = Timeout;
  4514. Request->TimesRetry = TimesRetry;
  4515. Request->OutBuffer = DataBuffer;
  4516. Request->DataLength = DataLength;
  4517. Request->StartLba = StartLba;
  4518. Request->SectorCount = SectorCount;
  4519. Request->BlkIo2Req = BlkIo2Req;
  4520. //
  4521. // Create Event
  4522. //
  4523. Status = gBS->CreateEvent (
  4524. EVT_NOTIFY_SIGNAL,
  4525. TPL_NOTIFY,
  4526. ScsiDiskNotify,
  4527. Request,
  4528. &AsyncIoEvent
  4529. );
  4530. if (EFI_ERROR (Status)) {
  4531. goto ErrorExit;
  4532. }
  4533. Status = ScsiWrite10CommandEx (
  4534. ScsiDiskDevice->ScsiIo,
  4535. Request->Timeout,
  4536. Request->SenseData,
  4537. &Request->SenseDataLength,
  4538. &Request->HostAdapterStatus,
  4539. &Request->TargetStatus,
  4540. Request->OutBuffer,
  4541. &Request->DataLength,
  4542. (UINT32)Request->StartLba,
  4543. Request->SectorCount,
  4544. AsyncIoEvent
  4545. );
  4546. if (EFI_ERROR (Status)) {
  4547. goto ErrorExit;
  4548. }
  4549. return EFI_SUCCESS;
  4550. ErrorExit:
  4551. if (AsyncIoEvent != NULL) {
  4552. gBS->CloseEvent (AsyncIoEvent);
  4553. }
  4554. if (Request != NULL) {
  4555. if (Request->SenseData != NULL) {
  4556. FreePool (Request->SenseData);
  4557. }
  4558. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  4559. RemoveEntryList (&Request->Link);
  4560. gBS->RestoreTPL (OldTpl);
  4561. FreePool (Request);
  4562. }
  4563. return Status;
  4564. }
  4565. /**
  4566. Submit Async Read(16) command.
  4567. @param ScsiDiskDevice The pointer of ScsiDiskDevice.
  4568. @param Timeout The time to complete the command.
  4569. @param TimesRetry The number of times the command has been retried.
  4570. @param DataBuffer The buffer to fill with the read out data.
  4571. @param DataLength The length of buffer.
  4572. @param StartLba The start logic block address.
  4573. @param SectorCount The number of blocks to read.
  4574. @param BlkIo2Req The upstream BlockIo2 request.
  4575. @param Token The pointer to the token associated with the
  4576. non-blocking read request.
  4577. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
  4578. lack of resources.
  4579. @return others Status returned by calling
  4580. ScsiRead16CommandEx().
  4581. **/
  4582. EFI_STATUS
  4583. ScsiDiskAsyncRead16 (
  4584. IN SCSI_DISK_DEV *ScsiDiskDevice,
  4585. IN UINT64 Timeout,
  4586. IN UINT8 TimesRetry,
  4587. OUT UINT8 *DataBuffer,
  4588. IN UINT32 DataLength,
  4589. IN UINT64 StartLba,
  4590. IN UINT32 SectorCount,
  4591. IN OUT SCSI_BLKIO2_REQUEST *BlkIo2Req,
  4592. IN EFI_BLOCK_IO2_TOKEN *Token
  4593. )
  4594. {
  4595. EFI_STATUS Status;
  4596. SCSI_ASYNC_RW_REQUEST *Request;
  4597. EFI_EVENT AsyncIoEvent;
  4598. EFI_TPL OldTpl;
  4599. AsyncIoEvent = NULL;
  4600. Request = AllocateZeroPool (sizeof (SCSI_ASYNC_RW_REQUEST));
  4601. if (Request == NULL) {
  4602. return EFI_OUT_OF_RESOURCES;
  4603. }
  4604. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  4605. InsertTailList (&BlkIo2Req->ScsiRWQueue, &Request->Link);
  4606. gBS->RestoreTPL (OldTpl);
  4607. Request->SenseDataLength = (UINT8)(6 * sizeof (EFI_SCSI_SENSE_DATA));
  4608. Request->SenseData = AllocateZeroPool (Request->SenseDataLength);
  4609. if (Request->SenseData == NULL) {
  4610. Status = EFI_OUT_OF_RESOURCES;
  4611. goto ErrorExit;
  4612. }
  4613. Request->ScsiDiskDevice = ScsiDiskDevice;
  4614. Request->Timeout = Timeout;
  4615. Request->TimesRetry = TimesRetry;
  4616. Request->InBuffer = DataBuffer;
  4617. Request->DataLength = DataLength;
  4618. Request->StartLba = StartLba;
  4619. Request->SectorCount = SectorCount;
  4620. Request->BlkIo2Req = BlkIo2Req;
  4621. //
  4622. // Create Event
  4623. //
  4624. Status = gBS->CreateEvent (
  4625. EVT_NOTIFY_SIGNAL,
  4626. TPL_NOTIFY,
  4627. ScsiDiskNotify,
  4628. Request,
  4629. &AsyncIoEvent
  4630. );
  4631. if (EFI_ERROR (Status)) {
  4632. goto ErrorExit;
  4633. }
  4634. Status = ScsiRead16CommandEx (
  4635. ScsiDiskDevice->ScsiIo,
  4636. Request->Timeout,
  4637. Request->SenseData,
  4638. &Request->SenseDataLength,
  4639. &Request->HostAdapterStatus,
  4640. &Request->TargetStatus,
  4641. Request->InBuffer,
  4642. &Request->DataLength,
  4643. Request->StartLba,
  4644. Request->SectorCount,
  4645. AsyncIoEvent
  4646. );
  4647. if (EFI_ERROR (Status)) {
  4648. goto ErrorExit;
  4649. }
  4650. return EFI_SUCCESS;
  4651. ErrorExit:
  4652. if (AsyncIoEvent != NULL) {
  4653. gBS->CloseEvent (AsyncIoEvent);
  4654. }
  4655. if (Request != NULL) {
  4656. if (Request->SenseData != NULL) {
  4657. FreePool (Request->SenseData);
  4658. }
  4659. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  4660. RemoveEntryList (&Request->Link);
  4661. gBS->RestoreTPL (OldTpl);
  4662. FreePool (Request);
  4663. }
  4664. return Status;
  4665. }
  4666. /**
  4667. Submit Async Write(16) command.
  4668. @param ScsiDiskDevice The pointer of ScsiDiskDevice.
  4669. @param Timeout The time to complete the command.
  4670. @param TimesRetry The number of times the command has been retried.
  4671. @param DataBuffer The buffer contains the data to write.
  4672. @param DataLength The length of buffer.
  4673. @param StartLba The start logic block address.
  4674. @param SectorCount The number of blocks to write.
  4675. @param BlkIo2Req The upstream BlockIo2 request.
  4676. @param Token The pointer to the token associated with the
  4677. non-blocking read request.
  4678. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
  4679. lack of resources.
  4680. @return others Status returned by calling
  4681. ScsiWrite16CommandEx().
  4682. **/
  4683. EFI_STATUS
  4684. ScsiDiskAsyncWrite16 (
  4685. IN SCSI_DISK_DEV *ScsiDiskDevice,
  4686. IN UINT64 Timeout,
  4687. IN UINT8 TimesRetry,
  4688. IN UINT8 *DataBuffer,
  4689. IN UINT32 DataLength,
  4690. IN UINT64 StartLba,
  4691. IN UINT32 SectorCount,
  4692. IN OUT SCSI_BLKIO2_REQUEST *BlkIo2Req,
  4693. IN EFI_BLOCK_IO2_TOKEN *Token
  4694. )
  4695. {
  4696. EFI_STATUS Status;
  4697. SCSI_ASYNC_RW_REQUEST *Request;
  4698. EFI_EVENT AsyncIoEvent;
  4699. EFI_TPL OldTpl;
  4700. AsyncIoEvent = NULL;
  4701. Request = AllocateZeroPool (sizeof (SCSI_ASYNC_RW_REQUEST));
  4702. if (Request == NULL) {
  4703. return EFI_OUT_OF_RESOURCES;
  4704. }
  4705. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  4706. InsertTailList (&BlkIo2Req->ScsiRWQueue, &Request->Link);
  4707. gBS->RestoreTPL (OldTpl);
  4708. Request->SenseDataLength = (UINT8)(6 * sizeof (EFI_SCSI_SENSE_DATA));
  4709. Request->SenseData = AllocateZeroPool (Request->SenseDataLength);
  4710. if (Request->SenseData == NULL) {
  4711. Status = EFI_OUT_OF_RESOURCES;
  4712. goto ErrorExit;
  4713. }
  4714. Request->ScsiDiskDevice = ScsiDiskDevice;
  4715. Request->Timeout = Timeout;
  4716. Request->TimesRetry = TimesRetry;
  4717. Request->OutBuffer = DataBuffer;
  4718. Request->DataLength = DataLength;
  4719. Request->StartLba = StartLba;
  4720. Request->SectorCount = SectorCount;
  4721. Request->BlkIo2Req = BlkIo2Req;
  4722. //
  4723. // Create Event
  4724. //
  4725. Status = gBS->CreateEvent (
  4726. EVT_NOTIFY_SIGNAL,
  4727. TPL_NOTIFY,
  4728. ScsiDiskNotify,
  4729. Request,
  4730. &AsyncIoEvent
  4731. );
  4732. if (EFI_ERROR (Status)) {
  4733. goto ErrorExit;
  4734. }
  4735. Status = ScsiWrite16CommandEx (
  4736. ScsiDiskDevice->ScsiIo,
  4737. Request->Timeout,
  4738. Request->SenseData,
  4739. &Request->SenseDataLength,
  4740. &Request->HostAdapterStatus,
  4741. &Request->TargetStatus,
  4742. Request->OutBuffer,
  4743. &Request->DataLength,
  4744. Request->StartLba,
  4745. Request->SectorCount,
  4746. AsyncIoEvent
  4747. );
  4748. if (EFI_ERROR (Status)) {
  4749. goto ErrorExit;
  4750. }
  4751. return EFI_SUCCESS;
  4752. ErrorExit:
  4753. if (AsyncIoEvent != NULL) {
  4754. gBS->CloseEvent (AsyncIoEvent);
  4755. }
  4756. if (Request != NULL) {
  4757. if (Request->SenseData != NULL) {
  4758. FreePool (Request->SenseData);
  4759. }
  4760. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  4761. RemoveEntryList (&Request->Link);
  4762. gBS->RestoreTPL (OldTpl);
  4763. FreePool (Request);
  4764. }
  4765. return Status;
  4766. }
  4767. /**
  4768. Check sense key to find if media presents.
  4769. @param SenseData The pointer of EFI_SCSI_SENSE_DATA
  4770. @param SenseCounts The number of sense key
  4771. @retval TRUE NOT any media
  4772. @retval FALSE Media presents
  4773. **/
  4774. BOOLEAN
  4775. ScsiDiskIsNoMedia (
  4776. IN EFI_SCSI_SENSE_DATA *SenseData,
  4777. IN UINTN SenseCounts
  4778. )
  4779. {
  4780. EFI_SCSI_SENSE_DATA *SensePtr;
  4781. UINTN Index;
  4782. BOOLEAN IsNoMedia;
  4783. IsNoMedia = FALSE;
  4784. SensePtr = SenseData;
  4785. for (Index = 0; Index < SenseCounts; Index++) {
  4786. //
  4787. // Sense Key is EFI_SCSI_SK_NOT_READY (0x2),
  4788. // Additional Sense Code is ASC_NO_MEDIA (0x3A)
  4789. //
  4790. if ((SensePtr->Sense_Key == EFI_SCSI_SK_NOT_READY) &&
  4791. (SensePtr->Addnl_Sense_Code == EFI_SCSI_ASC_NO_MEDIA))
  4792. {
  4793. IsNoMedia = TRUE;
  4794. }
  4795. SensePtr++;
  4796. }
  4797. return IsNoMedia;
  4798. }
  4799. /**
  4800. Parse sense key.
  4801. @param SenseData The pointer of EFI_SCSI_SENSE_DATA
  4802. @param SenseCounts The number of sense key
  4803. @retval TRUE Error
  4804. @retval FALSE NOT error
  4805. **/
  4806. BOOLEAN
  4807. ScsiDiskIsMediaError (
  4808. IN EFI_SCSI_SENSE_DATA *SenseData,
  4809. IN UINTN SenseCounts
  4810. )
  4811. {
  4812. EFI_SCSI_SENSE_DATA *SensePtr;
  4813. UINTN Index;
  4814. BOOLEAN IsError;
  4815. IsError = FALSE;
  4816. SensePtr = SenseData;
  4817. for (Index = 0; Index < SenseCounts; Index++) {
  4818. switch (SensePtr->Sense_Key) {
  4819. case EFI_SCSI_SK_MEDIUM_ERROR:
  4820. //
  4821. // Sense Key is EFI_SCSI_SK_MEDIUM_ERROR (0x3)
  4822. //
  4823. switch (SensePtr->Addnl_Sense_Code) {
  4824. //
  4825. // fall through
  4826. //
  4827. case EFI_SCSI_ASC_MEDIA_ERR1:
  4828. //
  4829. // fall through
  4830. //
  4831. case EFI_SCSI_ASC_MEDIA_ERR2:
  4832. //
  4833. // fall through
  4834. //
  4835. case EFI_SCSI_ASC_MEDIA_ERR3:
  4836. case EFI_SCSI_ASC_MEDIA_ERR4:
  4837. IsError = TRUE;
  4838. break;
  4839. default:
  4840. break;
  4841. }
  4842. break;
  4843. case EFI_SCSI_SK_NOT_READY:
  4844. //
  4845. // Sense Key is EFI_SCSI_SK_NOT_READY (0x2)
  4846. //
  4847. switch (SensePtr->Addnl_Sense_Code) {
  4848. //
  4849. // Additional Sense Code is ASC_MEDIA_UPSIDE_DOWN (0x6)
  4850. //
  4851. case EFI_SCSI_ASC_MEDIA_UPSIDE_DOWN:
  4852. IsError = TRUE;
  4853. break;
  4854. default:
  4855. break;
  4856. }
  4857. break;
  4858. default:
  4859. break;
  4860. }
  4861. SensePtr++;
  4862. }
  4863. return IsError;
  4864. }
  4865. /**
  4866. Check sense key to find if hardware error happens.
  4867. @param SenseData The pointer of EFI_SCSI_SENSE_DATA
  4868. @param SenseCounts The number of sense key
  4869. @retval TRUE Hardware error exits.
  4870. @retval FALSE NO error.
  4871. **/
  4872. BOOLEAN
  4873. ScsiDiskIsHardwareError (
  4874. IN EFI_SCSI_SENSE_DATA *SenseData,
  4875. IN UINTN SenseCounts
  4876. )
  4877. {
  4878. EFI_SCSI_SENSE_DATA *SensePtr;
  4879. UINTN Index;
  4880. BOOLEAN IsError;
  4881. IsError = FALSE;
  4882. SensePtr = SenseData;
  4883. for (Index = 0; Index < SenseCounts; Index++) {
  4884. //
  4885. // Sense Key is EFI_SCSI_SK_HARDWARE_ERROR (0x4)
  4886. //
  4887. if (SensePtr->Sense_Key == EFI_SCSI_SK_HARDWARE_ERROR) {
  4888. IsError = TRUE;
  4889. }
  4890. SensePtr++;
  4891. }
  4892. return IsError;
  4893. }
  4894. /**
  4895. Check sense key to find if media has changed.
  4896. @param SenseData The pointer of EFI_SCSI_SENSE_DATA
  4897. @param SenseCounts The number of sense key
  4898. @retval TRUE Media is changed.
  4899. @retval FALSE Media is NOT changed.
  4900. **/
  4901. BOOLEAN
  4902. ScsiDiskIsMediaChange (
  4903. IN EFI_SCSI_SENSE_DATA *SenseData,
  4904. IN UINTN SenseCounts
  4905. )
  4906. {
  4907. EFI_SCSI_SENSE_DATA *SensePtr;
  4908. UINTN Index;
  4909. BOOLEAN IsMediaChanged;
  4910. IsMediaChanged = FALSE;
  4911. SensePtr = SenseData;
  4912. for (Index = 0; Index < SenseCounts; Index++) {
  4913. //
  4914. // Sense Key is EFI_SCSI_SK_UNIT_ATTENTION (0x6),
  4915. // Additional sense code is EFI_SCSI_ASC_MEDIA_CHANGE (0x28)
  4916. //
  4917. if ((SensePtr->Sense_Key == EFI_SCSI_SK_UNIT_ATTENTION) &&
  4918. (SensePtr->Addnl_Sense_Code == EFI_SCSI_ASC_MEDIA_CHANGE))
  4919. {
  4920. IsMediaChanged = TRUE;
  4921. }
  4922. SensePtr++;
  4923. }
  4924. return IsMediaChanged;
  4925. }
  4926. /**
  4927. Check sense key to find if reset happens.
  4928. @param SenseData The pointer of EFI_SCSI_SENSE_DATA
  4929. @param SenseCounts The number of sense key
  4930. @retval TRUE It is reset before.
  4931. @retval FALSE It is NOT reset before.
  4932. **/
  4933. BOOLEAN
  4934. ScsiDiskIsResetBefore (
  4935. IN EFI_SCSI_SENSE_DATA *SenseData,
  4936. IN UINTN SenseCounts
  4937. )
  4938. {
  4939. EFI_SCSI_SENSE_DATA *SensePtr;
  4940. UINTN Index;
  4941. BOOLEAN IsResetBefore;
  4942. IsResetBefore = FALSE;
  4943. SensePtr = SenseData;
  4944. for (Index = 0; Index < SenseCounts; Index++) {
  4945. //
  4946. // Sense Key is EFI_SCSI_SK_UNIT_ATTENTION (0x6)
  4947. // Additional Sense Code is EFI_SCSI_ASC_RESET (0x29)
  4948. //
  4949. if ((SensePtr->Sense_Key == EFI_SCSI_SK_UNIT_ATTENTION) &&
  4950. (SensePtr->Addnl_Sense_Code == EFI_SCSI_ASC_RESET))
  4951. {
  4952. IsResetBefore = TRUE;
  4953. }
  4954. SensePtr++;
  4955. }
  4956. return IsResetBefore;
  4957. }
  4958. /**
  4959. Check sense key to find if the drive is ready.
  4960. @param SenseData The pointer of EFI_SCSI_SENSE_DATA
  4961. @param SenseCounts The number of sense key
  4962. @param RetryLater The flag means if need a retry
  4963. @retval TRUE Drive is ready.
  4964. @retval FALSE Drive is NOT ready.
  4965. **/
  4966. BOOLEAN
  4967. ScsiDiskIsDriveReady (
  4968. IN EFI_SCSI_SENSE_DATA *SenseData,
  4969. IN UINTN SenseCounts,
  4970. OUT BOOLEAN *RetryLater
  4971. )
  4972. {
  4973. EFI_SCSI_SENSE_DATA *SensePtr;
  4974. UINTN Index;
  4975. BOOLEAN IsReady;
  4976. IsReady = TRUE;
  4977. *RetryLater = FALSE;
  4978. SensePtr = SenseData;
  4979. for (Index = 0; Index < SenseCounts; Index++) {
  4980. switch (SensePtr->Sense_Key) {
  4981. case EFI_SCSI_SK_NOT_READY:
  4982. //
  4983. // Sense Key is EFI_SCSI_SK_NOT_READY (0x2)
  4984. //
  4985. switch (SensePtr->Addnl_Sense_Code) {
  4986. case EFI_SCSI_ASC_NOT_READY:
  4987. //
  4988. // Additional Sense Code is EFI_SCSI_ASC_NOT_READY (0x4)
  4989. //
  4990. switch (SensePtr->Addnl_Sense_Code_Qualifier) {
  4991. case EFI_SCSI_ASCQ_IN_PROGRESS:
  4992. //
  4993. // Additional Sense Code Qualifier is
  4994. // EFI_SCSI_ASCQ_IN_PROGRESS (0x1)
  4995. //
  4996. IsReady = FALSE;
  4997. *RetryLater = TRUE;
  4998. break;
  4999. default:
  5000. IsReady = FALSE;
  5001. *RetryLater = FALSE;
  5002. break;
  5003. }
  5004. break;
  5005. default:
  5006. break;
  5007. }
  5008. break;
  5009. default:
  5010. break;
  5011. }
  5012. SensePtr++;
  5013. }
  5014. return IsReady;
  5015. }
  5016. /**
  5017. Check sense key to find if it has sense key.
  5018. @param SenseData - The pointer of EFI_SCSI_SENSE_DATA
  5019. @param SenseCounts - The number of sense key
  5020. @retval TRUE It has sense key.
  5021. @retval FALSE It has NOT any sense key.
  5022. **/
  5023. BOOLEAN
  5024. ScsiDiskHaveSenseKey (
  5025. IN EFI_SCSI_SENSE_DATA *SenseData,
  5026. IN UINTN SenseCounts
  5027. )
  5028. {
  5029. EFI_SCSI_SENSE_DATA *SensePtr;
  5030. UINTN Index;
  5031. BOOLEAN HaveSenseKey;
  5032. if (SenseCounts == 0) {
  5033. HaveSenseKey = FALSE;
  5034. } else {
  5035. HaveSenseKey = TRUE;
  5036. }
  5037. SensePtr = SenseData;
  5038. for (Index = 0; Index < SenseCounts; Index++) {
  5039. //
  5040. // Sense Key is SK_NO_SENSE (0x0)
  5041. //
  5042. if ((SensePtr->Sense_Key == EFI_SCSI_SK_NO_SENSE) &&
  5043. (Index == 0))
  5044. {
  5045. HaveSenseKey = FALSE;
  5046. }
  5047. SensePtr++;
  5048. }
  5049. return HaveSenseKey;
  5050. }
  5051. /**
  5052. Release resource about disk device.
  5053. @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
  5054. **/
  5055. VOID
  5056. ReleaseScsiDiskDeviceResources (
  5057. IN SCSI_DISK_DEV *ScsiDiskDevice
  5058. )
  5059. {
  5060. if (ScsiDiskDevice == NULL) {
  5061. return;
  5062. }
  5063. if (ScsiDiskDevice->SenseData != NULL) {
  5064. FreePool (ScsiDiskDevice->SenseData);
  5065. ScsiDiskDevice->SenseData = NULL;
  5066. }
  5067. if (ScsiDiskDevice->ControllerNameTable != NULL) {
  5068. FreeUnicodeStringTable (ScsiDiskDevice->ControllerNameTable);
  5069. ScsiDiskDevice->ControllerNameTable = NULL;
  5070. }
  5071. FreePool (ScsiDiskDevice);
  5072. ScsiDiskDevice = NULL;
  5073. }
  5074. /**
  5075. Determine if Block Io & Block Io2 should be produced.
  5076. @param ChildHandle Child Handle to retrieve Parent information.
  5077. @retval TRUE Should produce Block Io & Block Io2.
  5078. @retval FALSE Should not produce Block Io & Block Io2.
  5079. **/
  5080. BOOLEAN
  5081. DetermineInstallBlockIo (
  5082. IN EFI_HANDLE ChildHandle
  5083. )
  5084. {
  5085. EFI_SCSI_PASS_THRU_PROTOCOL *ScsiPassThru;
  5086. EFI_EXT_SCSI_PASS_THRU_PROTOCOL *ExtScsiPassThru;
  5087. //
  5088. // Firstly, check if ExtScsiPassThru Protocol parent handle exists. If existence,
  5089. // check its attribute, logic or physical.
  5090. //
  5091. ExtScsiPassThru = (EFI_EXT_SCSI_PASS_THRU_PROTOCOL *)GetParentProtocol (&gEfiExtScsiPassThruProtocolGuid, ChildHandle);
  5092. if (ExtScsiPassThru != NULL) {
  5093. if ((ExtScsiPassThru->Mode->Attributes & EFI_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL) != 0) {
  5094. return TRUE;
  5095. }
  5096. }
  5097. //
  5098. // Secondly, check if ScsiPassThru Protocol parent handle exists. If existence,
  5099. // check its attribute, logic or physical.
  5100. //
  5101. ScsiPassThru = (EFI_SCSI_PASS_THRU_PROTOCOL *)GetParentProtocol (&gEfiScsiPassThruProtocolGuid, ChildHandle);
  5102. if (ScsiPassThru != NULL) {
  5103. if ((ScsiPassThru->Mode->Attributes & EFI_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL) != 0) {
  5104. return TRUE;
  5105. }
  5106. }
  5107. return FALSE;
  5108. }
  5109. /**
  5110. Search protocol database and check to see if the protocol
  5111. specified by ProtocolGuid is present on a ControllerHandle and opened by
  5112. ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
  5113. If the ControllerHandle is found, then the protocol specified by ProtocolGuid
  5114. will be opened on it.
  5115. @param ProtocolGuid ProtocolGuid pointer.
  5116. @param ChildHandle Child Handle to retrieve Parent information.
  5117. **/
  5118. VOID *
  5119. EFIAPI
  5120. GetParentProtocol (
  5121. IN EFI_GUID *ProtocolGuid,
  5122. IN EFI_HANDLE ChildHandle
  5123. )
  5124. {
  5125. UINTN Index;
  5126. UINTN HandleCount;
  5127. VOID *Interface;
  5128. EFI_STATUS Status;
  5129. EFI_HANDLE *HandleBuffer;
  5130. //
  5131. // Retrieve the list of all handles from the handle database
  5132. //
  5133. Status = gBS->LocateHandleBuffer (
  5134. ByProtocol,
  5135. ProtocolGuid,
  5136. NULL,
  5137. &HandleCount,
  5138. &HandleBuffer
  5139. );
  5140. if (EFI_ERROR (Status)) {
  5141. return NULL;
  5142. }
  5143. //
  5144. // Iterate to find who is parent handle that is opened with ProtocolGuid by ChildHandle
  5145. //
  5146. for (Index = 0; Index < HandleCount; Index++) {
  5147. Status = EfiTestChildHandle (HandleBuffer[Index], ChildHandle, ProtocolGuid);
  5148. if (!EFI_ERROR (Status)) {
  5149. Status = gBS->HandleProtocol (HandleBuffer[Index], ProtocolGuid, (VOID **)&Interface);
  5150. if (!EFI_ERROR (Status)) {
  5151. gBS->FreePool (HandleBuffer);
  5152. return Interface;
  5153. }
  5154. }
  5155. }
  5156. gBS->FreePool (HandleBuffer);
  5157. return NULL;
  5158. }
  5159. /**
  5160. Determine if EFI Erase Block Protocol should be produced.
  5161. @param ScsiDiskDevice The pointer of SCSI_DISK_DEV.
  5162. @param ChildHandle Handle of device.
  5163. @retval TRUE Should produce EFI Erase Block Protocol.
  5164. @retval FALSE Should not produce EFI Erase Block Protocol.
  5165. **/
  5166. BOOLEAN
  5167. DetermineInstallEraseBlock (
  5168. IN SCSI_DISK_DEV *ScsiDiskDevice,
  5169. IN EFI_HANDLE ChildHandle
  5170. )
  5171. {
  5172. UINT8 HostAdapterStatus;
  5173. UINT8 TargetStatus;
  5174. EFI_STATUS CommandStatus;
  5175. EFI_STATUS Status;
  5176. BOOLEAN UfsDevice;
  5177. BOOLEAN RetVal;
  5178. EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;
  5179. UINT8 SenseDataLength;
  5180. UINT32 DataLength16;
  5181. EFI_SCSI_DISK_CAPACITY_DATA16 *CapacityData16;
  5182. UfsDevice = FALSE;
  5183. RetVal = TRUE;
  5184. CapacityData16 = NULL;
  5185. //
  5186. // UNMAP command is not supported by any of the UFS WLUNs.
  5187. //
  5188. if (ScsiDiskDevice->DeviceType == EFI_SCSI_TYPE_WLUN) {
  5189. RetVal = FALSE;
  5190. goto Done;
  5191. }
  5192. Status = gBS->HandleProtocol (
  5193. ChildHandle,
  5194. &gEfiDevicePathProtocolGuid,
  5195. (VOID **)&DevicePathNode
  5196. );
  5197. //
  5198. // Device Path protocol must be installed on the device handle.
  5199. //
  5200. ASSERT_EFI_ERROR (Status);
  5201. while (!IsDevicePathEndType (DevicePathNode)) {
  5202. //
  5203. // For now, only support Erase Block Protocol on UFS devices.
  5204. //
  5205. if ((DevicePathNode->Type == MESSAGING_DEVICE_PATH) &&
  5206. (DevicePathNode->SubType == MSG_UFS_DP))
  5207. {
  5208. UfsDevice = TRUE;
  5209. break;
  5210. }
  5211. DevicePathNode = NextDevicePathNode (DevicePathNode);
  5212. }
  5213. if (!UfsDevice) {
  5214. RetVal = FALSE;
  5215. goto Done;
  5216. }
  5217. //
  5218. // Check whether the erase functionality is enabled on the UFS device.
  5219. //
  5220. CapacityData16 = AllocateAlignedBuffer (ScsiDiskDevice, sizeof (EFI_SCSI_DISK_CAPACITY_DATA16));
  5221. if (CapacityData16 == NULL) {
  5222. RetVal = FALSE;
  5223. goto Done;
  5224. }
  5225. SenseDataLength = 0;
  5226. DataLength16 = sizeof (EFI_SCSI_DISK_CAPACITY_DATA16);
  5227. ZeroMem (CapacityData16, sizeof (EFI_SCSI_DISK_CAPACITY_DATA16));
  5228. CommandStatus = ScsiReadCapacity16Command (
  5229. ScsiDiskDevice->ScsiIo,
  5230. SCSI_DISK_TIMEOUT,
  5231. NULL,
  5232. &SenseDataLength,
  5233. &HostAdapterStatus,
  5234. &TargetStatus,
  5235. (VOID *)CapacityData16,
  5236. &DataLength16,
  5237. FALSE
  5238. );
  5239. if (CommandStatus == EFI_SUCCESS) {
  5240. //
  5241. // Universal Flash Storage (UFS) Version 2.0
  5242. // Section 11.3.9.2
  5243. // Bits TPE and TPRZ should both be set to enable the erase feature on UFS.
  5244. //
  5245. if (((CapacityData16->LowestAlignLogic2 & BIT7) == 0) ||
  5246. ((CapacityData16->LowestAlignLogic2 & BIT6) == 0))
  5247. {
  5248. DEBUG ((
  5249. DEBUG_VERBOSE,
  5250. "ScsiDisk EraseBlock: Either TPE or TPRZ is not set: 0x%x.\n",
  5251. CapacityData16->LowestAlignLogic2
  5252. ));
  5253. RetVal = FALSE;
  5254. goto Done;
  5255. }
  5256. } else {
  5257. DEBUG ((
  5258. DEBUG_VERBOSE,
  5259. "ScsiDisk EraseBlock: ReadCapacity16 failed with status %r.\n",
  5260. CommandStatus
  5261. ));
  5262. RetVal = FALSE;
  5263. goto Done;
  5264. }
  5265. //
  5266. // Check whether the UFS device server implements the UNMAP command.
  5267. //
  5268. if ((ScsiDiskDevice->UnmapInfo.MaxLbaCnt == 0) ||
  5269. (ScsiDiskDevice->UnmapInfo.MaxBlkDespCnt == 0))
  5270. {
  5271. DEBUG ((
  5272. DEBUG_VERBOSE,
  5273. "ScsiDisk EraseBlock: The device server does not implement the UNMAP command.\n"
  5274. ));
  5275. RetVal = FALSE;
  5276. goto Done;
  5277. }
  5278. Done:
  5279. if (CapacityData16 != NULL) {
  5280. FreeAlignedBuffer (CapacityData16, sizeof (EFI_SCSI_DISK_CAPACITY_DATA16));
  5281. }
  5282. return RetVal;
  5283. }
  5284. /**
  5285. Determine if EFI Storage Security Command Protocol should be produced.
  5286. @param ScsiDiskDevice The pointer of SCSI_DISK_DEV.
  5287. @param ChildHandle Handle of device.
  5288. @retval TRUE Should produce EFI Storage Security Command Protocol.
  5289. @retval FALSE Should not produce EFI Storage Security Command Protocol.
  5290. **/
  5291. BOOLEAN
  5292. DetermineInstallStorageSecurity (
  5293. IN SCSI_DISK_DEV *ScsiDiskDevice,
  5294. IN EFI_HANDLE ChildHandle
  5295. )
  5296. {
  5297. EFI_STATUS Status;
  5298. UFS_DEVICE_PATH *UfsDevice;
  5299. BOOLEAN RetVal;
  5300. EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;
  5301. UfsDevice = NULL;
  5302. RetVal = TRUE;
  5303. Status = gBS->HandleProtocol (
  5304. ChildHandle,
  5305. &gEfiDevicePathProtocolGuid,
  5306. (VOID **)&DevicePathNode
  5307. );
  5308. //
  5309. // Device Path protocol must be installed on the device handle.
  5310. //
  5311. ASSERT_EFI_ERROR (Status);
  5312. while (!IsDevicePathEndType (DevicePathNode)) {
  5313. //
  5314. // For now, only support Storage Security Command Protocol on UFS devices.
  5315. //
  5316. if ((DevicePathNode->Type == MESSAGING_DEVICE_PATH) &&
  5317. (DevicePathNode->SubType == MSG_UFS_DP))
  5318. {
  5319. UfsDevice = (UFS_DEVICE_PATH *)DevicePathNode;
  5320. break;
  5321. }
  5322. DevicePathNode = NextDevicePathNode (DevicePathNode);
  5323. }
  5324. if (UfsDevice == NULL) {
  5325. RetVal = FALSE;
  5326. goto Done;
  5327. }
  5328. if (UfsDevice->Lun != UFS_WLUN_RPMB) {
  5329. RetVal = FALSE;
  5330. }
  5331. Done:
  5332. return RetVal;
  5333. }
  5334. /**
  5335. Provides inquiry information for the controller type.
  5336. This function is used by the IDE bus driver to get inquiry data. Data format
  5337. of Identify data is defined by the Interface GUID.
  5338. @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
  5339. @param[in, out] InquiryData Pointer to a buffer for the inquiry data.
  5340. @param[in, out] InquiryDataSize Pointer to the value for the inquiry data size.
  5341. @retval EFI_SUCCESS The command was accepted without any errors.
  5342. @retval EFI_NOT_FOUND Device does not support this data class
  5343. @retval EFI_DEVICE_ERROR Error reading InquiryData from device
  5344. @retval EFI_BUFFER_TOO_SMALL InquiryDataSize not big enough
  5345. **/
  5346. EFI_STATUS
  5347. EFIAPI
  5348. ScsiDiskInfoInquiry (
  5349. IN EFI_DISK_INFO_PROTOCOL *This,
  5350. IN OUT VOID *InquiryData,
  5351. IN OUT UINT32 *InquiryDataSize
  5352. )
  5353. {
  5354. EFI_STATUS Status;
  5355. SCSI_DISK_DEV *ScsiDiskDevice;
  5356. ScsiDiskDevice = SCSI_DISK_DEV_FROM_DISKINFO (This);
  5357. Status = EFI_BUFFER_TOO_SMALL;
  5358. if (*InquiryDataSize >= sizeof (ScsiDiskDevice->InquiryData)) {
  5359. Status = EFI_SUCCESS;
  5360. CopyMem (InquiryData, &ScsiDiskDevice->InquiryData, sizeof (ScsiDiskDevice->InquiryData));
  5361. }
  5362. *InquiryDataSize = sizeof (ScsiDiskDevice->InquiryData);
  5363. return Status;
  5364. }
  5365. /**
  5366. Provides identify information for the controller type.
  5367. This function is used by the IDE bus driver to get identify data. Data format
  5368. of Identify data is defined by the Interface GUID.
  5369. @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL
  5370. instance.
  5371. @param[in, out] IdentifyData Pointer to a buffer for the identify data.
  5372. @param[in, out] IdentifyDataSize Pointer to the value for the identify data
  5373. size.
  5374. @retval EFI_SUCCESS The command was accepted without any errors.
  5375. @retval EFI_NOT_FOUND Device does not support this data class
  5376. @retval EFI_DEVICE_ERROR Error reading IdentifyData from device
  5377. @retval EFI_BUFFER_TOO_SMALL IdentifyDataSize not big enough
  5378. **/
  5379. EFI_STATUS
  5380. EFIAPI
  5381. ScsiDiskInfoIdentify (
  5382. IN EFI_DISK_INFO_PROTOCOL *This,
  5383. IN OUT VOID *IdentifyData,
  5384. IN OUT UINT32 *IdentifyDataSize
  5385. )
  5386. {
  5387. EFI_STATUS Status;
  5388. SCSI_DISK_DEV *ScsiDiskDevice;
  5389. if (CompareGuid (&This->Interface, &gEfiDiskInfoScsiInterfaceGuid) || CompareGuid (&This->Interface, &gEfiDiskInfoUfsInterfaceGuid)) {
  5390. //
  5391. // Physical SCSI bus does not support this data class.
  5392. //
  5393. return EFI_NOT_FOUND;
  5394. }
  5395. ScsiDiskDevice = SCSI_DISK_DEV_FROM_DISKINFO (This);
  5396. Status = EFI_BUFFER_TOO_SMALL;
  5397. if (*IdentifyDataSize >= sizeof (ScsiDiskDevice->IdentifyData)) {
  5398. Status = EFI_SUCCESS;
  5399. CopyMem (IdentifyData, &ScsiDiskDevice->IdentifyData, sizeof (ScsiDiskDevice->IdentifyData));
  5400. }
  5401. *IdentifyDataSize = sizeof (ScsiDiskDevice->IdentifyData);
  5402. return Status;
  5403. }
  5404. /**
  5405. Provides sense data information for the controller type.
  5406. This function is used by the IDE bus driver to get sense data.
  5407. Data format of Sense data is defined by the Interface GUID.
  5408. @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
  5409. @param[in, out] SenseData Pointer to the SenseData.
  5410. @param[in, out] SenseDataSize Size of SenseData in bytes.
  5411. @param[out] SenseDataNumber Pointer to the value for the sense data size.
  5412. @retval EFI_SUCCESS The command was accepted without any errors.
  5413. @retval EFI_NOT_FOUND Device does not support this data class.
  5414. @retval EFI_DEVICE_ERROR Error reading SenseData from device.
  5415. @retval EFI_BUFFER_TOO_SMALL SenseDataSize not big enough.
  5416. **/
  5417. EFI_STATUS
  5418. EFIAPI
  5419. ScsiDiskInfoSenseData (
  5420. IN EFI_DISK_INFO_PROTOCOL *This,
  5421. IN OUT VOID *SenseData,
  5422. IN OUT UINT32 *SenseDataSize,
  5423. OUT UINT8 *SenseDataNumber
  5424. )
  5425. {
  5426. return EFI_NOT_FOUND;
  5427. }
  5428. /**
  5429. This function is used by the IDE bus driver to get controller information.
  5430. @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
  5431. @param[out] IdeChannel Pointer to the Ide Channel number. Primary or secondary.
  5432. @param[out] IdeDevice Pointer to the Ide Device number. Master or slave.
  5433. @retval EFI_SUCCESS IdeChannel and IdeDevice are valid.
  5434. @retval EFI_UNSUPPORTED This is not an IDE device.
  5435. **/
  5436. EFI_STATUS
  5437. EFIAPI
  5438. ScsiDiskInfoWhichIde (
  5439. IN EFI_DISK_INFO_PROTOCOL *This,
  5440. OUT UINT32 *IdeChannel,
  5441. OUT UINT32 *IdeDevice
  5442. )
  5443. {
  5444. SCSI_DISK_DEV *ScsiDiskDevice;
  5445. if (CompareGuid (&This->Interface, &gEfiDiskInfoScsiInterfaceGuid) || CompareGuid (&This->Interface, &gEfiDiskInfoUfsInterfaceGuid)) {
  5446. //
  5447. // This is not an IDE physical device.
  5448. //
  5449. return EFI_UNSUPPORTED;
  5450. }
  5451. ScsiDiskDevice = SCSI_DISK_DEV_FROM_DISKINFO (This);
  5452. *IdeChannel = ScsiDiskDevice->Channel;
  5453. *IdeDevice = ScsiDiskDevice->Device;
  5454. return EFI_SUCCESS;
  5455. }
  5456. /**
  5457. Issues ATA IDENTIFY DEVICE command to identify ATAPI device.
  5458. This function tries to fill 512-byte ATAPI_IDENTIFY_DATA for ATAPI device to
  5459. implement Identify() interface for DiskInfo protocol. The ATA command is sent
  5460. via SCSI Request Packet.
  5461. @param ScsiDiskDevice The pointer of SCSI_DISK_DEV
  5462. @retval EFI_SUCCESS The ATAPI device identify data were retrieved successfully.
  5463. @retval others Some error occurred during the identification that ATAPI device.
  5464. **/
  5465. EFI_STATUS
  5466. AtapiIdentifyDevice (
  5467. IN OUT SCSI_DISK_DEV *ScsiDiskDevice
  5468. )
  5469. {
  5470. EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket;
  5471. UINT8 Cdb[6];
  5472. //
  5473. // Initialize SCSI REQUEST_PACKET and 6-byte Cdb
  5474. //
  5475. ZeroMem (&CommandPacket, sizeof (CommandPacket));
  5476. ZeroMem (Cdb, sizeof (Cdb));
  5477. Cdb[0] = ATA_CMD_IDENTIFY_DEVICE;
  5478. CommandPacket.Timeout = SCSI_DISK_TIMEOUT;
  5479. CommandPacket.Cdb = Cdb;
  5480. CommandPacket.CdbLength = (UINT8)sizeof (Cdb);
  5481. CommandPacket.InDataBuffer = &ScsiDiskDevice->IdentifyData;
  5482. CommandPacket.InTransferLength = sizeof (ScsiDiskDevice->IdentifyData);
  5483. return ScsiDiskDevice->ScsiIo->ExecuteScsiCommand (ScsiDiskDevice->ScsiIo, &CommandPacket, NULL);
  5484. }
  5485. /**
  5486. Initialize the installation of DiskInfo protocol.
  5487. This function prepares for the installation of DiskInfo protocol on the child handle.
  5488. By default, it installs DiskInfo protocol with SCSI interface GUID. If it further
  5489. detects that the physical device is an ATAPI/AHCI device, it then updates interface GUID
  5490. to be IDE/AHCI interface GUID.
  5491. @param ScsiDiskDevice The pointer of SCSI_DISK_DEV.
  5492. @param ChildHandle Child handle to install DiskInfo protocol.
  5493. **/
  5494. VOID
  5495. InitializeInstallDiskInfo (
  5496. IN SCSI_DISK_DEV *ScsiDiskDevice,
  5497. IN EFI_HANDLE ChildHandle
  5498. )
  5499. {
  5500. EFI_STATUS Status;
  5501. EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;
  5502. EFI_DEVICE_PATH_PROTOCOL *ChildDevicePathNode;
  5503. ATAPI_DEVICE_PATH *AtapiDevicePath;
  5504. SATA_DEVICE_PATH *SataDevicePath;
  5505. UINTN IdentifyRetry;
  5506. Status = gBS->HandleProtocol (ChildHandle, &gEfiDevicePathProtocolGuid, (VOID **)&DevicePathNode);
  5507. //
  5508. // Device Path protocol must be installed on the device handle.
  5509. //
  5510. ASSERT_EFI_ERROR (Status);
  5511. //
  5512. // Copy the DiskInfo protocol template.
  5513. //
  5514. CopyMem (&ScsiDiskDevice->DiskInfo, &gScsiDiskInfoProtocolTemplate, sizeof (gScsiDiskInfoProtocolTemplate));
  5515. while (!IsDevicePathEnd (DevicePathNode)) {
  5516. ChildDevicePathNode = NextDevicePathNode (DevicePathNode);
  5517. if ((DevicePathType (DevicePathNode) == HARDWARE_DEVICE_PATH) &&
  5518. (DevicePathSubType (DevicePathNode) == HW_PCI_DP) &&
  5519. (DevicePathType (ChildDevicePathNode) == MESSAGING_DEVICE_PATH) &&
  5520. ((DevicePathSubType (ChildDevicePathNode) == MSG_ATAPI_DP) ||
  5521. (DevicePathSubType (ChildDevicePathNode) == MSG_SATA_DP)))
  5522. {
  5523. IdentifyRetry = 3;
  5524. do {
  5525. //
  5526. // Issue ATA Identify Device Command via SCSI command, which is required to publish DiskInfo protocol
  5527. // with IDE/AHCI interface GUID.
  5528. //
  5529. Status = AtapiIdentifyDevice (ScsiDiskDevice);
  5530. if (!EFI_ERROR (Status)) {
  5531. if (DevicePathSubType (ChildDevicePathNode) == MSG_ATAPI_DP) {
  5532. //
  5533. // We find the valid ATAPI device path
  5534. //
  5535. AtapiDevicePath = (ATAPI_DEVICE_PATH *)ChildDevicePathNode;
  5536. ScsiDiskDevice->Channel = AtapiDevicePath->PrimarySecondary;
  5537. ScsiDiskDevice->Device = AtapiDevicePath->SlaveMaster;
  5538. //
  5539. // Update the DiskInfo.Interface to IDE interface GUID for the physical ATAPI device.
  5540. //
  5541. CopyGuid (&ScsiDiskDevice->DiskInfo.Interface, &gEfiDiskInfoIdeInterfaceGuid);
  5542. } else {
  5543. //
  5544. // We find the valid SATA device path
  5545. //
  5546. SataDevicePath = (SATA_DEVICE_PATH *)ChildDevicePathNode;
  5547. ScsiDiskDevice->Channel = SataDevicePath->HBAPortNumber;
  5548. ScsiDiskDevice->Device = SataDevicePath->PortMultiplierPortNumber;
  5549. //
  5550. // Update the DiskInfo.Interface to AHCI interface GUID for the physical AHCI device.
  5551. //
  5552. CopyGuid (&ScsiDiskDevice->DiskInfo.Interface, &gEfiDiskInfoAhciInterfaceGuid);
  5553. }
  5554. return;
  5555. }
  5556. } while (--IdentifyRetry > 0);
  5557. } else if ((DevicePathType (ChildDevicePathNode) == MESSAGING_DEVICE_PATH) &&
  5558. (DevicePathSubType (ChildDevicePathNode) == MSG_UFS_DP))
  5559. {
  5560. CopyGuid (&ScsiDiskDevice->DiskInfo.Interface, &gEfiDiskInfoUfsInterfaceGuid);
  5561. break;
  5562. }
  5563. DevicePathNode = ChildDevicePathNode;
  5564. }
  5565. return;
  5566. }