libata-core.c 190 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409
  1. /*
  2. * libata-core.c - helper library for ATA
  3. *
  4. * Maintained by: Tejun Heo <tj@kernel.org>
  5. * Please ALWAYS copy linux-ide@vger.kernel.org
  6. * on emails.
  7. *
  8. * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
  9. * Copyright 2003-2004 Jeff Garzik
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; see the file COPYING. If not, write to
  24. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. *
  27. * libata documentation is available via 'make {ps|pdf}docs',
  28. * as Documentation/driver-api/libata.rst
  29. *
  30. * Hardware documentation available from http://www.t13.org/ and
  31. * http://www.sata-io.org/
  32. *
  33. * Standards documents from:
  34. * http://www.t13.org (ATA standards, PCI DMA IDE spec)
  35. * http://www.t10.org (SCSI MMC - for ATAPI MMC)
  36. * http://www.sata-io.org (SATA)
  37. * http://www.compactflash.org (CF)
  38. * http://www.qic.org (QIC157 - Tape and DSC)
  39. * http://www.ce-ata.org (CE-ATA: not supported)
  40. *
  41. */
  42. #include <linux/kernel.h>
  43. #include <linux/module.h>
  44. #include <linux/pci.h>
  45. #include <linux/init.h>
  46. #include <linux/list.h>
  47. #include <linux/mm.h>
  48. #include <linux/spinlock.h>
  49. #include <linux/blkdev.h>
  50. #include <linux/delay.h>
  51. #include <linux/timer.h>
  52. #include <linux/time.h>
  53. #include <linux/interrupt.h>
  54. #include <linux/completion.h>
  55. #include <linux/suspend.h>
  56. #include <linux/workqueue.h>
  57. #include <linux/scatterlist.h>
  58. #include <linux/io.h>
  59. #include <linux/async.h>
  60. #include <linux/log2.h>
  61. #include <linux/slab.h>
  62. #include <linux/glob.h>
  63. #include <scsi/scsi.h>
  64. #include <scsi/scsi_cmnd.h>
  65. #include <scsi/scsi_host.h>
  66. #include <linux/libata.h>
  67. #include <asm/byteorder.h>
  68. #include <asm/unaligned.h>
  69. #include <linux/cdrom.h>
  70. #include <linux/ratelimit.h>
  71. #include <linux/leds.h>
  72. #include <linux/pm_runtime.h>
  73. #include <linux/platform_device.h>
  74. #define CREATE_TRACE_POINTS
  75. #include <trace/events/libata.h>
  76. #include "libata.h"
  77. #include "libata-transport.h"
  78. /* debounce timing parameters in msecs { interval, duration, timeout } */
  79. const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
  80. const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
  81. const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
  82. const struct ata_port_operations ata_base_port_ops = {
  83. .prereset = ata_std_prereset,
  84. .postreset = ata_std_postreset,
  85. .error_handler = ata_std_error_handler,
  86. .sched_eh = ata_std_sched_eh,
  87. .end_eh = ata_std_end_eh,
  88. };
  89. const struct ata_port_operations sata_port_ops = {
  90. .inherits = &ata_base_port_ops,
  91. .qc_defer = ata_std_qc_defer,
  92. .hardreset = sata_std_hardreset,
  93. };
  94. static unsigned int ata_dev_init_params(struct ata_device *dev,
  95. u16 heads, u16 sectors);
  96. static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
  97. static void ata_dev_xfermask(struct ata_device *dev);
  98. static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
  99. atomic_t ata_print_id = ATOMIC_INIT(0);
  100. struct ata_force_param {
  101. const char *name;
  102. unsigned int cbl;
  103. int spd_limit;
  104. unsigned long xfer_mask;
  105. unsigned int horkage_on;
  106. unsigned int horkage_off;
  107. unsigned int lflags;
  108. };
  109. struct ata_force_ent {
  110. int port;
  111. int device;
  112. struct ata_force_param param;
  113. };
  114. static struct ata_force_ent *ata_force_tbl;
  115. static int ata_force_tbl_size;
  116. static char ata_force_param_buf[PAGE_SIZE] __initdata;
  117. /* param_buf is thrown away after initialization, disallow read */
  118. module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
  119. MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/admin-guide/kernel-parameters.rst for details)");
  120. static int atapi_enabled = 1;
  121. module_param(atapi_enabled, int, 0444);
  122. MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
  123. static int atapi_dmadir = 0;
  124. module_param(atapi_dmadir, int, 0444);
  125. MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
  126. int atapi_passthru16 = 1;
  127. module_param(atapi_passthru16, int, 0444);
  128. MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
  129. int libata_fua = 0;
  130. module_param_named(fua, libata_fua, int, 0444);
  131. MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
  132. static int ata_ignore_hpa;
  133. module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
  134. MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
  135. static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
  136. module_param_named(dma, libata_dma_mask, int, 0444);
  137. MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
  138. static int ata_probe_timeout;
  139. module_param(ata_probe_timeout, int, 0444);
  140. MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
  141. int libata_noacpi = 0;
  142. module_param_named(noacpi, libata_noacpi, int, 0444);
  143. MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
  144. int libata_allow_tpm = 0;
  145. module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
  146. MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
  147. static int atapi_an;
  148. module_param(atapi_an, int, 0444);
  149. MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
  150. MODULE_AUTHOR("Jeff Garzik");
  151. MODULE_DESCRIPTION("Library module for ATA devices");
  152. MODULE_LICENSE("GPL");
  153. MODULE_VERSION(DRV_VERSION);
  154. static bool ata_sstatus_online(u32 sstatus)
  155. {
  156. return (sstatus & 0xf) == 0x3;
  157. }
  158. /**
  159. * ata_link_next - link iteration helper
  160. * @link: the previous link, NULL to start
  161. * @ap: ATA port containing links to iterate
  162. * @mode: iteration mode, one of ATA_LITER_*
  163. *
  164. * LOCKING:
  165. * Host lock or EH context.
  166. *
  167. * RETURNS:
  168. * Pointer to the next link.
  169. */
  170. struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
  171. enum ata_link_iter_mode mode)
  172. {
  173. BUG_ON(mode != ATA_LITER_EDGE &&
  174. mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
  175. /* NULL link indicates start of iteration */
  176. if (!link)
  177. switch (mode) {
  178. case ATA_LITER_EDGE:
  179. case ATA_LITER_PMP_FIRST:
  180. if (sata_pmp_attached(ap))
  181. return ap->pmp_link;
  182. /* fall through */
  183. case ATA_LITER_HOST_FIRST:
  184. return &ap->link;
  185. }
  186. /* we just iterated over the host link, what's next? */
  187. if (link == &ap->link)
  188. switch (mode) {
  189. case ATA_LITER_HOST_FIRST:
  190. if (sata_pmp_attached(ap))
  191. return ap->pmp_link;
  192. /* fall through */
  193. case ATA_LITER_PMP_FIRST:
  194. if (unlikely(ap->slave_link))
  195. return ap->slave_link;
  196. /* fall through */
  197. case ATA_LITER_EDGE:
  198. return NULL;
  199. }
  200. /* slave_link excludes PMP */
  201. if (unlikely(link == ap->slave_link))
  202. return NULL;
  203. /* we were over a PMP link */
  204. if (++link < ap->pmp_link + ap->nr_pmp_links)
  205. return link;
  206. if (mode == ATA_LITER_PMP_FIRST)
  207. return &ap->link;
  208. return NULL;
  209. }
  210. /**
  211. * ata_dev_next - device iteration helper
  212. * @dev: the previous device, NULL to start
  213. * @link: ATA link containing devices to iterate
  214. * @mode: iteration mode, one of ATA_DITER_*
  215. *
  216. * LOCKING:
  217. * Host lock or EH context.
  218. *
  219. * RETURNS:
  220. * Pointer to the next device.
  221. */
  222. struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
  223. enum ata_dev_iter_mode mode)
  224. {
  225. BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
  226. mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
  227. /* NULL dev indicates start of iteration */
  228. if (!dev)
  229. switch (mode) {
  230. case ATA_DITER_ENABLED:
  231. case ATA_DITER_ALL:
  232. dev = link->device;
  233. goto check;
  234. case ATA_DITER_ENABLED_REVERSE:
  235. case ATA_DITER_ALL_REVERSE:
  236. dev = link->device + ata_link_max_devices(link) - 1;
  237. goto check;
  238. }
  239. next:
  240. /* move to the next one */
  241. switch (mode) {
  242. case ATA_DITER_ENABLED:
  243. case ATA_DITER_ALL:
  244. if (++dev < link->device + ata_link_max_devices(link))
  245. goto check;
  246. return NULL;
  247. case ATA_DITER_ENABLED_REVERSE:
  248. case ATA_DITER_ALL_REVERSE:
  249. if (--dev >= link->device)
  250. goto check;
  251. return NULL;
  252. }
  253. check:
  254. if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
  255. !ata_dev_enabled(dev))
  256. goto next;
  257. return dev;
  258. }
  259. /**
  260. * ata_dev_phys_link - find physical link for a device
  261. * @dev: ATA device to look up physical link for
  262. *
  263. * Look up physical link which @dev is attached to. Note that
  264. * this is different from @dev->link only when @dev is on slave
  265. * link. For all other cases, it's the same as @dev->link.
  266. *
  267. * LOCKING:
  268. * Don't care.
  269. *
  270. * RETURNS:
  271. * Pointer to the found physical link.
  272. */
  273. struct ata_link *ata_dev_phys_link(struct ata_device *dev)
  274. {
  275. struct ata_port *ap = dev->link->ap;
  276. if (!ap->slave_link)
  277. return dev->link;
  278. if (!dev->devno)
  279. return &ap->link;
  280. return ap->slave_link;
  281. }
  282. /**
  283. * ata_force_cbl - force cable type according to libata.force
  284. * @ap: ATA port of interest
  285. *
  286. * Force cable type according to libata.force and whine about it.
  287. * The last entry which has matching port number is used, so it
  288. * can be specified as part of device force parameters. For
  289. * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
  290. * same effect.
  291. *
  292. * LOCKING:
  293. * EH context.
  294. */
  295. void ata_force_cbl(struct ata_port *ap)
  296. {
  297. int i;
  298. for (i = ata_force_tbl_size - 1; i >= 0; i--) {
  299. const struct ata_force_ent *fe = &ata_force_tbl[i];
  300. if (fe->port != -1 && fe->port != ap->print_id)
  301. continue;
  302. if (fe->param.cbl == ATA_CBL_NONE)
  303. continue;
  304. ap->cbl = fe->param.cbl;
  305. ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
  306. return;
  307. }
  308. }
  309. /**
  310. * ata_force_link_limits - force link limits according to libata.force
  311. * @link: ATA link of interest
  312. *
  313. * Force link flags and SATA spd limit according to libata.force
  314. * and whine about it. When only the port part is specified
  315. * (e.g. 1:), the limit applies to all links connected to both
  316. * the host link and all fan-out ports connected via PMP. If the
  317. * device part is specified as 0 (e.g. 1.00:), it specifies the
  318. * first fan-out link not the host link. Device number 15 always
  319. * points to the host link whether PMP is attached or not. If the
  320. * controller has slave link, device number 16 points to it.
  321. *
  322. * LOCKING:
  323. * EH context.
  324. */
  325. static void ata_force_link_limits(struct ata_link *link)
  326. {
  327. bool did_spd = false;
  328. int linkno = link->pmp;
  329. int i;
  330. if (ata_is_host_link(link))
  331. linkno += 15;
  332. for (i = ata_force_tbl_size - 1; i >= 0; i--) {
  333. const struct ata_force_ent *fe = &ata_force_tbl[i];
  334. if (fe->port != -1 && fe->port != link->ap->print_id)
  335. continue;
  336. if (fe->device != -1 && fe->device != linkno)
  337. continue;
  338. /* only honor the first spd limit */
  339. if (!did_spd && fe->param.spd_limit) {
  340. link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
  341. ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
  342. fe->param.name);
  343. did_spd = true;
  344. }
  345. /* let lflags stack */
  346. if (fe->param.lflags) {
  347. link->flags |= fe->param.lflags;
  348. ata_link_notice(link,
  349. "FORCE: link flag 0x%x forced -> 0x%x\n",
  350. fe->param.lflags, link->flags);
  351. }
  352. }
  353. }
  354. /**
  355. * ata_force_xfermask - force xfermask according to libata.force
  356. * @dev: ATA device of interest
  357. *
  358. * Force xfer_mask according to libata.force and whine about it.
  359. * For consistency with link selection, device number 15 selects
  360. * the first device connected to the host link.
  361. *
  362. * LOCKING:
  363. * EH context.
  364. */
  365. static void ata_force_xfermask(struct ata_device *dev)
  366. {
  367. int devno = dev->link->pmp + dev->devno;
  368. int alt_devno = devno;
  369. int i;
  370. /* allow n.15/16 for devices attached to host port */
  371. if (ata_is_host_link(dev->link))
  372. alt_devno += 15;
  373. for (i = ata_force_tbl_size - 1; i >= 0; i--) {
  374. const struct ata_force_ent *fe = &ata_force_tbl[i];
  375. unsigned long pio_mask, mwdma_mask, udma_mask;
  376. if (fe->port != -1 && fe->port != dev->link->ap->print_id)
  377. continue;
  378. if (fe->device != -1 && fe->device != devno &&
  379. fe->device != alt_devno)
  380. continue;
  381. if (!fe->param.xfer_mask)
  382. continue;
  383. ata_unpack_xfermask(fe->param.xfer_mask,
  384. &pio_mask, &mwdma_mask, &udma_mask);
  385. if (udma_mask)
  386. dev->udma_mask = udma_mask;
  387. else if (mwdma_mask) {
  388. dev->udma_mask = 0;
  389. dev->mwdma_mask = mwdma_mask;
  390. } else {
  391. dev->udma_mask = 0;
  392. dev->mwdma_mask = 0;
  393. dev->pio_mask = pio_mask;
  394. }
  395. ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n",
  396. fe->param.name);
  397. return;
  398. }
  399. }
  400. /**
  401. * ata_force_horkage - force horkage according to libata.force
  402. * @dev: ATA device of interest
  403. *
  404. * Force horkage according to libata.force and whine about it.
  405. * For consistency with link selection, device number 15 selects
  406. * the first device connected to the host link.
  407. *
  408. * LOCKING:
  409. * EH context.
  410. */
  411. static void ata_force_horkage(struct ata_device *dev)
  412. {
  413. int devno = dev->link->pmp + dev->devno;
  414. int alt_devno = devno;
  415. int i;
  416. /* allow n.15/16 for devices attached to host port */
  417. if (ata_is_host_link(dev->link))
  418. alt_devno += 15;
  419. for (i = 0; i < ata_force_tbl_size; i++) {
  420. const struct ata_force_ent *fe = &ata_force_tbl[i];
  421. if (fe->port != -1 && fe->port != dev->link->ap->print_id)
  422. continue;
  423. if (fe->device != -1 && fe->device != devno &&
  424. fe->device != alt_devno)
  425. continue;
  426. if (!(~dev->horkage & fe->param.horkage_on) &&
  427. !(dev->horkage & fe->param.horkage_off))
  428. continue;
  429. dev->horkage |= fe->param.horkage_on;
  430. dev->horkage &= ~fe->param.horkage_off;
  431. ata_dev_notice(dev, "FORCE: horkage modified (%s)\n",
  432. fe->param.name);
  433. }
  434. }
  435. /**
  436. * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
  437. * @opcode: SCSI opcode
  438. *
  439. * Determine ATAPI command type from @opcode.
  440. *
  441. * LOCKING:
  442. * None.
  443. *
  444. * RETURNS:
  445. * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
  446. */
  447. int atapi_cmd_type(u8 opcode)
  448. {
  449. switch (opcode) {
  450. case GPCMD_READ_10:
  451. case GPCMD_READ_12:
  452. return ATAPI_READ;
  453. case GPCMD_WRITE_10:
  454. case GPCMD_WRITE_12:
  455. case GPCMD_WRITE_AND_VERIFY_10:
  456. return ATAPI_WRITE;
  457. case GPCMD_READ_CD:
  458. case GPCMD_READ_CD_MSF:
  459. return ATAPI_READ_CD;
  460. case ATA_16:
  461. case ATA_12:
  462. if (atapi_passthru16)
  463. return ATAPI_PASS_THRU;
  464. /* fall thru */
  465. default:
  466. return ATAPI_MISC;
  467. }
  468. }
  469. /**
  470. * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
  471. * @tf: Taskfile to convert
  472. * @pmp: Port multiplier port
  473. * @is_cmd: This FIS is for command
  474. * @fis: Buffer into which data will output
  475. *
  476. * Converts a standard ATA taskfile to a Serial ATA
  477. * FIS structure (Register - Host to Device).
  478. *
  479. * LOCKING:
  480. * Inherited from caller.
  481. */
  482. void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
  483. {
  484. fis[0] = 0x27; /* Register - Host to Device FIS */
  485. fis[1] = pmp & 0xf; /* Port multiplier number*/
  486. if (is_cmd)
  487. fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
  488. fis[2] = tf->command;
  489. fis[3] = tf->feature;
  490. fis[4] = tf->lbal;
  491. fis[5] = tf->lbam;
  492. fis[6] = tf->lbah;
  493. fis[7] = tf->device;
  494. fis[8] = tf->hob_lbal;
  495. fis[9] = tf->hob_lbam;
  496. fis[10] = tf->hob_lbah;
  497. fis[11] = tf->hob_feature;
  498. fis[12] = tf->nsect;
  499. fis[13] = tf->hob_nsect;
  500. fis[14] = 0;
  501. fis[15] = tf->ctl;
  502. fis[16] = tf->auxiliary & 0xff;
  503. fis[17] = (tf->auxiliary >> 8) & 0xff;
  504. fis[18] = (tf->auxiliary >> 16) & 0xff;
  505. fis[19] = (tf->auxiliary >> 24) & 0xff;
  506. }
  507. /**
  508. * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
  509. * @fis: Buffer from which data will be input
  510. * @tf: Taskfile to output
  511. *
  512. * Converts a serial ATA FIS structure to a standard ATA taskfile.
  513. *
  514. * LOCKING:
  515. * Inherited from caller.
  516. */
  517. void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
  518. {
  519. tf->command = fis[2]; /* status */
  520. tf->feature = fis[3]; /* error */
  521. tf->lbal = fis[4];
  522. tf->lbam = fis[5];
  523. tf->lbah = fis[6];
  524. tf->device = fis[7];
  525. tf->hob_lbal = fis[8];
  526. tf->hob_lbam = fis[9];
  527. tf->hob_lbah = fis[10];
  528. tf->nsect = fis[12];
  529. tf->hob_nsect = fis[13];
  530. }
  531. static const u8 ata_rw_cmds[] = {
  532. /* pio multi */
  533. ATA_CMD_READ_MULTI,
  534. ATA_CMD_WRITE_MULTI,
  535. ATA_CMD_READ_MULTI_EXT,
  536. ATA_CMD_WRITE_MULTI_EXT,
  537. 0,
  538. 0,
  539. 0,
  540. ATA_CMD_WRITE_MULTI_FUA_EXT,
  541. /* pio */
  542. ATA_CMD_PIO_READ,
  543. ATA_CMD_PIO_WRITE,
  544. ATA_CMD_PIO_READ_EXT,
  545. ATA_CMD_PIO_WRITE_EXT,
  546. 0,
  547. 0,
  548. 0,
  549. 0,
  550. /* dma */
  551. ATA_CMD_READ,
  552. ATA_CMD_WRITE,
  553. ATA_CMD_READ_EXT,
  554. ATA_CMD_WRITE_EXT,
  555. 0,
  556. 0,
  557. 0,
  558. ATA_CMD_WRITE_FUA_EXT
  559. };
  560. /**
  561. * ata_rwcmd_protocol - set taskfile r/w commands and protocol
  562. * @tf: command to examine and configure
  563. * @dev: device tf belongs to
  564. *
  565. * Examine the device configuration and tf->flags to calculate
  566. * the proper read/write commands and protocol to use.
  567. *
  568. * LOCKING:
  569. * caller.
  570. */
  571. static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
  572. {
  573. u8 cmd;
  574. int index, fua, lba48, write;
  575. fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
  576. lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
  577. write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
  578. if (dev->flags & ATA_DFLAG_PIO) {
  579. tf->protocol = ATA_PROT_PIO;
  580. index = dev->multi_count ? 0 : 8;
  581. } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
  582. /* Unable to use DMA due to host limitation */
  583. tf->protocol = ATA_PROT_PIO;
  584. index = dev->multi_count ? 0 : 8;
  585. } else {
  586. tf->protocol = ATA_PROT_DMA;
  587. index = 16;
  588. }
  589. cmd = ata_rw_cmds[index + fua + lba48 + write];
  590. if (cmd) {
  591. tf->command = cmd;
  592. return 0;
  593. }
  594. return -1;
  595. }
  596. /**
  597. * ata_tf_read_block - Read block address from ATA taskfile
  598. * @tf: ATA taskfile of interest
  599. * @dev: ATA device @tf belongs to
  600. *
  601. * LOCKING:
  602. * None.
  603. *
  604. * Read block address from @tf. This function can handle all
  605. * three address formats - LBA, LBA48 and CHS. tf->protocol and
  606. * flags select the address format to use.
  607. *
  608. * RETURNS:
  609. * Block address read from @tf.
  610. */
  611. u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev)
  612. {
  613. u64 block = 0;
  614. if (tf->flags & ATA_TFLAG_LBA) {
  615. if (tf->flags & ATA_TFLAG_LBA48) {
  616. block |= (u64)tf->hob_lbah << 40;
  617. block |= (u64)tf->hob_lbam << 32;
  618. block |= (u64)tf->hob_lbal << 24;
  619. } else
  620. block |= (tf->device & 0xf) << 24;
  621. block |= tf->lbah << 16;
  622. block |= tf->lbam << 8;
  623. block |= tf->lbal;
  624. } else {
  625. u32 cyl, head, sect;
  626. cyl = tf->lbam | (tf->lbah << 8);
  627. head = tf->device & 0xf;
  628. sect = tf->lbal;
  629. if (!sect) {
  630. ata_dev_warn(dev,
  631. "device reported invalid CHS sector 0\n");
  632. return U64_MAX;
  633. }
  634. block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
  635. }
  636. return block;
  637. }
  638. /**
  639. * ata_build_rw_tf - Build ATA taskfile for given read/write request
  640. * @tf: Target ATA taskfile
  641. * @dev: ATA device @tf belongs to
  642. * @block: Block address
  643. * @n_block: Number of blocks
  644. * @tf_flags: RW/FUA etc...
  645. * @tag: tag
  646. * @class: IO priority class
  647. *
  648. * LOCKING:
  649. * None.
  650. *
  651. * Build ATA taskfile @tf for read/write request described by
  652. * @block, @n_block, @tf_flags and @tag on @dev.
  653. *
  654. * RETURNS:
  655. *
  656. * 0 on success, -ERANGE if the request is too large for @dev,
  657. * -EINVAL if the request is invalid.
  658. */
  659. int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
  660. u64 block, u32 n_block, unsigned int tf_flags,
  661. unsigned int tag, int class)
  662. {
  663. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  664. tf->flags |= tf_flags;
  665. if (ata_ncq_enabled(dev) && !ata_tag_internal(tag)) {
  666. /* yay, NCQ */
  667. if (!lba_48_ok(block, n_block))
  668. return -ERANGE;
  669. tf->protocol = ATA_PROT_NCQ;
  670. tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
  671. if (tf->flags & ATA_TFLAG_WRITE)
  672. tf->command = ATA_CMD_FPDMA_WRITE;
  673. else
  674. tf->command = ATA_CMD_FPDMA_READ;
  675. tf->nsect = tag << 3;
  676. tf->hob_feature = (n_block >> 8) & 0xff;
  677. tf->feature = n_block & 0xff;
  678. tf->hob_lbah = (block >> 40) & 0xff;
  679. tf->hob_lbam = (block >> 32) & 0xff;
  680. tf->hob_lbal = (block >> 24) & 0xff;
  681. tf->lbah = (block >> 16) & 0xff;
  682. tf->lbam = (block >> 8) & 0xff;
  683. tf->lbal = block & 0xff;
  684. tf->device = ATA_LBA;
  685. if (tf->flags & ATA_TFLAG_FUA)
  686. tf->device |= 1 << 7;
  687. if (dev->flags & ATA_DFLAG_NCQ_PRIO) {
  688. if (class == IOPRIO_CLASS_RT)
  689. tf->hob_nsect |= ATA_PRIO_HIGH <<
  690. ATA_SHIFT_PRIO;
  691. }
  692. } else if (dev->flags & ATA_DFLAG_LBA) {
  693. tf->flags |= ATA_TFLAG_LBA;
  694. if (lba_28_ok(block, n_block)) {
  695. /* use LBA28 */
  696. tf->device |= (block >> 24) & 0xf;
  697. } else if (lba_48_ok(block, n_block)) {
  698. if (!(dev->flags & ATA_DFLAG_LBA48))
  699. return -ERANGE;
  700. /* use LBA48 */
  701. tf->flags |= ATA_TFLAG_LBA48;
  702. tf->hob_nsect = (n_block >> 8) & 0xff;
  703. tf->hob_lbah = (block >> 40) & 0xff;
  704. tf->hob_lbam = (block >> 32) & 0xff;
  705. tf->hob_lbal = (block >> 24) & 0xff;
  706. } else
  707. /* request too large even for LBA48 */
  708. return -ERANGE;
  709. if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
  710. return -EINVAL;
  711. tf->nsect = n_block & 0xff;
  712. tf->lbah = (block >> 16) & 0xff;
  713. tf->lbam = (block >> 8) & 0xff;
  714. tf->lbal = block & 0xff;
  715. tf->device |= ATA_LBA;
  716. } else {
  717. /* CHS */
  718. u32 sect, head, cyl, track;
  719. /* The request -may- be too large for CHS addressing. */
  720. if (!lba_28_ok(block, n_block))
  721. return -ERANGE;
  722. if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
  723. return -EINVAL;
  724. /* Convert LBA to CHS */
  725. track = (u32)block / dev->sectors;
  726. cyl = track / dev->heads;
  727. head = track % dev->heads;
  728. sect = (u32)block % dev->sectors + 1;
  729. DPRINTK("block %u track %u cyl %u head %u sect %u\n",
  730. (u32)block, track, cyl, head, sect);
  731. /* Check whether the converted CHS can fit.
  732. Cylinder: 0-65535
  733. Head: 0-15
  734. Sector: 1-255*/
  735. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  736. return -ERANGE;
  737. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  738. tf->lbal = sect;
  739. tf->lbam = cyl;
  740. tf->lbah = cyl >> 8;
  741. tf->device |= head;
  742. }
  743. return 0;
  744. }
  745. /**
  746. * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
  747. * @pio_mask: pio_mask
  748. * @mwdma_mask: mwdma_mask
  749. * @udma_mask: udma_mask
  750. *
  751. * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
  752. * unsigned int xfer_mask.
  753. *
  754. * LOCKING:
  755. * None.
  756. *
  757. * RETURNS:
  758. * Packed xfer_mask.
  759. */
  760. unsigned long ata_pack_xfermask(unsigned long pio_mask,
  761. unsigned long mwdma_mask,
  762. unsigned long udma_mask)
  763. {
  764. return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
  765. ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
  766. ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
  767. }
  768. /**
  769. * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
  770. * @xfer_mask: xfer_mask to unpack
  771. * @pio_mask: resulting pio_mask
  772. * @mwdma_mask: resulting mwdma_mask
  773. * @udma_mask: resulting udma_mask
  774. *
  775. * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
  776. * Any NULL destination masks will be ignored.
  777. */
  778. void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
  779. unsigned long *mwdma_mask, unsigned long *udma_mask)
  780. {
  781. if (pio_mask)
  782. *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
  783. if (mwdma_mask)
  784. *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
  785. if (udma_mask)
  786. *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
  787. }
  788. static const struct ata_xfer_ent {
  789. int shift, bits;
  790. u8 base;
  791. } ata_xfer_tbl[] = {
  792. { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
  793. { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
  794. { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
  795. { -1, },
  796. };
  797. /**
  798. * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
  799. * @xfer_mask: xfer_mask of interest
  800. *
  801. * Return matching XFER_* value for @xfer_mask. Only the highest
  802. * bit of @xfer_mask is considered.
  803. *
  804. * LOCKING:
  805. * None.
  806. *
  807. * RETURNS:
  808. * Matching XFER_* value, 0xff if no match found.
  809. */
  810. u8 ata_xfer_mask2mode(unsigned long xfer_mask)
  811. {
  812. int highbit = fls(xfer_mask) - 1;
  813. const struct ata_xfer_ent *ent;
  814. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  815. if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
  816. return ent->base + highbit - ent->shift;
  817. return 0xff;
  818. }
  819. /**
  820. * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
  821. * @xfer_mode: XFER_* of interest
  822. *
  823. * Return matching xfer_mask for @xfer_mode.
  824. *
  825. * LOCKING:
  826. * None.
  827. *
  828. * RETURNS:
  829. * Matching xfer_mask, 0 if no match found.
  830. */
  831. unsigned long ata_xfer_mode2mask(u8 xfer_mode)
  832. {
  833. const struct ata_xfer_ent *ent;
  834. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  835. if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
  836. return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
  837. & ~((1 << ent->shift) - 1);
  838. return 0;
  839. }
  840. /**
  841. * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
  842. * @xfer_mode: XFER_* of interest
  843. *
  844. * Return matching xfer_shift for @xfer_mode.
  845. *
  846. * LOCKING:
  847. * None.
  848. *
  849. * RETURNS:
  850. * Matching xfer_shift, -1 if no match found.
  851. */
  852. int ata_xfer_mode2shift(unsigned long xfer_mode)
  853. {
  854. const struct ata_xfer_ent *ent;
  855. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  856. if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
  857. return ent->shift;
  858. return -1;
  859. }
  860. /**
  861. * ata_mode_string - convert xfer_mask to string
  862. * @xfer_mask: mask of bits supported; only highest bit counts.
  863. *
  864. * Determine string which represents the highest speed
  865. * (highest bit in @modemask).
  866. *
  867. * LOCKING:
  868. * None.
  869. *
  870. * RETURNS:
  871. * Constant C string representing highest speed listed in
  872. * @mode_mask, or the constant C string "<n/a>".
  873. */
  874. const char *ata_mode_string(unsigned long xfer_mask)
  875. {
  876. static const char * const xfer_mode_str[] = {
  877. "PIO0",
  878. "PIO1",
  879. "PIO2",
  880. "PIO3",
  881. "PIO4",
  882. "PIO5",
  883. "PIO6",
  884. "MWDMA0",
  885. "MWDMA1",
  886. "MWDMA2",
  887. "MWDMA3",
  888. "MWDMA4",
  889. "UDMA/16",
  890. "UDMA/25",
  891. "UDMA/33",
  892. "UDMA/44",
  893. "UDMA/66",
  894. "UDMA/100",
  895. "UDMA/133",
  896. "UDMA7",
  897. };
  898. int highbit;
  899. highbit = fls(xfer_mask) - 1;
  900. if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
  901. return xfer_mode_str[highbit];
  902. return "<n/a>";
  903. }
  904. const char *sata_spd_string(unsigned int spd)
  905. {
  906. static const char * const spd_str[] = {
  907. "1.5 Gbps",
  908. "3.0 Gbps",
  909. "6.0 Gbps",
  910. };
  911. if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
  912. return "<unknown>";
  913. return spd_str[spd - 1];
  914. }
  915. /**
  916. * ata_dev_classify - determine device type based on ATA-spec signature
  917. * @tf: ATA taskfile register set for device to be identified
  918. *
  919. * Determine from taskfile register contents whether a device is
  920. * ATA or ATAPI, as per "Signature and persistence" section
  921. * of ATA/PI spec (volume 1, sect 5.14).
  922. *
  923. * LOCKING:
  924. * None.
  925. *
  926. * RETURNS:
  927. * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
  928. * %ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
  929. */
  930. unsigned int ata_dev_classify(const struct ata_taskfile *tf)
  931. {
  932. /* Apple's open source Darwin code hints that some devices only
  933. * put a proper signature into the LBA mid/high registers,
  934. * So, we only check those. It's sufficient for uniqueness.
  935. *
  936. * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
  937. * signatures for ATA and ATAPI devices attached on SerialATA,
  938. * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
  939. * spec has never mentioned about using different signatures
  940. * for ATA/ATAPI devices. Then, Serial ATA II: Port
  941. * Multiplier specification began to use 0x69/0x96 to identify
  942. * port multpliers and 0x3c/0xc3 to identify SEMB device.
  943. * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
  944. * 0x69/0x96 shortly and described them as reserved for
  945. * SerialATA.
  946. *
  947. * We follow the current spec and consider that 0x69/0x96
  948. * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
  949. * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
  950. * SEMB signature. This is worked around in
  951. * ata_dev_read_id().
  952. */
  953. if ((tf->lbam == 0) && (tf->lbah == 0)) {
  954. DPRINTK("found ATA device by sig\n");
  955. return ATA_DEV_ATA;
  956. }
  957. if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
  958. DPRINTK("found ATAPI device by sig\n");
  959. return ATA_DEV_ATAPI;
  960. }
  961. if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
  962. DPRINTK("found PMP device by sig\n");
  963. return ATA_DEV_PMP;
  964. }
  965. if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
  966. DPRINTK("found SEMB device by sig (could be ATA device)\n");
  967. return ATA_DEV_SEMB;
  968. }
  969. if ((tf->lbam == 0xcd) && (tf->lbah == 0xab)) {
  970. DPRINTK("found ZAC device by sig\n");
  971. return ATA_DEV_ZAC;
  972. }
  973. DPRINTK("unknown device\n");
  974. return ATA_DEV_UNKNOWN;
  975. }
  976. /**
  977. * ata_id_string - Convert IDENTIFY DEVICE page into string
  978. * @id: IDENTIFY DEVICE results we will examine
  979. * @s: string into which data is output
  980. * @ofs: offset into identify device page
  981. * @len: length of string to return. must be an even number.
  982. *
  983. * The strings in the IDENTIFY DEVICE page are broken up into
  984. * 16-bit chunks. Run through the string, and output each
  985. * 8-bit chunk linearly, regardless of platform.
  986. *
  987. * LOCKING:
  988. * caller.
  989. */
  990. void ata_id_string(const u16 *id, unsigned char *s,
  991. unsigned int ofs, unsigned int len)
  992. {
  993. unsigned int c;
  994. BUG_ON(len & 1);
  995. while (len > 0) {
  996. c = id[ofs] >> 8;
  997. *s = c;
  998. s++;
  999. c = id[ofs] & 0xff;
  1000. *s = c;
  1001. s++;
  1002. ofs++;
  1003. len -= 2;
  1004. }
  1005. }
  1006. /**
  1007. * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
  1008. * @id: IDENTIFY DEVICE results we will examine
  1009. * @s: string into which data is output
  1010. * @ofs: offset into identify device page
  1011. * @len: length of string to return. must be an odd number.
  1012. *
  1013. * This function is identical to ata_id_string except that it
  1014. * trims trailing spaces and terminates the resulting string with
  1015. * null. @len must be actual maximum length (even number) + 1.
  1016. *
  1017. * LOCKING:
  1018. * caller.
  1019. */
  1020. void ata_id_c_string(const u16 *id, unsigned char *s,
  1021. unsigned int ofs, unsigned int len)
  1022. {
  1023. unsigned char *p;
  1024. ata_id_string(id, s, ofs, len - 1);
  1025. p = s + strnlen(s, len - 1);
  1026. while (p > s && p[-1] == ' ')
  1027. p--;
  1028. *p = '\0';
  1029. }
  1030. static u64 ata_id_n_sectors(const u16 *id)
  1031. {
  1032. if (ata_id_has_lba(id)) {
  1033. if (ata_id_has_lba48(id))
  1034. return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
  1035. else
  1036. return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
  1037. } else {
  1038. if (ata_id_current_chs_valid(id))
  1039. return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
  1040. id[ATA_ID_CUR_SECTORS];
  1041. else
  1042. return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
  1043. id[ATA_ID_SECTORS];
  1044. }
  1045. }
  1046. u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
  1047. {
  1048. u64 sectors = 0;
  1049. sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
  1050. sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
  1051. sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
  1052. sectors |= (tf->lbah & 0xff) << 16;
  1053. sectors |= (tf->lbam & 0xff) << 8;
  1054. sectors |= (tf->lbal & 0xff);
  1055. return sectors;
  1056. }
  1057. u64 ata_tf_to_lba(const struct ata_taskfile *tf)
  1058. {
  1059. u64 sectors = 0;
  1060. sectors |= (tf->device & 0x0f) << 24;
  1061. sectors |= (tf->lbah & 0xff) << 16;
  1062. sectors |= (tf->lbam & 0xff) << 8;
  1063. sectors |= (tf->lbal & 0xff);
  1064. return sectors;
  1065. }
  1066. /**
  1067. * ata_read_native_max_address - Read native max address
  1068. * @dev: target device
  1069. * @max_sectors: out parameter for the result native max address
  1070. *
  1071. * Perform an LBA48 or LBA28 native size query upon the device in
  1072. * question.
  1073. *
  1074. * RETURNS:
  1075. * 0 on success, -EACCES if command is aborted by the drive.
  1076. * -EIO on other errors.
  1077. */
  1078. static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
  1079. {
  1080. unsigned int err_mask;
  1081. struct ata_taskfile tf;
  1082. int lba48 = ata_id_has_lba48(dev->id);
  1083. ata_tf_init(dev, &tf);
  1084. /* always clear all address registers */
  1085. tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  1086. if (lba48) {
  1087. tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
  1088. tf.flags |= ATA_TFLAG_LBA48;
  1089. } else
  1090. tf.command = ATA_CMD_READ_NATIVE_MAX;
  1091. tf.protocol = ATA_PROT_NODATA;
  1092. tf.device |= ATA_LBA;
  1093. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  1094. if (err_mask) {
  1095. ata_dev_warn(dev,
  1096. "failed to read native max address (err_mask=0x%x)\n",
  1097. err_mask);
  1098. if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
  1099. return -EACCES;
  1100. return -EIO;
  1101. }
  1102. if (lba48)
  1103. *max_sectors = ata_tf_to_lba48(&tf) + 1;
  1104. else
  1105. *max_sectors = ata_tf_to_lba(&tf) + 1;
  1106. if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
  1107. (*max_sectors)--;
  1108. return 0;
  1109. }
  1110. /**
  1111. * ata_set_max_sectors - Set max sectors
  1112. * @dev: target device
  1113. * @new_sectors: new max sectors value to set for the device
  1114. *
  1115. * Set max sectors of @dev to @new_sectors.
  1116. *
  1117. * RETURNS:
  1118. * 0 on success, -EACCES if command is aborted or denied (due to
  1119. * previous non-volatile SET_MAX) by the drive. -EIO on other
  1120. * errors.
  1121. */
  1122. static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
  1123. {
  1124. unsigned int err_mask;
  1125. struct ata_taskfile tf;
  1126. int lba48 = ata_id_has_lba48(dev->id);
  1127. new_sectors--;
  1128. ata_tf_init(dev, &tf);
  1129. tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  1130. if (lba48) {
  1131. tf.command = ATA_CMD_SET_MAX_EXT;
  1132. tf.flags |= ATA_TFLAG_LBA48;
  1133. tf.hob_lbal = (new_sectors >> 24) & 0xff;
  1134. tf.hob_lbam = (new_sectors >> 32) & 0xff;
  1135. tf.hob_lbah = (new_sectors >> 40) & 0xff;
  1136. } else {
  1137. tf.command = ATA_CMD_SET_MAX;
  1138. tf.device |= (new_sectors >> 24) & 0xf;
  1139. }
  1140. tf.protocol = ATA_PROT_NODATA;
  1141. tf.device |= ATA_LBA;
  1142. tf.lbal = (new_sectors >> 0) & 0xff;
  1143. tf.lbam = (new_sectors >> 8) & 0xff;
  1144. tf.lbah = (new_sectors >> 16) & 0xff;
  1145. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  1146. if (err_mask) {
  1147. ata_dev_warn(dev,
  1148. "failed to set max address (err_mask=0x%x)\n",
  1149. err_mask);
  1150. if (err_mask == AC_ERR_DEV &&
  1151. (tf.feature & (ATA_ABORTED | ATA_IDNF)))
  1152. return -EACCES;
  1153. return -EIO;
  1154. }
  1155. return 0;
  1156. }
  1157. /**
  1158. * ata_hpa_resize - Resize a device with an HPA set
  1159. * @dev: Device to resize
  1160. *
  1161. * Read the size of an LBA28 or LBA48 disk with HPA features and resize
  1162. * it if required to the full size of the media. The caller must check
  1163. * the drive has the HPA feature set enabled.
  1164. *
  1165. * RETURNS:
  1166. * 0 on success, -errno on failure.
  1167. */
  1168. static int ata_hpa_resize(struct ata_device *dev)
  1169. {
  1170. struct ata_eh_context *ehc = &dev->link->eh_context;
  1171. int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
  1172. bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
  1173. u64 sectors = ata_id_n_sectors(dev->id);
  1174. u64 native_sectors;
  1175. int rc;
  1176. /* do we need to do it? */
  1177. if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) ||
  1178. !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
  1179. (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
  1180. return 0;
  1181. /* read native max address */
  1182. rc = ata_read_native_max_address(dev, &native_sectors);
  1183. if (rc) {
  1184. /* If device aborted the command or HPA isn't going to
  1185. * be unlocked, skip HPA resizing.
  1186. */
  1187. if (rc == -EACCES || !unlock_hpa) {
  1188. ata_dev_warn(dev,
  1189. "HPA support seems broken, skipping HPA handling\n");
  1190. dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
  1191. /* we can continue if device aborted the command */
  1192. if (rc == -EACCES)
  1193. rc = 0;
  1194. }
  1195. return rc;
  1196. }
  1197. dev->n_native_sectors = native_sectors;
  1198. /* nothing to do? */
  1199. if (native_sectors <= sectors || !unlock_hpa) {
  1200. if (!print_info || native_sectors == sectors)
  1201. return 0;
  1202. if (native_sectors > sectors)
  1203. ata_dev_info(dev,
  1204. "HPA detected: current %llu, native %llu\n",
  1205. (unsigned long long)sectors,
  1206. (unsigned long long)native_sectors);
  1207. else if (native_sectors < sectors)
  1208. ata_dev_warn(dev,
  1209. "native sectors (%llu) is smaller than sectors (%llu)\n",
  1210. (unsigned long long)native_sectors,
  1211. (unsigned long long)sectors);
  1212. return 0;
  1213. }
  1214. /* let's unlock HPA */
  1215. rc = ata_set_max_sectors(dev, native_sectors);
  1216. if (rc == -EACCES) {
  1217. /* if device aborted the command, skip HPA resizing */
  1218. ata_dev_warn(dev,
  1219. "device aborted resize (%llu -> %llu), skipping HPA handling\n",
  1220. (unsigned long long)sectors,
  1221. (unsigned long long)native_sectors);
  1222. dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
  1223. return 0;
  1224. } else if (rc)
  1225. return rc;
  1226. /* re-read IDENTIFY data */
  1227. rc = ata_dev_reread_id(dev, 0);
  1228. if (rc) {
  1229. ata_dev_err(dev,
  1230. "failed to re-read IDENTIFY data after HPA resizing\n");
  1231. return rc;
  1232. }
  1233. if (print_info) {
  1234. u64 new_sectors = ata_id_n_sectors(dev->id);
  1235. ata_dev_info(dev,
  1236. "HPA unlocked: %llu -> %llu, native %llu\n",
  1237. (unsigned long long)sectors,
  1238. (unsigned long long)new_sectors,
  1239. (unsigned long long)native_sectors);
  1240. }
  1241. return 0;
  1242. }
  1243. /**
  1244. * ata_dump_id - IDENTIFY DEVICE info debugging output
  1245. * @id: IDENTIFY DEVICE page to dump
  1246. *
  1247. * Dump selected 16-bit words from the given IDENTIFY DEVICE
  1248. * page.
  1249. *
  1250. * LOCKING:
  1251. * caller.
  1252. */
  1253. static inline void ata_dump_id(const u16 *id)
  1254. {
  1255. DPRINTK("49==0x%04x "
  1256. "53==0x%04x "
  1257. "63==0x%04x "
  1258. "64==0x%04x "
  1259. "75==0x%04x \n",
  1260. id[49],
  1261. id[53],
  1262. id[63],
  1263. id[64],
  1264. id[75]);
  1265. DPRINTK("80==0x%04x "
  1266. "81==0x%04x "
  1267. "82==0x%04x "
  1268. "83==0x%04x "
  1269. "84==0x%04x \n",
  1270. id[80],
  1271. id[81],
  1272. id[82],
  1273. id[83],
  1274. id[84]);
  1275. DPRINTK("88==0x%04x "
  1276. "93==0x%04x\n",
  1277. id[88],
  1278. id[93]);
  1279. }
  1280. /**
  1281. * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
  1282. * @id: IDENTIFY data to compute xfer mask from
  1283. *
  1284. * Compute the xfermask for this device. This is not as trivial
  1285. * as it seems if we must consider early devices correctly.
  1286. *
  1287. * FIXME: pre IDE drive timing (do we care ?).
  1288. *
  1289. * LOCKING:
  1290. * None.
  1291. *
  1292. * RETURNS:
  1293. * Computed xfermask
  1294. */
  1295. unsigned long ata_id_xfermask(const u16 *id)
  1296. {
  1297. unsigned long pio_mask, mwdma_mask, udma_mask;
  1298. /* Usual case. Word 53 indicates word 64 is valid */
  1299. if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
  1300. pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
  1301. pio_mask <<= 3;
  1302. pio_mask |= 0x7;
  1303. } else {
  1304. /* If word 64 isn't valid then Word 51 high byte holds
  1305. * the PIO timing number for the maximum. Turn it into
  1306. * a mask.
  1307. */
  1308. u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
  1309. if (mode < 5) /* Valid PIO range */
  1310. pio_mask = (2 << mode) - 1;
  1311. else
  1312. pio_mask = 1;
  1313. /* But wait.. there's more. Design your standards by
  1314. * committee and you too can get a free iordy field to
  1315. * process. However its the speeds not the modes that
  1316. * are supported... Note drivers using the timing API
  1317. * will get this right anyway
  1318. */
  1319. }
  1320. mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
  1321. if (ata_id_is_cfa(id)) {
  1322. /*
  1323. * Process compact flash extended modes
  1324. */
  1325. int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
  1326. int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
  1327. if (pio)
  1328. pio_mask |= (1 << 5);
  1329. if (pio > 1)
  1330. pio_mask |= (1 << 6);
  1331. if (dma)
  1332. mwdma_mask |= (1 << 3);
  1333. if (dma > 1)
  1334. mwdma_mask |= (1 << 4);
  1335. }
  1336. udma_mask = 0;
  1337. if (id[ATA_ID_FIELD_VALID] & (1 << 2))
  1338. udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
  1339. return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
  1340. }
  1341. static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
  1342. {
  1343. struct completion *waiting = qc->private_data;
  1344. complete(waiting);
  1345. }
  1346. /**
  1347. * ata_exec_internal_sg - execute libata internal command
  1348. * @dev: Device to which the command is sent
  1349. * @tf: Taskfile registers for the command and the result
  1350. * @cdb: CDB for packet command
  1351. * @dma_dir: Data transfer direction of the command
  1352. * @sgl: sg list for the data buffer of the command
  1353. * @n_elem: Number of sg entries
  1354. * @timeout: Timeout in msecs (0 for default)
  1355. *
  1356. * Executes libata internal command with timeout. @tf contains
  1357. * command on entry and result on return. Timeout and error
  1358. * conditions are reported via return value. No recovery action
  1359. * is taken after a command times out. It's caller's duty to
  1360. * clean up after timeout.
  1361. *
  1362. * LOCKING:
  1363. * None. Should be called with kernel context, might sleep.
  1364. *
  1365. * RETURNS:
  1366. * Zero on success, AC_ERR_* mask on failure
  1367. */
  1368. unsigned ata_exec_internal_sg(struct ata_device *dev,
  1369. struct ata_taskfile *tf, const u8 *cdb,
  1370. int dma_dir, struct scatterlist *sgl,
  1371. unsigned int n_elem, unsigned long timeout)
  1372. {
  1373. struct ata_link *link = dev->link;
  1374. struct ata_port *ap = link->ap;
  1375. u8 command = tf->command;
  1376. int auto_timeout = 0;
  1377. struct ata_queued_cmd *qc;
  1378. unsigned int preempted_tag;
  1379. u32 preempted_sactive;
  1380. u64 preempted_qc_active;
  1381. int preempted_nr_active_links;
  1382. DECLARE_COMPLETION_ONSTACK(wait);
  1383. unsigned long flags;
  1384. unsigned int err_mask;
  1385. int rc;
  1386. spin_lock_irqsave(ap->lock, flags);
  1387. /* no internal command while frozen */
  1388. if (ap->pflags & ATA_PFLAG_FROZEN) {
  1389. spin_unlock_irqrestore(ap->lock, flags);
  1390. return AC_ERR_SYSTEM;
  1391. }
  1392. /* initialize internal qc */
  1393. qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
  1394. qc->tag = ATA_TAG_INTERNAL;
  1395. qc->hw_tag = 0;
  1396. qc->scsicmd = NULL;
  1397. qc->ap = ap;
  1398. qc->dev = dev;
  1399. ata_qc_reinit(qc);
  1400. preempted_tag = link->active_tag;
  1401. preempted_sactive = link->sactive;
  1402. preempted_qc_active = ap->qc_active;
  1403. preempted_nr_active_links = ap->nr_active_links;
  1404. link->active_tag = ATA_TAG_POISON;
  1405. link->sactive = 0;
  1406. ap->qc_active = 0;
  1407. ap->nr_active_links = 0;
  1408. /* prepare & issue qc */
  1409. qc->tf = *tf;
  1410. if (cdb)
  1411. memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
  1412. /* some SATA bridges need us to indicate data xfer direction */
  1413. if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
  1414. dma_dir == DMA_FROM_DEVICE)
  1415. qc->tf.feature |= ATAPI_DMADIR;
  1416. qc->flags |= ATA_QCFLAG_RESULT_TF;
  1417. qc->dma_dir = dma_dir;
  1418. if (dma_dir != DMA_NONE) {
  1419. unsigned int i, buflen = 0;
  1420. struct scatterlist *sg;
  1421. for_each_sg(sgl, sg, n_elem, i)
  1422. buflen += sg->length;
  1423. ata_sg_init(qc, sgl, n_elem);
  1424. qc->nbytes = buflen;
  1425. }
  1426. qc->private_data = &wait;
  1427. qc->complete_fn = ata_qc_complete_internal;
  1428. ata_qc_issue(qc);
  1429. spin_unlock_irqrestore(ap->lock, flags);
  1430. if (!timeout) {
  1431. if (ata_probe_timeout)
  1432. timeout = ata_probe_timeout * 1000;
  1433. else {
  1434. timeout = ata_internal_cmd_timeout(dev, command);
  1435. auto_timeout = 1;
  1436. }
  1437. }
  1438. if (ap->ops->error_handler)
  1439. ata_eh_release(ap);
  1440. rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
  1441. if (ap->ops->error_handler)
  1442. ata_eh_acquire(ap);
  1443. ata_sff_flush_pio_task(ap);
  1444. if (!rc) {
  1445. spin_lock_irqsave(ap->lock, flags);
  1446. /* We're racing with irq here. If we lose, the
  1447. * following test prevents us from completing the qc
  1448. * twice. If we win, the port is frozen and will be
  1449. * cleaned up by ->post_internal_cmd().
  1450. */
  1451. if (qc->flags & ATA_QCFLAG_ACTIVE) {
  1452. qc->err_mask |= AC_ERR_TIMEOUT;
  1453. if (ap->ops->error_handler)
  1454. ata_port_freeze(ap);
  1455. else
  1456. ata_qc_complete(qc);
  1457. if (ata_msg_warn(ap))
  1458. ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n",
  1459. command);
  1460. }
  1461. spin_unlock_irqrestore(ap->lock, flags);
  1462. }
  1463. /* do post_internal_cmd */
  1464. if (ap->ops->post_internal_cmd)
  1465. ap->ops->post_internal_cmd(qc);
  1466. /* perform minimal error analysis */
  1467. if (qc->flags & ATA_QCFLAG_FAILED) {
  1468. if (qc->result_tf.command & (ATA_ERR | ATA_DF))
  1469. qc->err_mask |= AC_ERR_DEV;
  1470. if (!qc->err_mask)
  1471. qc->err_mask |= AC_ERR_OTHER;
  1472. if (qc->err_mask & ~AC_ERR_OTHER)
  1473. qc->err_mask &= ~AC_ERR_OTHER;
  1474. } else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) {
  1475. qc->result_tf.command |= ATA_SENSE;
  1476. }
  1477. /* finish up */
  1478. spin_lock_irqsave(ap->lock, flags);
  1479. *tf = qc->result_tf;
  1480. err_mask = qc->err_mask;
  1481. ata_qc_free(qc);
  1482. link->active_tag = preempted_tag;
  1483. link->sactive = preempted_sactive;
  1484. ap->qc_active = preempted_qc_active;
  1485. ap->nr_active_links = preempted_nr_active_links;
  1486. spin_unlock_irqrestore(ap->lock, flags);
  1487. if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
  1488. ata_internal_cmd_timed_out(dev, command);
  1489. return err_mask;
  1490. }
  1491. /**
  1492. * ata_exec_internal - execute libata internal command
  1493. * @dev: Device to which the command is sent
  1494. * @tf: Taskfile registers for the command and the result
  1495. * @cdb: CDB for packet command
  1496. * @dma_dir: Data transfer direction of the command
  1497. * @buf: Data buffer of the command
  1498. * @buflen: Length of data buffer
  1499. * @timeout: Timeout in msecs (0 for default)
  1500. *
  1501. * Wrapper around ata_exec_internal_sg() which takes simple
  1502. * buffer instead of sg list.
  1503. *
  1504. * LOCKING:
  1505. * None. Should be called with kernel context, might sleep.
  1506. *
  1507. * RETURNS:
  1508. * Zero on success, AC_ERR_* mask on failure
  1509. */
  1510. unsigned ata_exec_internal(struct ata_device *dev,
  1511. struct ata_taskfile *tf, const u8 *cdb,
  1512. int dma_dir, void *buf, unsigned int buflen,
  1513. unsigned long timeout)
  1514. {
  1515. struct scatterlist *psg = NULL, sg;
  1516. unsigned int n_elem = 0;
  1517. if (dma_dir != DMA_NONE) {
  1518. WARN_ON(!buf);
  1519. sg_init_one(&sg, buf, buflen);
  1520. psg = &sg;
  1521. n_elem++;
  1522. }
  1523. return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
  1524. timeout);
  1525. }
  1526. /**
  1527. * ata_pio_need_iordy - check if iordy needed
  1528. * @adev: ATA device
  1529. *
  1530. * Check if the current speed of the device requires IORDY. Used
  1531. * by various controllers for chip configuration.
  1532. */
  1533. unsigned int ata_pio_need_iordy(const struct ata_device *adev)
  1534. {
  1535. /* Don't set IORDY if we're preparing for reset. IORDY may
  1536. * lead to controller lock up on certain controllers if the
  1537. * port is not occupied. See bko#11703 for details.
  1538. */
  1539. if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
  1540. return 0;
  1541. /* Controller doesn't support IORDY. Probably a pointless
  1542. * check as the caller should know this.
  1543. */
  1544. if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
  1545. return 0;
  1546. /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */
  1547. if (ata_id_is_cfa(adev->id)
  1548. && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
  1549. return 0;
  1550. /* PIO3 and higher it is mandatory */
  1551. if (adev->pio_mode > XFER_PIO_2)
  1552. return 1;
  1553. /* We turn it on when possible */
  1554. if (ata_id_has_iordy(adev->id))
  1555. return 1;
  1556. return 0;
  1557. }
  1558. /**
  1559. * ata_pio_mask_no_iordy - Return the non IORDY mask
  1560. * @adev: ATA device
  1561. *
  1562. * Compute the highest mode possible if we are not using iordy. Return
  1563. * -1 if no iordy mode is available.
  1564. */
  1565. static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
  1566. {
  1567. /* If we have no drive specific rule, then PIO 2 is non IORDY */
  1568. if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
  1569. u16 pio = adev->id[ATA_ID_EIDE_PIO];
  1570. /* Is the speed faster than the drive allows non IORDY ? */
  1571. if (pio) {
  1572. /* This is cycle times not frequency - watch the logic! */
  1573. if (pio > 240) /* PIO2 is 240nS per cycle */
  1574. return 3 << ATA_SHIFT_PIO;
  1575. return 7 << ATA_SHIFT_PIO;
  1576. }
  1577. }
  1578. return 3 << ATA_SHIFT_PIO;
  1579. }
  1580. /**
  1581. * ata_do_dev_read_id - default ID read method
  1582. * @dev: device
  1583. * @tf: proposed taskfile
  1584. * @id: data buffer
  1585. *
  1586. * Issue the identify taskfile and hand back the buffer containing
  1587. * identify data. For some RAID controllers and for pre ATA devices
  1588. * this function is wrapped or replaced by the driver
  1589. */
  1590. unsigned int ata_do_dev_read_id(struct ata_device *dev,
  1591. struct ata_taskfile *tf, u16 *id)
  1592. {
  1593. return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
  1594. id, sizeof(id[0]) * ATA_ID_WORDS, 0);
  1595. }
  1596. /**
  1597. * ata_dev_read_id - Read ID data from the specified device
  1598. * @dev: target device
  1599. * @p_class: pointer to class of the target device (may be changed)
  1600. * @flags: ATA_READID_* flags
  1601. * @id: buffer to read IDENTIFY data into
  1602. *
  1603. * Read ID data from the specified device. ATA_CMD_ID_ATA is
  1604. * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
  1605. * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
  1606. * for pre-ATA4 drives.
  1607. *
  1608. * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
  1609. * now we abort if we hit that case.
  1610. *
  1611. * LOCKING:
  1612. * Kernel thread context (may sleep)
  1613. *
  1614. * RETURNS:
  1615. * 0 on success, -errno otherwise.
  1616. */
  1617. int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
  1618. unsigned int flags, u16 *id)
  1619. {
  1620. struct ata_port *ap = dev->link->ap;
  1621. unsigned int class = *p_class;
  1622. struct ata_taskfile tf;
  1623. unsigned int err_mask = 0;
  1624. const char *reason;
  1625. bool is_semb = class == ATA_DEV_SEMB;
  1626. int may_fallback = 1, tried_spinup = 0;
  1627. int rc;
  1628. if (ata_msg_ctl(ap))
  1629. ata_dev_dbg(dev, "%s: ENTER\n", __func__);
  1630. retry:
  1631. ata_tf_init(dev, &tf);
  1632. switch (class) {
  1633. case ATA_DEV_SEMB:
  1634. class = ATA_DEV_ATA; /* some hard drives report SEMB sig */
  1635. /* fall through */
  1636. case ATA_DEV_ATA:
  1637. case ATA_DEV_ZAC:
  1638. tf.command = ATA_CMD_ID_ATA;
  1639. break;
  1640. case ATA_DEV_ATAPI:
  1641. tf.command = ATA_CMD_ID_ATAPI;
  1642. break;
  1643. default:
  1644. rc = -ENODEV;
  1645. reason = "unsupported class";
  1646. goto err_out;
  1647. }
  1648. tf.protocol = ATA_PROT_PIO;
  1649. /* Some devices choke if TF registers contain garbage. Make
  1650. * sure those are properly initialized.
  1651. */
  1652. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1653. /* Device presence detection is unreliable on some
  1654. * controllers. Always poll IDENTIFY if available.
  1655. */
  1656. tf.flags |= ATA_TFLAG_POLLING;
  1657. if (ap->ops->read_id)
  1658. err_mask = ap->ops->read_id(dev, &tf, id);
  1659. else
  1660. err_mask = ata_do_dev_read_id(dev, &tf, id);
  1661. if (err_mask) {
  1662. if (err_mask & AC_ERR_NODEV_HINT) {
  1663. ata_dev_dbg(dev, "NODEV after polling detection\n");
  1664. return -ENOENT;
  1665. }
  1666. if (is_semb) {
  1667. ata_dev_info(dev,
  1668. "IDENTIFY failed on device w/ SEMB sig, disabled\n");
  1669. /* SEMB is not supported yet */
  1670. *p_class = ATA_DEV_SEMB_UNSUP;
  1671. return 0;
  1672. }
  1673. if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
  1674. /* Device or controller might have reported
  1675. * the wrong device class. Give a shot at the
  1676. * other IDENTIFY if the current one is
  1677. * aborted by the device.
  1678. */
  1679. if (may_fallback) {
  1680. may_fallback = 0;
  1681. if (class == ATA_DEV_ATA)
  1682. class = ATA_DEV_ATAPI;
  1683. else
  1684. class = ATA_DEV_ATA;
  1685. goto retry;
  1686. }
  1687. /* Control reaches here iff the device aborted
  1688. * both flavors of IDENTIFYs which happens
  1689. * sometimes with phantom devices.
  1690. */
  1691. ata_dev_dbg(dev,
  1692. "both IDENTIFYs aborted, assuming NODEV\n");
  1693. return -ENOENT;
  1694. }
  1695. rc = -EIO;
  1696. reason = "I/O error";
  1697. goto err_out;
  1698. }
  1699. if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
  1700. ata_dev_dbg(dev, "dumping IDENTIFY data, "
  1701. "class=%d may_fallback=%d tried_spinup=%d\n",
  1702. class, may_fallback, tried_spinup);
  1703. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
  1704. 16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
  1705. }
  1706. /* Falling back doesn't make sense if ID data was read
  1707. * successfully at least once.
  1708. */
  1709. may_fallback = 0;
  1710. swap_buf_le16(id, ATA_ID_WORDS);
  1711. /* sanity check */
  1712. rc = -EINVAL;
  1713. reason = "device reports invalid type";
  1714. if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
  1715. if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
  1716. goto err_out;
  1717. if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
  1718. ata_id_is_ata(id)) {
  1719. ata_dev_dbg(dev,
  1720. "host indicates ignore ATA devices, ignored\n");
  1721. return -ENOENT;
  1722. }
  1723. } else {
  1724. if (ata_id_is_ata(id))
  1725. goto err_out;
  1726. }
  1727. if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
  1728. tried_spinup = 1;
  1729. /*
  1730. * Drive powered-up in standby mode, and requires a specific
  1731. * SET_FEATURES spin-up subcommand before it will accept
  1732. * anything other than the original IDENTIFY command.
  1733. */
  1734. err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
  1735. if (err_mask && id[2] != 0x738c) {
  1736. rc = -EIO;
  1737. reason = "SPINUP failed";
  1738. goto err_out;
  1739. }
  1740. /*
  1741. * If the drive initially returned incomplete IDENTIFY info,
  1742. * we now must reissue the IDENTIFY command.
  1743. */
  1744. if (id[2] == 0x37c8)
  1745. goto retry;
  1746. }
  1747. if ((flags & ATA_READID_POSTRESET) &&
  1748. (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
  1749. /*
  1750. * The exact sequence expected by certain pre-ATA4 drives is:
  1751. * SRST RESET
  1752. * IDENTIFY (optional in early ATA)
  1753. * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
  1754. * anything else..
  1755. * Some drives were very specific about that exact sequence.
  1756. *
  1757. * Note that ATA4 says lba is mandatory so the second check
  1758. * should never trigger.
  1759. */
  1760. if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
  1761. err_mask = ata_dev_init_params(dev, id[3], id[6]);
  1762. if (err_mask) {
  1763. rc = -EIO;
  1764. reason = "INIT_DEV_PARAMS failed";
  1765. goto err_out;
  1766. }
  1767. /* current CHS translation info (id[53-58]) might be
  1768. * changed. reread the identify device info.
  1769. */
  1770. flags &= ~ATA_READID_POSTRESET;
  1771. goto retry;
  1772. }
  1773. }
  1774. *p_class = class;
  1775. return 0;
  1776. err_out:
  1777. if (ata_msg_warn(ap))
  1778. ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
  1779. reason, err_mask);
  1780. return rc;
  1781. }
  1782. /**
  1783. * ata_read_log_page - read a specific log page
  1784. * @dev: target device
  1785. * @log: log to read
  1786. * @page: page to read
  1787. * @buf: buffer to store read page
  1788. * @sectors: number of sectors to read
  1789. *
  1790. * Read log page using READ_LOG_EXT command.
  1791. *
  1792. * LOCKING:
  1793. * Kernel thread context (may sleep).
  1794. *
  1795. * RETURNS:
  1796. * 0 on success, AC_ERR_* mask otherwise.
  1797. */
  1798. unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
  1799. u8 page, void *buf, unsigned int sectors)
  1800. {
  1801. unsigned long ap_flags = dev->link->ap->flags;
  1802. struct ata_taskfile tf;
  1803. unsigned int err_mask;
  1804. bool dma = false;
  1805. DPRINTK("read log page - log 0x%x, page 0x%x\n", log, page);
  1806. /*
  1807. * Return error without actually issuing the command on controllers
  1808. * which e.g. lockup on a read log page.
  1809. */
  1810. if (ap_flags & ATA_FLAG_NO_LOG_PAGE)
  1811. return AC_ERR_DEV;
  1812. retry:
  1813. ata_tf_init(dev, &tf);
  1814. if (dev->dma_mode && ata_id_has_read_log_dma_ext(dev->id) &&
  1815. !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) {
  1816. tf.command = ATA_CMD_READ_LOG_DMA_EXT;
  1817. tf.protocol = ATA_PROT_DMA;
  1818. dma = true;
  1819. } else {
  1820. tf.command = ATA_CMD_READ_LOG_EXT;
  1821. tf.protocol = ATA_PROT_PIO;
  1822. dma = false;
  1823. }
  1824. tf.lbal = log;
  1825. tf.lbam = page;
  1826. tf.nsect = sectors;
  1827. tf.hob_nsect = sectors >> 8;
  1828. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
  1829. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
  1830. buf, sectors * ATA_SECT_SIZE, 0);
  1831. if (err_mask && dma) {
  1832. dev->horkage |= ATA_HORKAGE_NO_DMA_LOG;
  1833. ata_dev_warn(dev, "READ LOG DMA EXT failed, trying PIO\n");
  1834. goto retry;
  1835. }
  1836. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  1837. return err_mask;
  1838. }
  1839. static bool ata_log_supported(struct ata_device *dev, u8 log)
  1840. {
  1841. struct ata_port *ap = dev->link->ap;
  1842. if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1))
  1843. return false;
  1844. return get_unaligned_le16(&ap->sector_buf[log * 2]) ? true : false;
  1845. }
  1846. static bool ata_identify_page_supported(struct ata_device *dev, u8 page)
  1847. {
  1848. struct ata_port *ap = dev->link->ap;
  1849. unsigned int err, i;
  1850. if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) {
  1851. ata_dev_warn(dev, "ATA Identify Device Log not supported\n");
  1852. return false;
  1853. }
  1854. /*
  1855. * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is
  1856. * supported.
  1857. */
  1858. err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf,
  1859. 1);
  1860. if (err) {
  1861. ata_dev_info(dev,
  1862. "failed to get Device Identify Log Emask 0x%x\n",
  1863. err);
  1864. return false;
  1865. }
  1866. for (i = 0; i < ap->sector_buf[8]; i++) {
  1867. if (ap->sector_buf[9 + i] == page)
  1868. return true;
  1869. }
  1870. return false;
  1871. }
  1872. static int ata_do_link_spd_horkage(struct ata_device *dev)
  1873. {
  1874. struct ata_link *plink = ata_dev_phys_link(dev);
  1875. u32 target, target_limit;
  1876. if (!sata_scr_valid(plink))
  1877. return 0;
  1878. if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
  1879. target = 1;
  1880. else
  1881. return 0;
  1882. target_limit = (1 << target) - 1;
  1883. /* if already on stricter limit, no need to push further */
  1884. if (plink->sata_spd_limit <= target_limit)
  1885. return 0;
  1886. plink->sata_spd_limit = target_limit;
  1887. /* Request another EH round by returning -EAGAIN if link is
  1888. * going faster than the target speed. Forward progress is
  1889. * guaranteed by setting sata_spd_limit to target_limit above.
  1890. */
  1891. if (plink->sata_spd > target) {
  1892. ata_dev_info(dev, "applying link speed limit horkage to %s\n",
  1893. sata_spd_string(target));
  1894. return -EAGAIN;
  1895. }
  1896. return 0;
  1897. }
  1898. static inline u8 ata_dev_knobble(struct ata_device *dev)
  1899. {
  1900. struct ata_port *ap = dev->link->ap;
  1901. if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
  1902. return 0;
  1903. return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
  1904. }
  1905. static void ata_dev_config_ncq_send_recv(struct ata_device *dev)
  1906. {
  1907. struct ata_port *ap = dev->link->ap;
  1908. unsigned int err_mask;
  1909. if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) {
  1910. ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n");
  1911. return;
  1912. }
  1913. err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
  1914. 0, ap->sector_buf, 1);
  1915. if (err_mask) {
  1916. ata_dev_dbg(dev,
  1917. "failed to get NCQ Send/Recv Log Emask 0x%x\n",
  1918. err_mask);
  1919. } else {
  1920. u8 *cmds = dev->ncq_send_recv_cmds;
  1921. dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
  1922. memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
  1923. if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
  1924. ata_dev_dbg(dev, "disabling queued TRIM support\n");
  1925. cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
  1926. ~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
  1927. }
  1928. }
  1929. }
  1930. static void ata_dev_config_ncq_non_data(struct ata_device *dev)
  1931. {
  1932. struct ata_port *ap = dev->link->ap;
  1933. unsigned int err_mask;
  1934. if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) {
  1935. ata_dev_warn(dev,
  1936. "NCQ Send/Recv Log not supported\n");
  1937. return;
  1938. }
  1939. err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
  1940. 0, ap->sector_buf, 1);
  1941. if (err_mask) {
  1942. ata_dev_dbg(dev,
  1943. "failed to get NCQ Non-Data Log Emask 0x%x\n",
  1944. err_mask);
  1945. } else {
  1946. u8 *cmds = dev->ncq_non_data_cmds;
  1947. memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
  1948. }
  1949. }
  1950. static void ata_dev_config_ncq_prio(struct ata_device *dev)
  1951. {
  1952. struct ata_port *ap = dev->link->ap;
  1953. unsigned int err_mask;
  1954. if (!(dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE)) {
  1955. dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
  1956. return;
  1957. }
  1958. err_mask = ata_read_log_page(dev,
  1959. ATA_LOG_IDENTIFY_DEVICE,
  1960. ATA_LOG_SATA_SETTINGS,
  1961. ap->sector_buf,
  1962. 1);
  1963. if (err_mask) {
  1964. ata_dev_dbg(dev,
  1965. "failed to get Identify Device data, Emask 0x%x\n",
  1966. err_mask);
  1967. return;
  1968. }
  1969. if (ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)) {
  1970. dev->flags |= ATA_DFLAG_NCQ_PRIO;
  1971. } else {
  1972. dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
  1973. ata_dev_dbg(dev, "SATA page does not support priority\n");
  1974. }
  1975. }
  1976. static int ata_dev_config_ncq(struct ata_device *dev,
  1977. char *desc, size_t desc_sz)
  1978. {
  1979. struct ata_port *ap = dev->link->ap;
  1980. int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
  1981. unsigned int err_mask;
  1982. char *aa_desc = "";
  1983. if (!ata_id_has_ncq(dev->id)) {
  1984. desc[0] = '\0';
  1985. return 0;
  1986. }
  1987. if (dev->horkage & ATA_HORKAGE_NONCQ) {
  1988. snprintf(desc, desc_sz, "NCQ (not used)");
  1989. return 0;
  1990. }
  1991. if (ap->flags & ATA_FLAG_NCQ) {
  1992. hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE);
  1993. dev->flags |= ATA_DFLAG_NCQ;
  1994. }
  1995. if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
  1996. (ap->flags & ATA_FLAG_FPDMA_AA) &&
  1997. ata_id_has_fpdma_aa(dev->id)) {
  1998. err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
  1999. SATA_FPDMA_AA);
  2000. if (err_mask) {
  2001. ata_dev_err(dev,
  2002. "failed to enable AA (error_mask=0x%x)\n",
  2003. err_mask);
  2004. if (err_mask != AC_ERR_DEV) {
  2005. dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
  2006. return -EIO;
  2007. }
  2008. } else
  2009. aa_desc = ", AA";
  2010. }
  2011. if (hdepth >= ddepth)
  2012. snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
  2013. else
  2014. snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
  2015. ddepth, aa_desc);
  2016. if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
  2017. if (ata_id_has_ncq_send_and_recv(dev->id))
  2018. ata_dev_config_ncq_send_recv(dev);
  2019. if (ata_id_has_ncq_non_data(dev->id))
  2020. ata_dev_config_ncq_non_data(dev);
  2021. if (ata_id_has_ncq_prio(dev->id))
  2022. ata_dev_config_ncq_prio(dev);
  2023. }
  2024. return 0;
  2025. }
  2026. static void ata_dev_config_sense_reporting(struct ata_device *dev)
  2027. {
  2028. unsigned int err_mask;
  2029. if (!ata_id_has_sense_reporting(dev->id))
  2030. return;
  2031. if (ata_id_sense_reporting_enabled(dev->id))
  2032. return;
  2033. err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
  2034. if (err_mask) {
  2035. ata_dev_dbg(dev,
  2036. "failed to enable Sense Data Reporting, Emask 0x%x\n",
  2037. err_mask);
  2038. }
  2039. }
  2040. static void ata_dev_config_zac(struct ata_device *dev)
  2041. {
  2042. struct ata_port *ap = dev->link->ap;
  2043. unsigned int err_mask;
  2044. u8 *identify_buf = ap->sector_buf;
  2045. dev->zac_zones_optimal_open = U32_MAX;
  2046. dev->zac_zones_optimal_nonseq = U32_MAX;
  2047. dev->zac_zones_max_open = U32_MAX;
  2048. /*
  2049. * Always set the 'ZAC' flag for Host-managed devices.
  2050. */
  2051. if (dev->class == ATA_DEV_ZAC)
  2052. dev->flags |= ATA_DFLAG_ZAC;
  2053. else if (ata_id_zoned_cap(dev->id) == 0x01)
  2054. /*
  2055. * Check for host-aware devices.
  2056. */
  2057. dev->flags |= ATA_DFLAG_ZAC;
  2058. if (!(dev->flags & ATA_DFLAG_ZAC))
  2059. return;
  2060. if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) {
  2061. ata_dev_warn(dev,
  2062. "ATA Zoned Information Log not supported\n");
  2063. return;
  2064. }
  2065. /*
  2066. * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
  2067. */
  2068. err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
  2069. ATA_LOG_ZONED_INFORMATION,
  2070. identify_buf, 1);
  2071. if (!err_mask) {
  2072. u64 zoned_cap, opt_open, opt_nonseq, max_open;
  2073. zoned_cap = get_unaligned_le64(&identify_buf[8]);
  2074. if ((zoned_cap >> 63))
  2075. dev->zac_zoned_cap = (zoned_cap & 1);
  2076. opt_open = get_unaligned_le64(&identify_buf[24]);
  2077. if ((opt_open >> 63))
  2078. dev->zac_zones_optimal_open = (u32)opt_open;
  2079. opt_nonseq = get_unaligned_le64(&identify_buf[32]);
  2080. if ((opt_nonseq >> 63))
  2081. dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
  2082. max_open = get_unaligned_le64(&identify_buf[40]);
  2083. if ((max_open >> 63))
  2084. dev->zac_zones_max_open = (u32)max_open;
  2085. }
  2086. }
  2087. static void ata_dev_config_trusted(struct ata_device *dev)
  2088. {
  2089. struct ata_port *ap = dev->link->ap;
  2090. u64 trusted_cap;
  2091. unsigned int err;
  2092. if (!ata_id_has_trusted(dev->id))
  2093. return;
  2094. if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) {
  2095. ata_dev_warn(dev,
  2096. "Security Log not supported\n");
  2097. return;
  2098. }
  2099. err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY,
  2100. ap->sector_buf, 1);
  2101. if (err) {
  2102. ata_dev_dbg(dev,
  2103. "failed to read Security Log, Emask 0x%x\n", err);
  2104. return;
  2105. }
  2106. trusted_cap = get_unaligned_le64(&ap->sector_buf[40]);
  2107. if (!(trusted_cap & (1ULL << 63))) {
  2108. ata_dev_dbg(dev,
  2109. "Trusted Computing capability qword not valid!\n");
  2110. return;
  2111. }
  2112. if (trusted_cap & (1 << 0))
  2113. dev->flags |= ATA_DFLAG_TRUSTED;
  2114. }
  2115. /**
  2116. * ata_dev_configure - Configure the specified ATA/ATAPI device
  2117. * @dev: Target device to configure
  2118. *
  2119. * Configure @dev according to @dev->id. Generic and low-level
  2120. * driver specific fixups are also applied.
  2121. *
  2122. * LOCKING:
  2123. * Kernel thread context (may sleep)
  2124. *
  2125. * RETURNS:
  2126. * 0 on success, -errno otherwise
  2127. */
  2128. int ata_dev_configure(struct ata_device *dev)
  2129. {
  2130. struct ata_port *ap = dev->link->ap;
  2131. struct ata_eh_context *ehc = &dev->link->eh_context;
  2132. int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
  2133. const u16 *id = dev->id;
  2134. unsigned long xfer_mask;
  2135. unsigned int err_mask;
  2136. char revbuf[7]; /* XYZ-99\0 */
  2137. char fwrevbuf[ATA_ID_FW_REV_LEN+1];
  2138. char modelbuf[ATA_ID_PROD_LEN+1];
  2139. int rc;
  2140. if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
  2141. ata_dev_info(dev, "%s: ENTER/EXIT -- nodev\n", __func__);
  2142. return 0;
  2143. }
  2144. if (ata_msg_probe(ap))
  2145. ata_dev_dbg(dev, "%s: ENTER\n", __func__);
  2146. /* set horkage */
  2147. dev->horkage |= ata_dev_blacklisted(dev);
  2148. ata_force_horkage(dev);
  2149. if (dev->horkage & ATA_HORKAGE_DISABLE) {
  2150. ata_dev_info(dev, "unsupported device, disabling\n");
  2151. ata_dev_disable(dev);
  2152. return 0;
  2153. }
  2154. if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
  2155. dev->class == ATA_DEV_ATAPI) {
  2156. ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
  2157. atapi_enabled ? "not supported with this driver"
  2158. : "disabled");
  2159. ata_dev_disable(dev);
  2160. return 0;
  2161. }
  2162. rc = ata_do_link_spd_horkage(dev);
  2163. if (rc)
  2164. return rc;
  2165. /* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
  2166. if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
  2167. (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
  2168. dev->horkage |= ATA_HORKAGE_NOLPM;
  2169. if (ap->flags & ATA_FLAG_NO_LPM)
  2170. dev->horkage |= ATA_HORKAGE_NOLPM;
  2171. if (dev->horkage & ATA_HORKAGE_NOLPM) {
  2172. ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
  2173. dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
  2174. }
  2175. /* let ACPI work its magic */
  2176. rc = ata_acpi_on_devcfg(dev);
  2177. if (rc)
  2178. return rc;
  2179. /* massage HPA, do it early as it might change IDENTIFY data */
  2180. rc = ata_hpa_resize(dev);
  2181. if (rc)
  2182. return rc;
  2183. /* print device capabilities */
  2184. if (ata_msg_probe(ap))
  2185. ata_dev_dbg(dev,
  2186. "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
  2187. "85:%04x 86:%04x 87:%04x 88:%04x\n",
  2188. __func__,
  2189. id[49], id[82], id[83], id[84],
  2190. id[85], id[86], id[87], id[88]);
  2191. /* initialize to-be-configured parameters */
  2192. dev->flags &= ~ATA_DFLAG_CFG_MASK;
  2193. dev->max_sectors = 0;
  2194. dev->cdb_len = 0;
  2195. dev->n_sectors = 0;
  2196. dev->cylinders = 0;
  2197. dev->heads = 0;
  2198. dev->sectors = 0;
  2199. dev->multi_count = 0;
  2200. /*
  2201. * common ATA, ATAPI feature tests
  2202. */
  2203. /* find max transfer mode; for printk only */
  2204. xfer_mask = ata_id_xfermask(id);
  2205. if (ata_msg_probe(ap))
  2206. ata_dump_id(id);
  2207. /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
  2208. ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
  2209. sizeof(fwrevbuf));
  2210. ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
  2211. sizeof(modelbuf));
  2212. /* ATA-specific feature tests */
  2213. if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
  2214. if (ata_id_is_cfa(id)) {
  2215. /* CPRM may make this media unusable */
  2216. if (id[ATA_ID_CFA_KEY_MGMT] & 1)
  2217. ata_dev_warn(dev,
  2218. "supports DRM functions and may not be fully accessible\n");
  2219. snprintf(revbuf, 7, "CFA");
  2220. } else {
  2221. snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
  2222. /* Warn the user if the device has TPM extensions */
  2223. if (ata_id_has_tpm(id))
  2224. ata_dev_warn(dev,
  2225. "supports DRM functions and may not be fully accessible\n");
  2226. }
  2227. dev->n_sectors = ata_id_n_sectors(id);
  2228. /* get current R/W Multiple count setting */
  2229. if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
  2230. unsigned int max = dev->id[47] & 0xff;
  2231. unsigned int cnt = dev->id[59] & 0xff;
  2232. /* only recognize/allow powers of two here */
  2233. if (is_power_of_2(max) && is_power_of_2(cnt))
  2234. if (cnt <= max)
  2235. dev->multi_count = cnt;
  2236. }
  2237. if (ata_id_has_lba(id)) {
  2238. const char *lba_desc;
  2239. char ncq_desc[24];
  2240. lba_desc = "LBA";
  2241. dev->flags |= ATA_DFLAG_LBA;
  2242. if (ata_id_has_lba48(id)) {
  2243. dev->flags |= ATA_DFLAG_LBA48;
  2244. lba_desc = "LBA48";
  2245. if (dev->n_sectors >= (1UL << 28) &&
  2246. ata_id_has_flush_ext(id))
  2247. dev->flags |= ATA_DFLAG_FLUSH_EXT;
  2248. }
  2249. /* config NCQ */
  2250. rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
  2251. if (rc)
  2252. return rc;
  2253. /* print device info to dmesg */
  2254. if (ata_msg_drv(ap) && print_info) {
  2255. ata_dev_info(dev, "%s: %s, %s, max %s\n",
  2256. revbuf, modelbuf, fwrevbuf,
  2257. ata_mode_string(xfer_mask));
  2258. ata_dev_info(dev,
  2259. "%llu sectors, multi %u: %s %s\n",
  2260. (unsigned long long)dev->n_sectors,
  2261. dev->multi_count, lba_desc, ncq_desc);
  2262. }
  2263. } else {
  2264. /* CHS */
  2265. /* Default translation */
  2266. dev->cylinders = id[1];
  2267. dev->heads = id[3];
  2268. dev->sectors = id[6];
  2269. if (ata_id_current_chs_valid(id)) {
  2270. /* Current CHS translation is valid. */
  2271. dev->cylinders = id[54];
  2272. dev->heads = id[55];
  2273. dev->sectors = id[56];
  2274. }
  2275. /* print device info to dmesg */
  2276. if (ata_msg_drv(ap) && print_info) {
  2277. ata_dev_info(dev, "%s: %s, %s, max %s\n",
  2278. revbuf, modelbuf, fwrevbuf,
  2279. ata_mode_string(xfer_mask));
  2280. ata_dev_info(dev,
  2281. "%llu sectors, multi %u, CHS %u/%u/%u\n",
  2282. (unsigned long long)dev->n_sectors,
  2283. dev->multi_count, dev->cylinders,
  2284. dev->heads, dev->sectors);
  2285. }
  2286. }
  2287. /* Check and mark DevSlp capability. Get DevSlp timing variables
  2288. * from SATA Settings page of Identify Device Data Log.
  2289. */
  2290. if (ata_id_has_devslp(dev->id)) {
  2291. u8 *sata_setting = ap->sector_buf;
  2292. int i, j;
  2293. dev->flags |= ATA_DFLAG_DEVSLP;
  2294. err_mask = ata_read_log_page(dev,
  2295. ATA_LOG_IDENTIFY_DEVICE,
  2296. ATA_LOG_SATA_SETTINGS,
  2297. sata_setting,
  2298. 1);
  2299. if (err_mask)
  2300. ata_dev_dbg(dev,
  2301. "failed to get Identify Device Data, Emask 0x%x\n",
  2302. err_mask);
  2303. else
  2304. for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
  2305. j = ATA_LOG_DEVSLP_OFFSET + i;
  2306. dev->devslp_timing[i] = sata_setting[j];
  2307. }
  2308. }
  2309. ata_dev_config_sense_reporting(dev);
  2310. ata_dev_config_zac(dev);
  2311. ata_dev_config_trusted(dev);
  2312. dev->cdb_len = 32;
  2313. }
  2314. /* ATAPI-specific feature tests */
  2315. else if (dev->class == ATA_DEV_ATAPI) {
  2316. const char *cdb_intr_string = "";
  2317. const char *atapi_an_string = "";
  2318. const char *dma_dir_string = "";
  2319. u32 sntf;
  2320. rc = atapi_cdb_len(id);
  2321. if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
  2322. if (ata_msg_warn(ap))
  2323. ata_dev_warn(dev, "unsupported CDB len\n");
  2324. rc = -EINVAL;
  2325. goto err_out_nosup;
  2326. }
  2327. dev->cdb_len = (unsigned int) rc;
  2328. /* Enable ATAPI AN if both the host and device have
  2329. * the support. If PMP is attached, SNTF is required
  2330. * to enable ATAPI AN to discern between PHY status
  2331. * changed notifications and ATAPI ANs.
  2332. */
  2333. if (atapi_an &&
  2334. (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
  2335. (!sata_pmp_attached(ap) ||
  2336. sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
  2337. /* issue SET feature command to turn this on */
  2338. err_mask = ata_dev_set_feature(dev,
  2339. SETFEATURES_SATA_ENABLE, SATA_AN);
  2340. if (err_mask)
  2341. ata_dev_err(dev,
  2342. "failed to enable ATAPI AN (err_mask=0x%x)\n",
  2343. err_mask);
  2344. else {
  2345. dev->flags |= ATA_DFLAG_AN;
  2346. atapi_an_string = ", ATAPI AN";
  2347. }
  2348. }
  2349. if (ata_id_cdb_intr(dev->id)) {
  2350. dev->flags |= ATA_DFLAG_CDB_INTR;
  2351. cdb_intr_string = ", CDB intr";
  2352. }
  2353. if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
  2354. dev->flags |= ATA_DFLAG_DMADIR;
  2355. dma_dir_string = ", DMADIR";
  2356. }
  2357. if (ata_id_has_da(dev->id)) {
  2358. dev->flags |= ATA_DFLAG_DA;
  2359. zpodd_init(dev);
  2360. }
  2361. /* print device info to dmesg */
  2362. if (ata_msg_drv(ap) && print_info)
  2363. ata_dev_info(dev,
  2364. "ATAPI: %s, %s, max %s%s%s%s\n",
  2365. modelbuf, fwrevbuf,
  2366. ata_mode_string(xfer_mask),
  2367. cdb_intr_string, atapi_an_string,
  2368. dma_dir_string);
  2369. }
  2370. /* determine max_sectors */
  2371. dev->max_sectors = ATA_MAX_SECTORS;
  2372. if (dev->flags & ATA_DFLAG_LBA48)
  2373. dev->max_sectors = ATA_MAX_SECTORS_LBA48;
  2374. /* Limit PATA drive on SATA cable bridge transfers to udma5,
  2375. 200 sectors */
  2376. if (ata_dev_knobble(dev)) {
  2377. if (ata_msg_drv(ap) && print_info)
  2378. ata_dev_info(dev, "applying bridge limits\n");
  2379. dev->udma_mask &= ATA_UDMA5;
  2380. dev->max_sectors = ATA_MAX_SECTORS;
  2381. }
  2382. if ((dev->class == ATA_DEV_ATAPI) &&
  2383. (atapi_command_packet_set(id) == TYPE_TAPE)) {
  2384. dev->max_sectors = ATA_MAX_SECTORS_TAPE;
  2385. dev->horkage |= ATA_HORKAGE_STUCK_ERR;
  2386. }
  2387. if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
  2388. dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
  2389. dev->max_sectors);
  2390. if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
  2391. dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
  2392. dev->max_sectors);
  2393. if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
  2394. dev->max_sectors = ATA_MAX_SECTORS_LBA48;
  2395. if (ap->ops->dev_config)
  2396. ap->ops->dev_config(dev);
  2397. if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
  2398. /* Let the user know. We don't want to disallow opens for
  2399. rescue purposes, or in case the vendor is just a blithering
  2400. idiot. Do this after the dev_config call as some controllers
  2401. with buggy firmware may want to avoid reporting false device
  2402. bugs */
  2403. if (print_info) {
  2404. ata_dev_warn(dev,
  2405. "Drive reports diagnostics failure. This may indicate a drive\n");
  2406. ata_dev_warn(dev,
  2407. "fault or invalid emulation. Contact drive vendor for information.\n");
  2408. }
  2409. }
  2410. if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
  2411. ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
  2412. ata_dev_warn(dev, " contact the vendor or visit http://ata.wiki.kernel.org\n");
  2413. }
  2414. return 0;
  2415. err_out_nosup:
  2416. if (ata_msg_probe(ap))
  2417. ata_dev_dbg(dev, "%s: EXIT, err\n", __func__);
  2418. return rc;
  2419. }
  2420. /**
  2421. * ata_cable_40wire - return 40 wire cable type
  2422. * @ap: port
  2423. *
  2424. * Helper method for drivers which want to hardwire 40 wire cable
  2425. * detection.
  2426. */
  2427. int ata_cable_40wire(struct ata_port *ap)
  2428. {
  2429. return ATA_CBL_PATA40;
  2430. }
  2431. /**
  2432. * ata_cable_80wire - return 80 wire cable type
  2433. * @ap: port
  2434. *
  2435. * Helper method for drivers which want to hardwire 80 wire cable
  2436. * detection.
  2437. */
  2438. int ata_cable_80wire(struct ata_port *ap)
  2439. {
  2440. return ATA_CBL_PATA80;
  2441. }
  2442. /**
  2443. * ata_cable_unknown - return unknown PATA cable.
  2444. * @ap: port
  2445. *
  2446. * Helper method for drivers which have no PATA cable detection.
  2447. */
  2448. int ata_cable_unknown(struct ata_port *ap)
  2449. {
  2450. return ATA_CBL_PATA_UNK;
  2451. }
  2452. /**
  2453. * ata_cable_ignore - return ignored PATA cable.
  2454. * @ap: port
  2455. *
  2456. * Helper method for drivers which don't use cable type to limit
  2457. * transfer mode.
  2458. */
  2459. int ata_cable_ignore(struct ata_port *ap)
  2460. {
  2461. return ATA_CBL_PATA_IGN;
  2462. }
  2463. /**
  2464. * ata_cable_sata - return SATA cable type
  2465. * @ap: port
  2466. *
  2467. * Helper method for drivers which have SATA cables
  2468. */
  2469. int ata_cable_sata(struct ata_port *ap)
  2470. {
  2471. return ATA_CBL_SATA;
  2472. }
  2473. /**
  2474. * ata_bus_probe - Reset and probe ATA bus
  2475. * @ap: Bus to probe
  2476. *
  2477. * Master ATA bus probing function. Initiates a hardware-dependent
  2478. * bus reset, then attempts to identify any devices found on
  2479. * the bus.
  2480. *
  2481. * LOCKING:
  2482. * PCI/etc. bus probe sem.
  2483. *
  2484. * RETURNS:
  2485. * Zero on success, negative errno otherwise.
  2486. */
  2487. int ata_bus_probe(struct ata_port *ap)
  2488. {
  2489. unsigned int classes[ATA_MAX_DEVICES];
  2490. int tries[ATA_MAX_DEVICES];
  2491. int rc;
  2492. struct ata_device *dev;
  2493. ata_for_each_dev(dev, &ap->link, ALL)
  2494. tries[dev->devno] = ATA_PROBE_MAX_TRIES;
  2495. retry:
  2496. ata_for_each_dev(dev, &ap->link, ALL) {
  2497. /* If we issue an SRST then an ATA drive (not ATAPI)
  2498. * may change configuration and be in PIO0 timing. If
  2499. * we do a hard reset (or are coming from power on)
  2500. * this is true for ATA or ATAPI. Until we've set a
  2501. * suitable controller mode we should not touch the
  2502. * bus as we may be talking too fast.
  2503. */
  2504. dev->pio_mode = XFER_PIO_0;
  2505. dev->dma_mode = 0xff;
  2506. /* If the controller has a pio mode setup function
  2507. * then use it to set the chipset to rights. Don't
  2508. * touch the DMA setup as that will be dealt with when
  2509. * configuring devices.
  2510. */
  2511. if (ap->ops->set_piomode)
  2512. ap->ops->set_piomode(ap, dev);
  2513. }
  2514. /* reset and determine device classes */
  2515. ap->ops->phy_reset(ap);
  2516. ata_for_each_dev(dev, &ap->link, ALL) {
  2517. if (dev->class != ATA_DEV_UNKNOWN)
  2518. classes[dev->devno] = dev->class;
  2519. else
  2520. classes[dev->devno] = ATA_DEV_NONE;
  2521. dev->class = ATA_DEV_UNKNOWN;
  2522. }
  2523. /* read IDENTIFY page and configure devices. We have to do the identify
  2524. specific sequence bass-ackwards so that PDIAG- is released by
  2525. the slave device */
  2526. ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
  2527. if (tries[dev->devno])
  2528. dev->class = classes[dev->devno];
  2529. if (!ata_dev_enabled(dev))
  2530. continue;
  2531. rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
  2532. dev->id);
  2533. if (rc)
  2534. goto fail;
  2535. }
  2536. /* Now ask for the cable type as PDIAG- should have been released */
  2537. if (ap->ops->cable_detect)
  2538. ap->cbl = ap->ops->cable_detect(ap);
  2539. /* We may have SATA bridge glue hiding here irrespective of
  2540. * the reported cable types and sensed types. When SATA
  2541. * drives indicate we have a bridge, we don't know which end
  2542. * of the link the bridge is which is a problem.
  2543. */
  2544. ata_for_each_dev(dev, &ap->link, ENABLED)
  2545. if (ata_id_is_sata(dev->id))
  2546. ap->cbl = ATA_CBL_SATA;
  2547. /* After the identify sequence we can now set up the devices. We do
  2548. this in the normal order so that the user doesn't get confused */
  2549. ata_for_each_dev(dev, &ap->link, ENABLED) {
  2550. ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
  2551. rc = ata_dev_configure(dev);
  2552. ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
  2553. if (rc)
  2554. goto fail;
  2555. }
  2556. /* configure transfer mode */
  2557. rc = ata_set_mode(&ap->link, &dev);
  2558. if (rc)
  2559. goto fail;
  2560. ata_for_each_dev(dev, &ap->link, ENABLED)
  2561. return 0;
  2562. return -ENODEV;
  2563. fail:
  2564. tries[dev->devno]--;
  2565. switch (rc) {
  2566. case -EINVAL:
  2567. /* eeek, something went very wrong, give up */
  2568. tries[dev->devno] = 0;
  2569. break;
  2570. case -ENODEV:
  2571. /* give it just one more chance */
  2572. tries[dev->devno] = min(tries[dev->devno], 1);
  2573. /* fall through */
  2574. case -EIO:
  2575. if (tries[dev->devno] == 1) {
  2576. /* This is the last chance, better to slow
  2577. * down than lose it.
  2578. */
  2579. sata_down_spd_limit(&ap->link, 0);
  2580. ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
  2581. }
  2582. }
  2583. if (!tries[dev->devno])
  2584. ata_dev_disable(dev);
  2585. goto retry;
  2586. }
  2587. /**
  2588. * sata_print_link_status - Print SATA link status
  2589. * @link: SATA link to printk link status about
  2590. *
  2591. * This function prints link speed and status of a SATA link.
  2592. *
  2593. * LOCKING:
  2594. * None.
  2595. */
  2596. static void sata_print_link_status(struct ata_link *link)
  2597. {
  2598. u32 sstatus, scontrol, tmp;
  2599. if (sata_scr_read(link, SCR_STATUS, &sstatus))
  2600. return;
  2601. sata_scr_read(link, SCR_CONTROL, &scontrol);
  2602. if (ata_phys_link_online(link)) {
  2603. tmp = (sstatus >> 4) & 0xf;
  2604. ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
  2605. sata_spd_string(tmp), sstatus, scontrol);
  2606. } else {
  2607. ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
  2608. sstatus, scontrol);
  2609. }
  2610. }
  2611. /**
  2612. * ata_dev_pair - return other device on cable
  2613. * @adev: device
  2614. *
  2615. * Obtain the other device on the same cable, or if none is
  2616. * present NULL is returned
  2617. */
  2618. struct ata_device *ata_dev_pair(struct ata_device *adev)
  2619. {
  2620. struct ata_link *link = adev->link;
  2621. struct ata_device *pair = &link->device[1 - adev->devno];
  2622. if (!ata_dev_enabled(pair))
  2623. return NULL;
  2624. return pair;
  2625. }
  2626. /**
  2627. * sata_down_spd_limit - adjust SATA spd limit downward
  2628. * @link: Link to adjust SATA spd limit for
  2629. * @spd_limit: Additional limit
  2630. *
  2631. * Adjust SATA spd limit of @link downward. Note that this
  2632. * function only adjusts the limit. The change must be applied
  2633. * using sata_set_spd().
  2634. *
  2635. * If @spd_limit is non-zero, the speed is limited to equal to or
  2636. * lower than @spd_limit if such speed is supported. If
  2637. * @spd_limit is slower than any supported speed, only the lowest
  2638. * supported speed is allowed.
  2639. *
  2640. * LOCKING:
  2641. * Inherited from caller.
  2642. *
  2643. * RETURNS:
  2644. * 0 on success, negative errno on failure
  2645. */
  2646. int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
  2647. {
  2648. u32 sstatus, spd, mask;
  2649. int rc, bit;
  2650. if (!sata_scr_valid(link))
  2651. return -EOPNOTSUPP;
  2652. /* If SCR can be read, use it to determine the current SPD.
  2653. * If not, use cached value in link->sata_spd.
  2654. */
  2655. rc = sata_scr_read(link, SCR_STATUS, &sstatus);
  2656. if (rc == 0 && ata_sstatus_online(sstatus))
  2657. spd = (sstatus >> 4) & 0xf;
  2658. else
  2659. spd = link->sata_spd;
  2660. mask = link->sata_spd_limit;
  2661. if (mask <= 1)
  2662. return -EINVAL;
  2663. /* unconditionally mask off the highest bit */
  2664. bit = fls(mask) - 1;
  2665. mask &= ~(1 << bit);
  2666. /*
  2667. * Mask off all speeds higher than or equal to the current one. At
  2668. * this point, if current SPD is not available and we previously
  2669. * recorded the link speed from SStatus, the driver has already
  2670. * masked off the highest bit so mask should already be 1 or 0.
  2671. * Otherwise, we should not force 1.5Gbps on a link where we have
  2672. * not previously recorded speed from SStatus. Just return in this
  2673. * case.
  2674. */
  2675. if (spd > 1)
  2676. mask &= (1 << (spd - 1)) - 1;
  2677. else
  2678. return -EINVAL;
  2679. /* were we already at the bottom? */
  2680. if (!mask)
  2681. return -EINVAL;
  2682. if (spd_limit) {
  2683. if (mask & ((1 << spd_limit) - 1))
  2684. mask &= (1 << spd_limit) - 1;
  2685. else {
  2686. bit = ffs(mask) - 1;
  2687. mask = 1 << bit;
  2688. }
  2689. }
  2690. link->sata_spd_limit = mask;
  2691. ata_link_warn(link, "limiting SATA link speed to %s\n",
  2692. sata_spd_string(fls(mask)));
  2693. return 0;
  2694. }
  2695. static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
  2696. {
  2697. struct ata_link *host_link = &link->ap->link;
  2698. u32 limit, target, spd;
  2699. limit = link->sata_spd_limit;
  2700. /* Don't configure downstream link faster than upstream link.
  2701. * It doesn't speed up anything and some PMPs choke on such
  2702. * configuration.
  2703. */
  2704. if (!ata_is_host_link(link) && host_link->sata_spd)
  2705. limit &= (1 << host_link->sata_spd) - 1;
  2706. if (limit == UINT_MAX)
  2707. target = 0;
  2708. else
  2709. target = fls(limit);
  2710. spd = (*scontrol >> 4) & 0xf;
  2711. *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
  2712. return spd != target;
  2713. }
  2714. /**
  2715. * sata_set_spd_needed - is SATA spd configuration needed
  2716. * @link: Link in question
  2717. *
  2718. * Test whether the spd limit in SControl matches
  2719. * @link->sata_spd_limit. This function is used to determine
  2720. * whether hardreset is necessary to apply SATA spd
  2721. * configuration.
  2722. *
  2723. * LOCKING:
  2724. * Inherited from caller.
  2725. *
  2726. * RETURNS:
  2727. * 1 if SATA spd configuration is needed, 0 otherwise.
  2728. */
  2729. static int sata_set_spd_needed(struct ata_link *link)
  2730. {
  2731. u32 scontrol;
  2732. if (sata_scr_read(link, SCR_CONTROL, &scontrol))
  2733. return 1;
  2734. return __sata_set_spd_needed(link, &scontrol);
  2735. }
  2736. /**
  2737. * sata_set_spd - set SATA spd according to spd limit
  2738. * @link: Link to set SATA spd for
  2739. *
  2740. * Set SATA spd of @link according to sata_spd_limit.
  2741. *
  2742. * LOCKING:
  2743. * Inherited from caller.
  2744. *
  2745. * RETURNS:
  2746. * 0 if spd doesn't need to be changed, 1 if spd has been
  2747. * changed. Negative errno if SCR registers are inaccessible.
  2748. */
  2749. int sata_set_spd(struct ata_link *link)
  2750. {
  2751. u32 scontrol;
  2752. int rc;
  2753. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  2754. return rc;
  2755. if (!__sata_set_spd_needed(link, &scontrol))
  2756. return 0;
  2757. if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
  2758. return rc;
  2759. return 1;
  2760. }
  2761. /*
  2762. * This mode timing computation functionality is ported over from
  2763. * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
  2764. */
  2765. /*
  2766. * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
  2767. * These were taken from ATA/ATAPI-6 standard, rev 0a, except
  2768. * for UDMA6, which is currently supported only by Maxtor drives.
  2769. *
  2770. * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
  2771. */
  2772. static const struct ata_timing ata_timing[] = {
  2773. /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 0, 960, 0 }, */
  2774. { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 0, 600, 0 },
  2775. { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 0, 383, 0 },
  2776. { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 0, 240, 0 },
  2777. { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 0, 180, 0 },
  2778. { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 0, 120, 0 },
  2779. { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 0, 100, 0 },
  2780. { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 0, 80, 0 },
  2781. { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 50, 960, 0 },
  2782. { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 30, 480, 0 },
  2783. { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 20, 240, 0 },
  2784. { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 20, 480, 0 },
  2785. { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 5, 150, 0 },
  2786. { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 5, 120, 0 },
  2787. { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 5, 100, 0 },
  2788. { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 5, 80, 0 },
  2789. /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 0, 150 }, */
  2790. { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 0, 120 },
  2791. { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 0, 80 },
  2792. { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 0, 60 },
  2793. { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 0, 45 },
  2794. { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 0, 30 },
  2795. { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 0, 20 },
  2796. { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 0, 15 },
  2797. { 0xFF }
  2798. };
  2799. #define ENOUGH(v, unit) (((v)-1)/(unit)+1)
  2800. #define EZ(v, unit) ((v)?ENOUGH(((v) * 1000), unit):0)
  2801. static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
  2802. {
  2803. q->setup = EZ(t->setup, T);
  2804. q->act8b = EZ(t->act8b, T);
  2805. q->rec8b = EZ(t->rec8b, T);
  2806. q->cyc8b = EZ(t->cyc8b, T);
  2807. q->active = EZ(t->active, T);
  2808. q->recover = EZ(t->recover, T);
  2809. q->dmack_hold = EZ(t->dmack_hold, T);
  2810. q->cycle = EZ(t->cycle, T);
  2811. q->udma = EZ(t->udma, UT);
  2812. }
  2813. void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
  2814. struct ata_timing *m, unsigned int what)
  2815. {
  2816. if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
  2817. if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
  2818. if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
  2819. if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
  2820. if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
  2821. if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
  2822. if (what & ATA_TIMING_DMACK_HOLD) m->dmack_hold = max(a->dmack_hold, b->dmack_hold);
  2823. if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
  2824. if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
  2825. }
  2826. const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
  2827. {
  2828. const struct ata_timing *t = ata_timing;
  2829. while (xfer_mode > t->mode)
  2830. t++;
  2831. if (xfer_mode == t->mode)
  2832. return t;
  2833. WARN_ONCE(true, "%s: unable to find timing for xfer_mode 0x%x\n",
  2834. __func__, xfer_mode);
  2835. return NULL;
  2836. }
  2837. int ata_timing_compute(struct ata_device *adev, unsigned short speed,
  2838. struct ata_timing *t, int T, int UT)
  2839. {
  2840. const u16 *id = adev->id;
  2841. const struct ata_timing *s;
  2842. struct ata_timing p;
  2843. /*
  2844. * Find the mode.
  2845. */
  2846. if (!(s = ata_timing_find_mode(speed)))
  2847. return -EINVAL;
  2848. memcpy(t, s, sizeof(*s));
  2849. /*
  2850. * If the drive is an EIDE drive, it can tell us it needs extended
  2851. * PIO/MW_DMA cycle timing.
  2852. */
  2853. if (id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
  2854. memset(&p, 0, sizeof(p));
  2855. if (speed >= XFER_PIO_0 && speed < XFER_SW_DMA_0) {
  2856. if (speed <= XFER_PIO_2)
  2857. p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO];
  2858. else if ((speed <= XFER_PIO_4) ||
  2859. (speed == XFER_PIO_5 && !ata_id_is_cfa(id)))
  2860. p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY];
  2861. } else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
  2862. p.cycle = id[ATA_ID_EIDE_DMA_MIN];
  2863. ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
  2864. }
  2865. /*
  2866. * Convert the timing to bus clock counts.
  2867. */
  2868. ata_timing_quantize(t, t, T, UT);
  2869. /*
  2870. * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
  2871. * S.M.A.R.T * and some other commands. We have to ensure that the
  2872. * DMA cycle timing is slower/equal than the fastest PIO timing.
  2873. */
  2874. if (speed > XFER_PIO_6) {
  2875. ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
  2876. ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
  2877. }
  2878. /*
  2879. * Lengthen active & recovery time so that cycle time is correct.
  2880. */
  2881. if (t->act8b + t->rec8b < t->cyc8b) {
  2882. t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
  2883. t->rec8b = t->cyc8b - t->act8b;
  2884. }
  2885. if (t->active + t->recover < t->cycle) {
  2886. t->active += (t->cycle - (t->active + t->recover)) / 2;
  2887. t->recover = t->cycle - t->active;
  2888. }
  2889. /* In a few cases quantisation may produce enough errors to
  2890. leave t->cycle too low for the sum of active and recovery
  2891. if so we must correct this */
  2892. if (t->active + t->recover > t->cycle)
  2893. t->cycle = t->active + t->recover;
  2894. return 0;
  2895. }
  2896. /**
  2897. * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
  2898. * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
  2899. * @cycle: cycle duration in ns
  2900. *
  2901. * Return matching xfer mode for @cycle. The returned mode is of
  2902. * the transfer type specified by @xfer_shift. If @cycle is too
  2903. * slow for @xfer_shift, 0xff is returned. If @cycle is faster
  2904. * than the fastest known mode, the fasted mode is returned.
  2905. *
  2906. * LOCKING:
  2907. * None.
  2908. *
  2909. * RETURNS:
  2910. * Matching xfer_mode, 0xff if no match found.
  2911. */
  2912. u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
  2913. {
  2914. u8 base_mode = 0xff, last_mode = 0xff;
  2915. const struct ata_xfer_ent *ent;
  2916. const struct ata_timing *t;
  2917. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  2918. if (ent->shift == xfer_shift)
  2919. base_mode = ent->base;
  2920. for (t = ata_timing_find_mode(base_mode);
  2921. t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
  2922. unsigned short this_cycle;
  2923. switch (xfer_shift) {
  2924. case ATA_SHIFT_PIO:
  2925. case ATA_SHIFT_MWDMA:
  2926. this_cycle = t->cycle;
  2927. break;
  2928. case ATA_SHIFT_UDMA:
  2929. this_cycle = t->udma;
  2930. break;
  2931. default:
  2932. return 0xff;
  2933. }
  2934. if (cycle > this_cycle)
  2935. break;
  2936. last_mode = t->mode;
  2937. }
  2938. return last_mode;
  2939. }
  2940. /**
  2941. * ata_down_xfermask_limit - adjust dev xfer masks downward
  2942. * @dev: Device to adjust xfer masks
  2943. * @sel: ATA_DNXFER_* selector
  2944. *
  2945. * Adjust xfer masks of @dev downward. Note that this function
  2946. * does not apply the change. Invoking ata_set_mode() afterwards
  2947. * will apply the limit.
  2948. *
  2949. * LOCKING:
  2950. * Inherited from caller.
  2951. *
  2952. * RETURNS:
  2953. * 0 on success, negative errno on failure
  2954. */
  2955. int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
  2956. {
  2957. char buf[32];
  2958. unsigned long orig_mask, xfer_mask;
  2959. unsigned long pio_mask, mwdma_mask, udma_mask;
  2960. int quiet, highbit;
  2961. quiet = !!(sel & ATA_DNXFER_QUIET);
  2962. sel &= ~ATA_DNXFER_QUIET;
  2963. xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
  2964. dev->mwdma_mask,
  2965. dev->udma_mask);
  2966. ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
  2967. switch (sel) {
  2968. case ATA_DNXFER_PIO:
  2969. highbit = fls(pio_mask) - 1;
  2970. pio_mask &= ~(1 << highbit);
  2971. break;
  2972. case ATA_DNXFER_DMA:
  2973. if (udma_mask) {
  2974. highbit = fls(udma_mask) - 1;
  2975. udma_mask &= ~(1 << highbit);
  2976. if (!udma_mask)
  2977. return -ENOENT;
  2978. } else if (mwdma_mask) {
  2979. highbit = fls(mwdma_mask) - 1;
  2980. mwdma_mask &= ~(1 << highbit);
  2981. if (!mwdma_mask)
  2982. return -ENOENT;
  2983. }
  2984. break;
  2985. case ATA_DNXFER_40C:
  2986. udma_mask &= ATA_UDMA_MASK_40C;
  2987. break;
  2988. case ATA_DNXFER_FORCE_PIO0:
  2989. pio_mask &= 1;
  2990. /* fall through */
  2991. case ATA_DNXFER_FORCE_PIO:
  2992. mwdma_mask = 0;
  2993. udma_mask = 0;
  2994. break;
  2995. default:
  2996. BUG();
  2997. }
  2998. xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
  2999. if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
  3000. return -ENOENT;
  3001. if (!quiet) {
  3002. if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
  3003. snprintf(buf, sizeof(buf), "%s:%s",
  3004. ata_mode_string(xfer_mask),
  3005. ata_mode_string(xfer_mask & ATA_MASK_PIO));
  3006. else
  3007. snprintf(buf, sizeof(buf), "%s",
  3008. ata_mode_string(xfer_mask));
  3009. ata_dev_warn(dev, "limiting speed to %s\n", buf);
  3010. }
  3011. ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
  3012. &dev->udma_mask);
  3013. return 0;
  3014. }
  3015. static int ata_dev_set_mode(struct ata_device *dev)
  3016. {
  3017. struct ata_port *ap = dev->link->ap;
  3018. struct ata_eh_context *ehc = &dev->link->eh_context;
  3019. const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
  3020. const char *dev_err_whine = "";
  3021. int ign_dev_err = 0;
  3022. unsigned int err_mask = 0;
  3023. int rc;
  3024. dev->flags &= ~ATA_DFLAG_PIO;
  3025. if (dev->xfer_shift == ATA_SHIFT_PIO)
  3026. dev->flags |= ATA_DFLAG_PIO;
  3027. if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
  3028. dev_err_whine = " (SET_XFERMODE skipped)";
  3029. else {
  3030. if (nosetxfer)
  3031. ata_dev_warn(dev,
  3032. "NOSETXFER but PATA detected - can't "
  3033. "skip SETXFER, might malfunction\n");
  3034. err_mask = ata_dev_set_xfermode(dev);
  3035. }
  3036. if (err_mask & ~AC_ERR_DEV)
  3037. goto fail;
  3038. /* revalidate */
  3039. ehc->i.flags |= ATA_EHI_POST_SETMODE;
  3040. rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
  3041. ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
  3042. if (rc)
  3043. return rc;
  3044. if (dev->xfer_shift == ATA_SHIFT_PIO) {
  3045. /* Old CFA may refuse this command, which is just fine */
  3046. if (ata_id_is_cfa(dev->id))
  3047. ign_dev_err = 1;
  3048. /* Catch several broken garbage emulations plus some pre
  3049. ATA devices */
  3050. if (ata_id_major_version(dev->id) == 0 &&
  3051. dev->pio_mode <= XFER_PIO_2)
  3052. ign_dev_err = 1;
  3053. /* Some very old devices and some bad newer ones fail
  3054. any kind of SET_XFERMODE request but support PIO0-2
  3055. timings and no IORDY */
  3056. if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
  3057. ign_dev_err = 1;
  3058. }
  3059. /* Early MWDMA devices do DMA but don't allow DMA mode setting.
  3060. Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
  3061. if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
  3062. dev->dma_mode == XFER_MW_DMA_0 &&
  3063. (dev->id[63] >> 8) & 1)
  3064. ign_dev_err = 1;
  3065. /* if the device is actually configured correctly, ignore dev err */
  3066. if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
  3067. ign_dev_err = 1;
  3068. if (err_mask & AC_ERR_DEV) {
  3069. if (!ign_dev_err)
  3070. goto fail;
  3071. else
  3072. dev_err_whine = " (device error ignored)";
  3073. }
  3074. DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
  3075. dev->xfer_shift, (int)dev->xfer_mode);
  3076. if (!(ehc->i.flags & ATA_EHI_QUIET) ||
  3077. ehc->i.flags & ATA_EHI_DID_HARDRESET)
  3078. ata_dev_info(dev, "configured for %s%s\n",
  3079. ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
  3080. dev_err_whine);
  3081. return 0;
  3082. fail:
  3083. ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
  3084. return -EIO;
  3085. }
  3086. /**
  3087. * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
  3088. * @link: link on which timings will be programmed
  3089. * @r_failed_dev: out parameter for failed device
  3090. *
  3091. * Standard implementation of the function used to tune and set
  3092. * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
  3093. * ata_dev_set_mode() fails, pointer to the failing device is
  3094. * returned in @r_failed_dev.
  3095. *
  3096. * LOCKING:
  3097. * PCI/etc. bus probe sem.
  3098. *
  3099. * RETURNS:
  3100. * 0 on success, negative errno otherwise
  3101. */
  3102. int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
  3103. {
  3104. struct ata_port *ap = link->ap;
  3105. struct ata_device *dev;
  3106. int rc = 0, used_dma = 0, found = 0;
  3107. /* step 1: calculate xfer_mask */
  3108. ata_for_each_dev(dev, link, ENABLED) {
  3109. unsigned long pio_mask, dma_mask;
  3110. unsigned int mode_mask;
  3111. mode_mask = ATA_DMA_MASK_ATA;
  3112. if (dev->class == ATA_DEV_ATAPI)
  3113. mode_mask = ATA_DMA_MASK_ATAPI;
  3114. else if (ata_id_is_cfa(dev->id))
  3115. mode_mask = ATA_DMA_MASK_CFA;
  3116. ata_dev_xfermask(dev);
  3117. ata_force_xfermask(dev);
  3118. pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
  3119. if (libata_dma_mask & mode_mask)
  3120. dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
  3121. dev->udma_mask);
  3122. else
  3123. dma_mask = 0;
  3124. dev->pio_mode = ata_xfer_mask2mode(pio_mask);
  3125. dev->dma_mode = ata_xfer_mask2mode(dma_mask);
  3126. found = 1;
  3127. if (ata_dma_enabled(dev))
  3128. used_dma = 1;
  3129. }
  3130. if (!found)
  3131. goto out;
  3132. /* step 2: always set host PIO timings */
  3133. ata_for_each_dev(dev, link, ENABLED) {
  3134. if (dev->pio_mode == 0xff) {
  3135. ata_dev_warn(dev, "no PIO support\n");
  3136. rc = -EINVAL;
  3137. goto out;
  3138. }
  3139. dev->xfer_mode = dev->pio_mode;
  3140. dev->xfer_shift = ATA_SHIFT_PIO;
  3141. if (ap->ops->set_piomode)
  3142. ap->ops->set_piomode(ap, dev);
  3143. }
  3144. /* step 3: set host DMA timings */
  3145. ata_for_each_dev(dev, link, ENABLED) {
  3146. if (!ata_dma_enabled(dev))
  3147. continue;
  3148. dev->xfer_mode = dev->dma_mode;
  3149. dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
  3150. if (ap->ops->set_dmamode)
  3151. ap->ops->set_dmamode(ap, dev);
  3152. }
  3153. /* step 4: update devices' xfer mode */
  3154. ata_for_each_dev(dev, link, ENABLED) {
  3155. rc = ata_dev_set_mode(dev);
  3156. if (rc)
  3157. goto out;
  3158. }
  3159. /* Record simplex status. If we selected DMA then the other
  3160. * host channels are not permitted to do so.
  3161. */
  3162. if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
  3163. ap->host->simplex_claimed = ap;
  3164. out:
  3165. if (rc)
  3166. *r_failed_dev = dev;
  3167. return rc;
  3168. }
  3169. /**
  3170. * ata_wait_ready - wait for link to become ready
  3171. * @link: link to be waited on
  3172. * @deadline: deadline jiffies for the operation
  3173. * @check_ready: callback to check link readiness
  3174. *
  3175. * Wait for @link to become ready. @check_ready should return
  3176. * positive number if @link is ready, 0 if it isn't, -ENODEV if
  3177. * link doesn't seem to be occupied, other errno for other error
  3178. * conditions.
  3179. *
  3180. * Transient -ENODEV conditions are allowed for
  3181. * ATA_TMOUT_FF_WAIT.
  3182. *
  3183. * LOCKING:
  3184. * EH context.
  3185. *
  3186. * RETURNS:
  3187. * 0 if @link is ready before @deadline; otherwise, -errno.
  3188. */
  3189. int ata_wait_ready(struct ata_link *link, unsigned long deadline,
  3190. int (*check_ready)(struct ata_link *link))
  3191. {
  3192. unsigned long start = jiffies;
  3193. unsigned long nodev_deadline;
  3194. int warned = 0;
  3195. /* choose which 0xff timeout to use, read comment in libata.h */
  3196. if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
  3197. nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
  3198. else
  3199. nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
  3200. /* Slave readiness can't be tested separately from master. On
  3201. * M/S emulation configuration, this function should be called
  3202. * only on the master and it will handle both master and slave.
  3203. */
  3204. WARN_ON(link == link->ap->slave_link);
  3205. if (time_after(nodev_deadline, deadline))
  3206. nodev_deadline = deadline;
  3207. while (1) {
  3208. unsigned long now = jiffies;
  3209. int ready, tmp;
  3210. ready = tmp = check_ready(link);
  3211. if (ready > 0)
  3212. return 0;
  3213. /*
  3214. * -ENODEV could be transient. Ignore -ENODEV if link
  3215. * is online. Also, some SATA devices take a long
  3216. * time to clear 0xff after reset. Wait for
  3217. * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
  3218. * offline.
  3219. *
  3220. * Note that some PATA controllers (pata_ali) explode
  3221. * if status register is read more than once when
  3222. * there's no device attached.
  3223. */
  3224. if (ready == -ENODEV) {
  3225. if (ata_link_online(link))
  3226. ready = 0;
  3227. else if ((link->ap->flags & ATA_FLAG_SATA) &&
  3228. !ata_link_offline(link) &&
  3229. time_before(now, nodev_deadline))
  3230. ready = 0;
  3231. }
  3232. if (ready)
  3233. return ready;
  3234. if (time_after(now, deadline))
  3235. return -EBUSY;
  3236. if (!warned && time_after(now, start + 5 * HZ) &&
  3237. (deadline - now > 3 * HZ)) {
  3238. ata_link_warn(link,
  3239. "link is slow to respond, please be patient "
  3240. "(ready=%d)\n", tmp);
  3241. warned = 1;
  3242. }
  3243. ata_msleep(link->ap, 50);
  3244. }
  3245. }
  3246. /**
  3247. * ata_wait_after_reset - wait for link to become ready after reset
  3248. * @link: link to be waited on
  3249. * @deadline: deadline jiffies for the operation
  3250. * @check_ready: callback to check link readiness
  3251. *
  3252. * Wait for @link to become ready after reset.
  3253. *
  3254. * LOCKING:
  3255. * EH context.
  3256. *
  3257. * RETURNS:
  3258. * 0 if @link is ready before @deadline; otherwise, -errno.
  3259. */
  3260. int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
  3261. int (*check_ready)(struct ata_link *link))
  3262. {
  3263. ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
  3264. return ata_wait_ready(link, deadline, check_ready);
  3265. }
  3266. /**
  3267. * sata_link_debounce - debounce SATA phy status
  3268. * @link: ATA link to debounce SATA phy status for
  3269. * @params: timing parameters { interval, duration, timeout } in msec
  3270. * @deadline: deadline jiffies for the operation
  3271. *
  3272. * Make sure SStatus of @link reaches stable state, determined by
  3273. * holding the same value where DET is not 1 for @duration polled
  3274. * every @interval, before @timeout. Timeout constraints the
  3275. * beginning of the stable state. Because DET gets stuck at 1 on
  3276. * some controllers after hot unplugging, this functions waits
  3277. * until timeout then returns 0 if DET is stable at 1.
  3278. *
  3279. * @timeout is further limited by @deadline. The sooner of the
  3280. * two is used.
  3281. *
  3282. * LOCKING:
  3283. * Kernel thread context (may sleep)
  3284. *
  3285. * RETURNS:
  3286. * 0 on success, -errno on failure.
  3287. */
  3288. int sata_link_debounce(struct ata_link *link, const unsigned long *params,
  3289. unsigned long deadline)
  3290. {
  3291. unsigned long interval = params[0];
  3292. unsigned long duration = params[1];
  3293. unsigned long last_jiffies, t;
  3294. u32 last, cur;
  3295. int rc;
  3296. t = ata_deadline(jiffies, params[2]);
  3297. if (time_before(t, deadline))
  3298. deadline = t;
  3299. if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
  3300. return rc;
  3301. cur &= 0xf;
  3302. last = cur;
  3303. last_jiffies = jiffies;
  3304. while (1) {
  3305. ata_msleep(link->ap, interval);
  3306. if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
  3307. return rc;
  3308. cur &= 0xf;
  3309. /* DET stable? */
  3310. if (cur == last) {
  3311. if (cur == 1 && time_before(jiffies, deadline))
  3312. continue;
  3313. if (time_after(jiffies,
  3314. ata_deadline(last_jiffies, duration)))
  3315. return 0;
  3316. continue;
  3317. }
  3318. /* unstable, start over */
  3319. last = cur;
  3320. last_jiffies = jiffies;
  3321. /* Check deadline. If debouncing failed, return
  3322. * -EPIPE to tell upper layer to lower link speed.
  3323. */
  3324. if (time_after(jiffies, deadline))
  3325. return -EPIPE;
  3326. }
  3327. }
  3328. /**
  3329. * sata_link_resume - resume SATA link
  3330. * @link: ATA link to resume SATA
  3331. * @params: timing parameters { interval, duration, timeout } in msec
  3332. * @deadline: deadline jiffies for the operation
  3333. *
  3334. * Resume SATA phy @link and debounce it.
  3335. *
  3336. * LOCKING:
  3337. * Kernel thread context (may sleep)
  3338. *
  3339. * RETURNS:
  3340. * 0 on success, -errno on failure.
  3341. */
  3342. int sata_link_resume(struct ata_link *link, const unsigned long *params,
  3343. unsigned long deadline)
  3344. {
  3345. int tries = ATA_LINK_RESUME_TRIES;
  3346. u32 scontrol, serror;
  3347. int rc;
  3348. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  3349. return rc;
  3350. /*
  3351. * Writes to SControl sometimes get ignored under certain
  3352. * controllers (ata_piix SIDPR). Make sure DET actually is
  3353. * cleared.
  3354. */
  3355. do {
  3356. scontrol = (scontrol & 0x0f0) | 0x300;
  3357. if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
  3358. return rc;
  3359. /*
  3360. * Some PHYs react badly if SStatus is pounded
  3361. * immediately after resuming. Delay 200ms before
  3362. * debouncing.
  3363. */
  3364. if (!(link->flags & ATA_LFLAG_NO_DB_DELAY))
  3365. ata_msleep(link->ap, 200);
  3366. /* is SControl restored correctly? */
  3367. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  3368. return rc;
  3369. } while ((scontrol & 0xf0f) != 0x300 && --tries);
  3370. if ((scontrol & 0xf0f) != 0x300) {
  3371. ata_link_warn(link, "failed to resume link (SControl %X)\n",
  3372. scontrol);
  3373. return 0;
  3374. }
  3375. if (tries < ATA_LINK_RESUME_TRIES)
  3376. ata_link_warn(link, "link resume succeeded after %d retries\n",
  3377. ATA_LINK_RESUME_TRIES - tries);
  3378. if ((rc = sata_link_debounce(link, params, deadline)))
  3379. return rc;
  3380. /* clear SError, some PHYs require this even for SRST to work */
  3381. if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
  3382. rc = sata_scr_write(link, SCR_ERROR, serror);
  3383. return rc != -EINVAL ? rc : 0;
  3384. }
  3385. /**
  3386. * sata_link_scr_lpm - manipulate SControl IPM and SPM fields
  3387. * @link: ATA link to manipulate SControl for
  3388. * @policy: LPM policy to configure
  3389. * @spm_wakeup: initiate LPM transition to active state
  3390. *
  3391. * Manipulate the IPM field of the SControl register of @link
  3392. * according to @policy. If @policy is ATA_LPM_MAX_POWER and
  3393. * @spm_wakeup is %true, the SPM field is manipulated to wake up
  3394. * the link. This function also clears PHYRDY_CHG before
  3395. * returning.
  3396. *
  3397. * LOCKING:
  3398. * EH context.
  3399. *
  3400. * RETURNS:
  3401. * 0 on success, -errno otherwise.
  3402. */
  3403. int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
  3404. bool spm_wakeup)
  3405. {
  3406. struct ata_eh_context *ehc = &link->eh_context;
  3407. bool woken_up = false;
  3408. u32 scontrol;
  3409. int rc;
  3410. rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
  3411. if (rc)
  3412. return rc;
  3413. switch (policy) {
  3414. case ATA_LPM_MAX_POWER:
  3415. /* disable all LPM transitions */
  3416. scontrol |= (0x7 << 8);
  3417. /* initiate transition to active state */
  3418. if (spm_wakeup) {
  3419. scontrol |= (0x4 << 12);
  3420. woken_up = true;
  3421. }
  3422. break;
  3423. case ATA_LPM_MED_POWER:
  3424. /* allow LPM to PARTIAL */
  3425. scontrol &= ~(0x1 << 8);
  3426. scontrol |= (0x6 << 8);
  3427. break;
  3428. case ATA_LPM_MED_POWER_WITH_DIPM:
  3429. case ATA_LPM_MIN_POWER_WITH_PARTIAL:
  3430. case ATA_LPM_MIN_POWER:
  3431. if (ata_link_nr_enabled(link) > 0)
  3432. /* no restrictions on LPM transitions */
  3433. scontrol &= ~(0x7 << 8);
  3434. else {
  3435. /* empty port, power off */
  3436. scontrol &= ~0xf;
  3437. scontrol |= (0x1 << 2);
  3438. }
  3439. break;
  3440. default:
  3441. WARN_ON(1);
  3442. }
  3443. rc = sata_scr_write(link, SCR_CONTROL, scontrol);
  3444. if (rc)
  3445. return rc;
  3446. /* give the link time to transit out of LPM state */
  3447. if (woken_up)
  3448. msleep(10);
  3449. /* clear PHYRDY_CHG from SError */
  3450. ehc->i.serror &= ~SERR_PHYRDY_CHG;
  3451. return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
  3452. }
  3453. /**
  3454. * ata_std_prereset - prepare for reset
  3455. * @link: ATA link to be reset
  3456. * @deadline: deadline jiffies for the operation
  3457. *
  3458. * @link is about to be reset. Initialize it. Failure from
  3459. * prereset makes libata abort whole reset sequence and give up
  3460. * that port, so prereset should be best-effort. It does its
  3461. * best to prepare for reset sequence but if things go wrong, it
  3462. * should just whine, not fail.
  3463. *
  3464. * LOCKING:
  3465. * Kernel thread context (may sleep)
  3466. *
  3467. * RETURNS:
  3468. * 0 on success, -errno otherwise.
  3469. */
  3470. int ata_std_prereset(struct ata_link *link, unsigned long deadline)
  3471. {
  3472. struct ata_port *ap = link->ap;
  3473. struct ata_eh_context *ehc = &link->eh_context;
  3474. const unsigned long *timing = sata_ehc_deb_timing(ehc);
  3475. int rc;
  3476. /* if we're about to do hardreset, nothing more to do */
  3477. if (ehc->i.action & ATA_EH_HARDRESET)
  3478. return 0;
  3479. /* if SATA, resume link */
  3480. if (ap->flags & ATA_FLAG_SATA) {
  3481. rc = sata_link_resume(link, timing, deadline);
  3482. /* whine about phy resume failure but proceed */
  3483. if (rc && rc != -EOPNOTSUPP)
  3484. ata_link_warn(link,
  3485. "failed to resume link for reset (errno=%d)\n",
  3486. rc);
  3487. }
  3488. /* no point in trying softreset on offline link */
  3489. if (ata_phys_link_offline(link))
  3490. ehc->i.action &= ~ATA_EH_SOFTRESET;
  3491. return 0;
  3492. }
  3493. /**
  3494. * sata_link_hardreset - reset link via SATA phy reset
  3495. * @link: link to reset
  3496. * @timing: timing parameters { interval, duration, timeout } in msec
  3497. * @deadline: deadline jiffies for the operation
  3498. * @online: optional out parameter indicating link onlineness
  3499. * @check_ready: optional callback to check link readiness
  3500. *
  3501. * SATA phy-reset @link using DET bits of SControl register.
  3502. * After hardreset, link readiness is waited upon using
  3503. * ata_wait_ready() if @check_ready is specified. LLDs are
  3504. * allowed to not specify @check_ready and wait itself after this
  3505. * function returns. Device classification is LLD's
  3506. * responsibility.
  3507. *
  3508. * *@online is set to one iff reset succeeded and @link is online
  3509. * after reset.
  3510. *
  3511. * LOCKING:
  3512. * Kernel thread context (may sleep)
  3513. *
  3514. * RETURNS:
  3515. * 0 on success, -errno otherwise.
  3516. */
  3517. int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
  3518. unsigned long deadline,
  3519. bool *online, int (*check_ready)(struct ata_link *))
  3520. {
  3521. u32 scontrol;
  3522. int rc;
  3523. DPRINTK("ENTER\n");
  3524. if (online)
  3525. *online = false;
  3526. if (sata_set_spd_needed(link)) {
  3527. /* SATA spec says nothing about how to reconfigure
  3528. * spd. To be on the safe side, turn off phy during
  3529. * reconfiguration. This works for at least ICH7 AHCI
  3530. * and Sil3124.
  3531. */
  3532. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  3533. goto out;
  3534. scontrol = (scontrol & 0x0f0) | 0x304;
  3535. if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
  3536. goto out;
  3537. sata_set_spd(link);
  3538. }
  3539. /* issue phy wake/reset */
  3540. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  3541. goto out;
  3542. scontrol = (scontrol & 0x0f0) | 0x301;
  3543. if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
  3544. goto out;
  3545. /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
  3546. * 10.4.2 says at least 1 ms.
  3547. */
  3548. ata_msleep(link->ap, 1);
  3549. /* bring link back */
  3550. rc = sata_link_resume(link, timing, deadline);
  3551. if (rc)
  3552. goto out;
  3553. /* if link is offline nothing more to do */
  3554. if (ata_phys_link_offline(link))
  3555. goto out;
  3556. /* Link is online. From this point, -ENODEV too is an error. */
  3557. if (online)
  3558. *online = true;
  3559. if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
  3560. /* If PMP is supported, we have to do follow-up SRST.
  3561. * Some PMPs don't send D2H Reg FIS after hardreset if
  3562. * the first port is empty. Wait only for
  3563. * ATA_TMOUT_PMP_SRST_WAIT.
  3564. */
  3565. if (check_ready) {
  3566. unsigned long pmp_deadline;
  3567. pmp_deadline = ata_deadline(jiffies,
  3568. ATA_TMOUT_PMP_SRST_WAIT);
  3569. if (time_after(pmp_deadline, deadline))
  3570. pmp_deadline = deadline;
  3571. ata_wait_ready(link, pmp_deadline, check_ready);
  3572. }
  3573. rc = -EAGAIN;
  3574. goto out;
  3575. }
  3576. rc = 0;
  3577. if (check_ready)
  3578. rc = ata_wait_ready(link, deadline, check_ready);
  3579. out:
  3580. if (rc && rc != -EAGAIN) {
  3581. /* online is set iff link is online && reset succeeded */
  3582. if (online)
  3583. *online = false;
  3584. ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
  3585. }
  3586. DPRINTK("EXIT, rc=%d\n", rc);
  3587. return rc;
  3588. }
  3589. /**
  3590. * sata_std_hardreset - COMRESET w/o waiting or classification
  3591. * @link: link to reset
  3592. * @class: resulting class of attached device
  3593. * @deadline: deadline jiffies for the operation
  3594. *
  3595. * Standard SATA COMRESET w/o waiting or classification.
  3596. *
  3597. * LOCKING:
  3598. * Kernel thread context (may sleep)
  3599. *
  3600. * RETURNS:
  3601. * 0 if link offline, -EAGAIN if link online, -errno on errors.
  3602. */
  3603. int sata_std_hardreset(struct ata_link *link, unsigned int *class,
  3604. unsigned long deadline)
  3605. {
  3606. const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
  3607. bool online;
  3608. int rc;
  3609. /* do hardreset */
  3610. rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
  3611. return online ? -EAGAIN : rc;
  3612. }
  3613. /**
  3614. * ata_std_postreset - standard postreset callback
  3615. * @link: the target ata_link
  3616. * @classes: classes of attached devices
  3617. *
  3618. * This function is invoked after a successful reset. Note that
  3619. * the device might have been reset more than once using
  3620. * different reset methods before postreset is invoked.
  3621. *
  3622. * LOCKING:
  3623. * Kernel thread context (may sleep)
  3624. */
  3625. void ata_std_postreset(struct ata_link *link, unsigned int *classes)
  3626. {
  3627. u32 serror;
  3628. DPRINTK("ENTER\n");
  3629. /* reset complete, clear SError */
  3630. if (!sata_scr_read(link, SCR_ERROR, &serror))
  3631. sata_scr_write(link, SCR_ERROR, serror);
  3632. /* print link status */
  3633. sata_print_link_status(link);
  3634. DPRINTK("EXIT\n");
  3635. }
  3636. /**
  3637. * ata_dev_same_device - Determine whether new ID matches configured device
  3638. * @dev: device to compare against
  3639. * @new_class: class of the new device
  3640. * @new_id: IDENTIFY page of the new device
  3641. *
  3642. * Compare @new_class and @new_id against @dev and determine
  3643. * whether @dev is the device indicated by @new_class and
  3644. * @new_id.
  3645. *
  3646. * LOCKING:
  3647. * None.
  3648. *
  3649. * RETURNS:
  3650. * 1 if @dev matches @new_class and @new_id, 0 otherwise.
  3651. */
  3652. static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
  3653. const u16 *new_id)
  3654. {
  3655. const u16 *old_id = dev->id;
  3656. unsigned char model[2][ATA_ID_PROD_LEN + 1];
  3657. unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
  3658. if (dev->class != new_class) {
  3659. ata_dev_info(dev, "class mismatch %d != %d\n",
  3660. dev->class, new_class);
  3661. return 0;
  3662. }
  3663. ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
  3664. ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
  3665. ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
  3666. ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
  3667. if (strcmp(model[0], model[1])) {
  3668. ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
  3669. model[0], model[1]);
  3670. return 0;
  3671. }
  3672. if (strcmp(serial[0], serial[1])) {
  3673. ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
  3674. serial[0], serial[1]);
  3675. return 0;
  3676. }
  3677. return 1;
  3678. }
  3679. /**
  3680. * ata_dev_reread_id - Re-read IDENTIFY data
  3681. * @dev: target ATA device
  3682. * @readid_flags: read ID flags
  3683. *
  3684. * Re-read IDENTIFY page and make sure @dev is still attached to
  3685. * the port.
  3686. *
  3687. * LOCKING:
  3688. * Kernel thread context (may sleep)
  3689. *
  3690. * RETURNS:
  3691. * 0 on success, negative errno otherwise
  3692. */
  3693. int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
  3694. {
  3695. unsigned int class = dev->class;
  3696. u16 *id = (void *)dev->link->ap->sector_buf;
  3697. int rc;
  3698. /* read ID data */
  3699. rc = ata_dev_read_id(dev, &class, readid_flags, id);
  3700. if (rc)
  3701. return rc;
  3702. /* is the device still there? */
  3703. if (!ata_dev_same_device(dev, class, id))
  3704. return -ENODEV;
  3705. memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
  3706. return 0;
  3707. }
  3708. /**
  3709. * ata_dev_revalidate - Revalidate ATA device
  3710. * @dev: device to revalidate
  3711. * @new_class: new class code
  3712. * @readid_flags: read ID flags
  3713. *
  3714. * Re-read IDENTIFY page, make sure @dev is still attached to the
  3715. * port and reconfigure it according to the new IDENTIFY page.
  3716. *
  3717. * LOCKING:
  3718. * Kernel thread context (may sleep)
  3719. *
  3720. * RETURNS:
  3721. * 0 on success, negative errno otherwise
  3722. */
  3723. int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
  3724. unsigned int readid_flags)
  3725. {
  3726. u64 n_sectors = dev->n_sectors;
  3727. u64 n_native_sectors = dev->n_native_sectors;
  3728. int rc;
  3729. if (!ata_dev_enabled(dev))
  3730. return -ENODEV;
  3731. /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
  3732. if (ata_class_enabled(new_class) &&
  3733. new_class != ATA_DEV_ATA &&
  3734. new_class != ATA_DEV_ATAPI &&
  3735. new_class != ATA_DEV_ZAC &&
  3736. new_class != ATA_DEV_SEMB) {
  3737. ata_dev_info(dev, "class mismatch %u != %u\n",
  3738. dev->class, new_class);
  3739. rc = -ENODEV;
  3740. goto fail;
  3741. }
  3742. /* re-read ID */
  3743. rc = ata_dev_reread_id(dev, readid_flags);
  3744. if (rc)
  3745. goto fail;
  3746. /* configure device according to the new ID */
  3747. rc = ata_dev_configure(dev);
  3748. if (rc)
  3749. goto fail;
  3750. /* verify n_sectors hasn't changed */
  3751. if (dev->class != ATA_DEV_ATA || !n_sectors ||
  3752. dev->n_sectors == n_sectors)
  3753. return 0;
  3754. /* n_sectors has changed */
  3755. ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
  3756. (unsigned long long)n_sectors,
  3757. (unsigned long long)dev->n_sectors);
  3758. /*
  3759. * Something could have caused HPA to be unlocked
  3760. * involuntarily. If n_native_sectors hasn't changed and the
  3761. * new size matches it, keep the device.
  3762. */
  3763. if (dev->n_native_sectors == n_native_sectors &&
  3764. dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
  3765. ata_dev_warn(dev,
  3766. "new n_sectors matches native, probably "
  3767. "late HPA unlock, n_sectors updated\n");
  3768. /* use the larger n_sectors */
  3769. return 0;
  3770. }
  3771. /*
  3772. * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try
  3773. * unlocking HPA in those cases.
  3774. *
  3775. * https://bugzilla.kernel.org/show_bug.cgi?id=15396
  3776. */
  3777. if (dev->n_native_sectors == n_native_sectors &&
  3778. dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
  3779. !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
  3780. ata_dev_warn(dev,
  3781. "old n_sectors matches native, probably "
  3782. "late HPA lock, will try to unlock HPA\n");
  3783. /* try unlocking HPA */
  3784. dev->flags |= ATA_DFLAG_UNLOCK_HPA;
  3785. rc = -EIO;
  3786. } else
  3787. rc = -ENODEV;
  3788. /* restore original n_[native_]sectors and fail */
  3789. dev->n_native_sectors = n_native_sectors;
  3790. dev->n_sectors = n_sectors;
  3791. fail:
  3792. ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
  3793. return rc;
  3794. }
  3795. struct ata_blacklist_entry {
  3796. const char *model_num;
  3797. const char *model_rev;
  3798. unsigned long horkage;
  3799. };
  3800. static const struct ata_blacklist_entry ata_device_blacklist [] = {
  3801. /* Devices with DMA related problems under Linux */
  3802. { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
  3803. { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
  3804. { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
  3805. { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
  3806. { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
  3807. { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
  3808. { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
  3809. { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
  3810. { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
  3811. { "CRD-848[02]B", NULL, ATA_HORKAGE_NODMA },
  3812. { "CRD-84", NULL, ATA_HORKAGE_NODMA },
  3813. { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
  3814. { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
  3815. { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
  3816. { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
  3817. { "HITACHI CDR-8[34]35",NULL, ATA_HORKAGE_NODMA },
  3818. { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
  3819. { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
  3820. { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
  3821. { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
  3822. { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
  3823. { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
  3824. { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
  3825. { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
  3826. { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
  3827. { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
  3828. { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
  3829. { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
  3830. { " 2GB ATA Flash Disk", "ADMA428M", ATA_HORKAGE_NODMA },
  3831. { "VRFDFC22048UCHC-TE*", NULL, ATA_HORKAGE_NODMA },
  3832. /* Odd clown on sil3726/4726 PMPs */
  3833. { "Config Disk", NULL, ATA_HORKAGE_DISABLE },
  3834. /* Weird ATAPI devices */
  3835. { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
  3836. { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA },
  3837. { "Slimtype DVD A DS8A8SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
  3838. { "Slimtype DVD A DS8A9SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
  3839. /*
  3840. * Causes silent data corruption with higher max sects.
  3841. * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
  3842. */
  3843. { "ST380013AS", "3.20", ATA_HORKAGE_MAX_SEC_1024 },
  3844. /*
  3845. * These devices time out with higher max sects.
  3846. * https://bugzilla.kernel.org/show_bug.cgi?id=121671
  3847. */
  3848. { "LITEON CX1-JB*-HP", NULL, ATA_HORKAGE_MAX_SEC_1024 },
  3849. { "LITEON EP1-*", NULL, ATA_HORKAGE_MAX_SEC_1024 },
  3850. /* Devices we expect to fail diagnostics */
  3851. /* Devices where NCQ should be avoided */
  3852. /* NCQ is slow */
  3853. { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
  3854. { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
  3855. /* http://thread.gmane.org/gmane.linux.ide/14907 */
  3856. { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
  3857. /* NCQ is broken */
  3858. { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
  3859. { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
  3860. { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
  3861. { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
  3862. { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ },
  3863. /* Seagate NCQ + FLUSH CACHE firmware bug */
  3864. { "ST31500341AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
  3865. ATA_HORKAGE_FIRMWARE_WARN },
  3866. { "ST31000333AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
  3867. ATA_HORKAGE_FIRMWARE_WARN },
  3868. { "ST3640[36]23AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
  3869. ATA_HORKAGE_FIRMWARE_WARN },
  3870. { "ST3320[68]13AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
  3871. ATA_HORKAGE_FIRMWARE_WARN },
  3872. /* drives which fail FPDMA_AA activation (some may freeze afterwards) */
  3873. { "ST1000LM024 HN-M101MBB", "2AR10001", ATA_HORKAGE_BROKEN_FPDMA_AA },
  3874. { "ST1000LM024 HN-M101MBB", "2BA30001", ATA_HORKAGE_BROKEN_FPDMA_AA },
  3875. { "VB0250EAVER", "HPG7", ATA_HORKAGE_BROKEN_FPDMA_AA },
  3876. /* Blacklist entries taken from Silicon Image 3124/3132
  3877. Windows driver .inf file - also several Linux problem reports */
  3878. { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
  3879. { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
  3880. { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
  3881. /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
  3882. { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ, },
  3883. /* Some Sandisk SSDs lock up hard with NCQ enabled. Reported on
  3884. SD7SN6S256G and SD8SN8U256G */
  3885. { "SanDisk SD[78]SN*G", NULL, ATA_HORKAGE_NONCQ, },
  3886. /* devices which puke on READ_NATIVE_MAX */
  3887. { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
  3888. { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
  3889. { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
  3890. { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
  3891. /* this one allows HPA unlocking but fails IOs on the area */
  3892. { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA },
  3893. /* Devices which report 1 sector over size HPA */
  3894. { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
  3895. { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
  3896. { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
  3897. /* Devices which get the IVB wrong */
  3898. { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
  3899. /* Maybe we should just blacklist TSSTcorp... */
  3900. { "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]", ATA_HORKAGE_IVB, },
  3901. /* Devices that do not need bridging limits applied */
  3902. { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK, },
  3903. { "BUFFALO HD-QSU2/R5", NULL, ATA_HORKAGE_BRIDGE_OK, },
  3904. /* Devices which aren't very happy with higher link speeds */
  3905. { "WD My Book", NULL, ATA_HORKAGE_1_5_GBPS, },
  3906. { "Seagate FreeAgent GoFlex", NULL, ATA_HORKAGE_1_5_GBPS, },
  3907. /*
  3908. * Devices which choke on SETXFER. Applies only if both the
  3909. * device and controller are SATA.
  3910. */
  3911. { "PIONEER DVD-RW DVRTD08", NULL, ATA_HORKAGE_NOSETXFER },
  3912. { "PIONEER DVD-RW DVRTD08A", NULL, ATA_HORKAGE_NOSETXFER },
  3913. { "PIONEER DVD-RW DVR-215", NULL, ATA_HORKAGE_NOSETXFER },
  3914. { "PIONEER DVD-RW DVR-212D", NULL, ATA_HORKAGE_NOSETXFER },
  3915. { "PIONEER DVD-RW DVR-216D", NULL, ATA_HORKAGE_NOSETXFER },
  3916. /* Crucial BX100 SSD 500GB has broken LPM support */
  3917. { "CT500BX100SSD1", NULL, ATA_HORKAGE_NOLPM },
  3918. /* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
  3919. { "Crucial_CT512MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
  3920. ATA_HORKAGE_ZERO_AFTER_TRIM |
  3921. ATA_HORKAGE_NOLPM, },
  3922. /* 512GB MX100 with newer firmware has only LPM issues */
  3923. { "Crucial_CT512MX100*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM |
  3924. ATA_HORKAGE_NOLPM, },
  3925. /* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
  3926. { "Crucial_CT480M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
  3927. ATA_HORKAGE_ZERO_AFTER_TRIM |
  3928. ATA_HORKAGE_NOLPM, },
  3929. { "Crucial_CT960M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
  3930. ATA_HORKAGE_ZERO_AFTER_TRIM |
  3931. ATA_HORKAGE_NOLPM, },
  3932. /* These specific Samsung models/firmware-revs do not handle LPM well */
  3933. { "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM, },
  3934. { "SAMSUNG SSD PM830 mSATA *", "CXM13D1Q", ATA_HORKAGE_NOLPM, },
  3935. { "SAMSUNG MZ7TD256HAFV-000L9", NULL, ATA_HORKAGE_NOLPM, },
  3936. /* devices that don't properly handle queued TRIM commands */
  3937. { "Micron_M500IT_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
  3938. ATA_HORKAGE_ZERO_AFTER_TRIM, },
  3939. { "Micron_M500_*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
  3940. ATA_HORKAGE_ZERO_AFTER_TRIM, },
  3941. { "Crucial_CT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
  3942. ATA_HORKAGE_ZERO_AFTER_TRIM, },
  3943. { "Micron_M5[15]0_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
  3944. ATA_HORKAGE_ZERO_AFTER_TRIM, },
  3945. { "Crucial_CT*M550*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
  3946. ATA_HORKAGE_ZERO_AFTER_TRIM, },
  3947. { "Crucial_CT*MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
  3948. ATA_HORKAGE_ZERO_AFTER_TRIM, },
  3949. { "Samsung SSD 840*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
  3950. ATA_HORKAGE_ZERO_AFTER_TRIM, },
  3951. { "Samsung SSD 850*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
  3952. ATA_HORKAGE_ZERO_AFTER_TRIM, },
  3953. { "FCCT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
  3954. ATA_HORKAGE_ZERO_AFTER_TRIM, },
  3955. /* devices that don't properly handle TRIM commands */
  3956. { "SuperSSpeed S238*", NULL, ATA_HORKAGE_NOTRIM, },
  3957. /*
  3958. * As defined, the DRAT (Deterministic Read After Trim) and RZAT
  3959. * (Return Zero After Trim) flags in the ATA Command Set are
  3960. * unreliable in the sense that they only define what happens if
  3961. * the device successfully executed the DSM TRIM command. TRIM
  3962. * is only advisory, however, and the device is free to silently
  3963. * ignore all or parts of the request.
  3964. *
  3965. * Whitelist drives that are known to reliably return zeroes
  3966. * after TRIM.
  3967. */
  3968. /*
  3969. * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
  3970. * that model before whitelisting all other intel SSDs.
  3971. */
  3972. { "INTEL*SSDSC2MH*", NULL, 0, },
  3973. { "Micron*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
  3974. { "Crucial*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
  3975. { "INTEL*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
  3976. { "SSD*INTEL*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
  3977. { "Samsung*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
  3978. { "SAMSUNG*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
  3979. { "SAMSUNG*MZ7KM*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
  3980. { "ST[1248][0248]0[FH]*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
  3981. /*
  3982. * Some WD SATA-I drives spin up and down erratically when the link
  3983. * is put into the slumber mode. We don't have full list of the
  3984. * affected devices. Disable LPM if the device matches one of the
  3985. * known prefixes and is SATA-1. As a side effect LPM partial is
  3986. * lost too.
  3987. *
  3988. * https://bugzilla.kernel.org/show_bug.cgi?id=57211
  3989. */
  3990. { "WDC WD800JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
  3991. { "WDC WD1200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
  3992. { "WDC WD1600JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
  3993. { "WDC WD2000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
  3994. { "WDC WD2500JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
  3995. { "WDC WD3000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
  3996. { "WDC WD3200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
  3997. /* End Marker */
  3998. { }
  3999. };
  4000. static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
  4001. {
  4002. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  4003. unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
  4004. const struct ata_blacklist_entry *ad = ata_device_blacklist;
  4005. ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
  4006. ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
  4007. while (ad->model_num) {
  4008. if (glob_match(ad->model_num, model_num)) {
  4009. if (ad->model_rev == NULL)
  4010. return ad->horkage;
  4011. if (glob_match(ad->model_rev, model_rev))
  4012. return ad->horkage;
  4013. }
  4014. ad++;
  4015. }
  4016. return 0;
  4017. }
  4018. static int ata_dma_blacklisted(const struct ata_device *dev)
  4019. {
  4020. /* We don't support polling DMA.
  4021. * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
  4022. * if the LLDD handles only interrupts in the HSM_ST_LAST state.
  4023. */
  4024. if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
  4025. (dev->flags & ATA_DFLAG_CDB_INTR))
  4026. return 1;
  4027. return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
  4028. }
  4029. /**
  4030. * ata_is_40wire - check drive side detection
  4031. * @dev: device
  4032. *
  4033. * Perform drive side detection decoding, allowing for device vendors
  4034. * who can't follow the documentation.
  4035. */
  4036. static int ata_is_40wire(struct ata_device *dev)
  4037. {
  4038. if (dev->horkage & ATA_HORKAGE_IVB)
  4039. return ata_drive_40wire_relaxed(dev->id);
  4040. return ata_drive_40wire(dev->id);
  4041. }
  4042. /**
  4043. * cable_is_40wire - 40/80/SATA decider
  4044. * @ap: port to consider
  4045. *
  4046. * This function encapsulates the policy for speed management
  4047. * in one place. At the moment we don't cache the result but
  4048. * there is a good case for setting ap->cbl to the result when
  4049. * we are called with unknown cables (and figuring out if it
  4050. * impacts hotplug at all).
  4051. *
  4052. * Return 1 if the cable appears to be 40 wire.
  4053. */
  4054. static int cable_is_40wire(struct ata_port *ap)
  4055. {
  4056. struct ata_link *link;
  4057. struct ata_device *dev;
  4058. /* If the controller thinks we are 40 wire, we are. */
  4059. if (ap->cbl == ATA_CBL_PATA40)
  4060. return 1;
  4061. /* If the controller thinks we are 80 wire, we are. */
  4062. if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
  4063. return 0;
  4064. /* If the system is known to be 40 wire short cable (eg
  4065. * laptop), then we allow 80 wire modes even if the drive
  4066. * isn't sure.
  4067. */
  4068. if (ap->cbl == ATA_CBL_PATA40_SHORT)
  4069. return 0;
  4070. /* If the controller doesn't know, we scan.
  4071. *
  4072. * Note: We look for all 40 wire detects at this point. Any
  4073. * 80 wire detect is taken to be 80 wire cable because
  4074. * - in many setups only the one drive (slave if present) will
  4075. * give a valid detect
  4076. * - if you have a non detect capable drive you don't want it
  4077. * to colour the choice
  4078. */
  4079. ata_for_each_link(link, ap, EDGE) {
  4080. ata_for_each_dev(dev, link, ENABLED) {
  4081. if (!ata_is_40wire(dev))
  4082. return 0;
  4083. }
  4084. }
  4085. return 1;
  4086. }
  4087. /**
  4088. * ata_dev_xfermask - Compute supported xfermask of the given device
  4089. * @dev: Device to compute xfermask for
  4090. *
  4091. * Compute supported xfermask of @dev and store it in
  4092. * dev->*_mask. This function is responsible for applying all
  4093. * known limits including host controller limits, device
  4094. * blacklist, etc...
  4095. *
  4096. * LOCKING:
  4097. * None.
  4098. */
  4099. static void ata_dev_xfermask(struct ata_device *dev)
  4100. {
  4101. struct ata_link *link = dev->link;
  4102. struct ata_port *ap = link->ap;
  4103. struct ata_host *host = ap->host;
  4104. unsigned long xfer_mask;
  4105. /* controller modes available */
  4106. xfer_mask = ata_pack_xfermask(ap->pio_mask,
  4107. ap->mwdma_mask, ap->udma_mask);
  4108. /* drive modes available */
  4109. xfer_mask &= ata_pack_xfermask(dev->pio_mask,
  4110. dev->mwdma_mask, dev->udma_mask);
  4111. xfer_mask &= ata_id_xfermask(dev->id);
  4112. /*
  4113. * CFA Advanced TrueIDE timings are not allowed on a shared
  4114. * cable
  4115. */
  4116. if (ata_dev_pair(dev)) {
  4117. /* No PIO5 or PIO6 */
  4118. xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
  4119. /* No MWDMA3 or MWDMA 4 */
  4120. xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
  4121. }
  4122. if (ata_dma_blacklisted(dev)) {
  4123. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  4124. ata_dev_warn(dev,
  4125. "device is on DMA blacklist, disabling DMA\n");
  4126. }
  4127. if ((host->flags & ATA_HOST_SIMPLEX) &&
  4128. host->simplex_claimed && host->simplex_claimed != ap) {
  4129. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  4130. ata_dev_warn(dev,
  4131. "simplex DMA is claimed by other device, disabling DMA\n");
  4132. }
  4133. if (ap->flags & ATA_FLAG_NO_IORDY)
  4134. xfer_mask &= ata_pio_mask_no_iordy(dev);
  4135. if (ap->ops->mode_filter)
  4136. xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
  4137. /* Apply cable rule here. Don't apply it early because when
  4138. * we handle hot plug the cable type can itself change.
  4139. * Check this last so that we know if the transfer rate was
  4140. * solely limited by the cable.
  4141. * Unknown or 80 wire cables reported host side are checked
  4142. * drive side as well. Cases where we know a 40wire cable
  4143. * is used safely for 80 are not checked here.
  4144. */
  4145. if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
  4146. /* UDMA/44 or higher would be available */
  4147. if (cable_is_40wire(ap)) {
  4148. ata_dev_warn(dev,
  4149. "limited to UDMA/33 due to 40-wire cable\n");
  4150. xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
  4151. }
  4152. ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
  4153. &dev->mwdma_mask, &dev->udma_mask);
  4154. }
  4155. /**
  4156. * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
  4157. * @dev: Device to which command will be sent
  4158. *
  4159. * Issue SET FEATURES - XFER MODE command to device @dev
  4160. * on port @ap.
  4161. *
  4162. * LOCKING:
  4163. * PCI/etc. bus probe sem.
  4164. *
  4165. * RETURNS:
  4166. * 0 on success, AC_ERR_* mask otherwise.
  4167. */
  4168. static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
  4169. {
  4170. struct ata_taskfile tf;
  4171. unsigned int err_mask;
  4172. /* set up set-features taskfile */
  4173. DPRINTK("set features - xfer mode\n");
  4174. /* Some controllers and ATAPI devices show flaky interrupt
  4175. * behavior after setting xfer mode. Use polling instead.
  4176. */
  4177. ata_tf_init(dev, &tf);
  4178. tf.command = ATA_CMD_SET_FEATURES;
  4179. tf.feature = SETFEATURES_XFER;
  4180. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
  4181. tf.protocol = ATA_PROT_NODATA;
  4182. /* If we are using IORDY we must send the mode setting command */
  4183. if (ata_pio_need_iordy(dev))
  4184. tf.nsect = dev->xfer_mode;
  4185. /* If the device has IORDY and the controller does not - turn it off */
  4186. else if (ata_id_has_iordy(dev->id))
  4187. tf.nsect = 0x01;
  4188. else /* In the ancient relic department - skip all of this */
  4189. return 0;
  4190. /* On some disks, this command causes spin-up, so we need longer timeout */
  4191. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
  4192. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  4193. return err_mask;
  4194. }
  4195. /**
  4196. * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
  4197. * @dev: Device to which command will be sent
  4198. * @enable: Whether to enable or disable the feature
  4199. * @feature: The sector count represents the feature to set
  4200. *
  4201. * Issue SET FEATURES - SATA FEATURES command to device @dev
  4202. * on port @ap with sector count
  4203. *
  4204. * LOCKING:
  4205. * PCI/etc. bus probe sem.
  4206. *
  4207. * RETURNS:
  4208. * 0 on success, AC_ERR_* mask otherwise.
  4209. */
  4210. unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
  4211. {
  4212. struct ata_taskfile tf;
  4213. unsigned int err_mask;
  4214. unsigned long timeout = 0;
  4215. /* set up set-features taskfile */
  4216. DPRINTK("set features - SATA features\n");
  4217. ata_tf_init(dev, &tf);
  4218. tf.command = ATA_CMD_SET_FEATURES;
  4219. tf.feature = enable;
  4220. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  4221. tf.protocol = ATA_PROT_NODATA;
  4222. tf.nsect = feature;
  4223. if (enable == SETFEATURES_SPINUP)
  4224. timeout = ata_probe_timeout ?
  4225. ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
  4226. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
  4227. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  4228. return err_mask;
  4229. }
  4230. EXPORT_SYMBOL_GPL(ata_dev_set_feature);
  4231. /**
  4232. * ata_dev_init_params - Issue INIT DEV PARAMS command
  4233. * @dev: Device to which command will be sent
  4234. * @heads: Number of heads (taskfile parameter)
  4235. * @sectors: Number of sectors (taskfile parameter)
  4236. *
  4237. * LOCKING:
  4238. * Kernel thread context (may sleep)
  4239. *
  4240. * RETURNS:
  4241. * 0 on success, AC_ERR_* mask otherwise.
  4242. */
  4243. static unsigned int ata_dev_init_params(struct ata_device *dev,
  4244. u16 heads, u16 sectors)
  4245. {
  4246. struct ata_taskfile tf;
  4247. unsigned int err_mask;
  4248. /* Number of sectors per track 1-255. Number of heads 1-16 */
  4249. if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
  4250. return AC_ERR_INVALID;
  4251. /* set up init dev params taskfile */
  4252. DPRINTK("init dev params \n");
  4253. ata_tf_init(dev, &tf);
  4254. tf.command = ATA_CMD_INIT_DEV_PARAMS;
  4255. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  4256. tf.protocol = ATA_PROT_NODATA;
  4257. tf.nsect = sectors;
  4258. tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
  4259. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  4260. /* A clean abort indicates an original or just out of spec drive
  4261. and we should continue as we issue the setup based on the
  4262. drive reported working geometry */
  4263. if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
  4264. err_mask = 0;
  4265. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  4266. return err_mask;
  4267. }
  4268. /**
  4269. * atapi_check_dma - Check whether ATAPI DMA can be supported
  4270. * @qc: Metadata associated with taskfile to check
  4271. *
  4272. * Allow low-level driver to filter ATA PACKET commands, returning
  4273. * a status indicating whether or not it is OK to use DMA for the
  4274. * supplied PACKET command.
  4275. *
  4276. * LOCKING:
  4277. * spin_lock_irqsave(host lock)
  4278. *
  4279. * RETURNS: 0 when ATAPI DMA can be used
  4280. * nonzero otherwise
  4281. */
  4282. int atapi_check_dma(struct ata_queued_cmd *qc)
  4283. {
  4284. struct ata_port *ap = qc->ap;
  4285. /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
  4286. * few ATAPI devices choke on such DMA requests.
  4287. */
  4288. if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
  4289. unlikely(qc->nbytes & 15))
  4290. return 1;
  4291. if (ap->ops->check_atapi_dma)
  4292. return ap->ops->check_atapi_dma(qc);
  4293. return 0;
  4294. }
  4295. /**
  4296. * ata_std_qc_defer - Check whether a qc needs to be deferred
  4297. * @qc: ATA command in question
  4298. *
  4299. * Non-NCQ commands cannot run with any other command, NCQ or
  4300. * not. As upper layer only knows the queue depth, we are
  4301. * responsible for maintaining exclusion. This function checks
  4302. * whether a new command @qc can be issued.
  4303. *
  4304. * LOCKING:
  4305. * spin_lock_irqsave(host lock)
  4306. *
  4307. * RETURNS:
  4308. * ATA_DEFER_* if deferring is needed, 0 otherwise.
  4309. */
  4310. int ata_std_qc_defer(struct ata_queued_cmd *qc)
  4311. {
  4312. struct ata_link *link = qc->dev->link;
  4313. if (ata_is_ncq(qc->tf.protocol)) {
  4314. if (!ata_tag_valid(link->active_tag))
  4315. return 0;
  4316. } else {
  4317. if (!ata_tag_valid(link->active_tag) && !link->sactive)
  4318. return 0;
  4319. }
  4320. return ATA_DEFER_LINK;
  4321. }
  4322. void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
  4323. /**
  4324. * ata_sg_init - Associate command with scatter-gather table.
  4325. * @qc: Command to be associated
  4326. * @sg: Scatter-gather table.
  4327. * @n_elem: Number of elements in s/g table.
  4328. *
  4329. * Initialize the data-related elements of queued_cmd @qc
  4330. * to point to a scatter-gather table @sg, containing @n_elem
  4331. * elements.
  4332. *
  4333. * LOCKING:
  4334. * spin_lock_irqsave(host lock)
  4335. */
  4336. void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
  4337. unsigned int n_elem)
  4338. {
  4339. qc->sg = sg;
  4340. qc->n_elem = n_elem;
  4341. qc->cursg = qc->sg;
  4342. }
  4343. #ifdef CONFIG_HAS_DMA
  4344. /**
  4345. * ata_sg_clean - Unmap DMA memory associated with command
  4346. * @qc: Command containing DMA memory to be released
  4347. *
  4348. * Unmap all mapped DMA memory associated with this command.
  4349. *
  4350. * LOCKING:
  4351. * spin_lock_irqsave(host lock)
  4352. */
  4353. static void ata_sg_clean(struct ata_queued_cmd *qc)
  4354. {
  4355. struct ata_port *ap = qc->ap;
  4356. struct scatterlist *sg = qc->sg;
  4357. int dir = qc->dma_dir;
  4358. WARN_ON_ONCE(sg == NULL);
  4359. VPRINTK("unmapping %u sg elements\n", qc->n_elem);
  4360. if (qc->n_elem)
  4361. dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
  4362. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  4363. qc->sg = NULL;
  4364. }
  4365. /**
  4366. * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
  4367. * @qc: Command with scatter-gather table to be mapped.
  4368. *
  4369. * DMA-map the scatter-gather table associated with queued_cmd @qc.
  4370. *
  4371. * LOCKING:
  4372. * spin_lock_irqsave(host lock)
  4373. *
  4374. * RETURNS:
  4375. * Zero on success, negative on error.
  4376. *
  4377. */
  4378. static int ata_sg_setup(struct ata_queued_cmd *qc)
  4379. {
  4380. struct ata_port *ap = qc->ap;
  4381. unsigned int n_elem;
  4382. VPRINTK("ENTER, ata%u\n", ap->print_id);
  4383. n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
  4384. if (n_elem < 1)
  4385. return -1;
  4386. VPRINTK("%d sg elements mapped\n", n_elem);
  4387. qc->orig_n_elem = qc->n_elem;
  4388. qc->n_elem = n_elem;
  4389. qc->flags |= ATA_QCFLAG_DMAMAP;
  4390. return 0;
  4391. }
  4392. #else /* !CONFIG_HAS_DMA */
  4393. static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
  4394. static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
  4395. #endif /* !CONFIG_HAS_DMA */
  4396. /**
  4397. * swap_buf_le16 - swap halves of 16-bit words in place
  4398. * @buf: Buffer to swap
  4399. * @buf_words: Number of 16-bit words in buffer.
  4400. *
  4401. * Swap halves of 16-bit words if needed to convert from
  4402. * little-endian byte order to native cpu byte order, or
  4403. * vice-versa.
  4404. *
  4405. * LOCKING:
  4406. * Inherited from caller.
  4407. */
  4408. void swap_buf_le16(u16 *buf, unsigned int buf_words)
  4409. {
  4410. #ifdef __BIG_ENDIAN
  4411. unsigned int i;
  4412. for (i = 0; i < buf_words; i++)
  4413. buf[i] = le16_to_cpu(buf[i]);
  4414. #endif /* __BIG_ENDIAN */
  4415. }
  4416. /**
  4417. * ata_qc_new_init - Request an available ATA command, and initialize it
  4418. * @dev: Device from whom we request an available command structure
  4419. * @tag: tag
  4420. *
  4421. * LOCKING:
  4422. * None.
  4423. */
  4424. struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag)
  4425. {
  4426. struct ata_port *ap = dev->link->ap;
  4427. struct ata_queued_cmd *qc;
  4428. /* no command while frozen */
  4429. if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
  4430. return NULL;
  4431. /* libsas case */
  4432. if (ap->flags & ATA_FLAG_SAS_HOST) {
  4433. tag = ata_sas_allocate_tag(ap);
  4434. if (tag < 0)
  4435. return NULL;
  4436. }
  4437. qc = __ata_qc_from_tag(ap, tag);
  4438. qc->tag = qc->hw_tag = tag;
  4439. qc->scsicmd = NULL;
  4440. qc->ap = ap;
  4441. qc->dev = dev;
  4442. ata_qc_reinit(qc);
  4443. return qc;
  4444. }
  4445. /**
  4446. * ata_qc_free - free unused ata_queued_cmd
  4447. * @qc: Command to complete
  4448. *
  4449. * Designed to free unused ata_queued_cmd object
  4450. * in case something prevents using it.
  4451. *
  4452. * LOCKING:
  4453. * spin_lock_irqsave(host lock)
  4454. */
  4455. void ata_qc_free(struct ata_queued_cmd *qc)
  4456. {
  4457. struct ata_port *ap;
  4458. unsigned int tag;
  4459. WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  4460. ap = qc->ap;
  4461. qc->flags = 0;
  4462. tag = qc->tag;
  4463. if (ata_tag_valid(tag)) {
  4464. qc->tag = ATA_TAG_POISON;
  4465. if (ap->flags & ATA_FLAG_SAS_HOST)
  4466. ata_sas_free_tag(tag, ap);
  4467. }
  4468. }
  4469. void __ata_qc_complete(struct ata_queued_cmd *qc)
  4470. {
  4471. struct ata_port *ap;
  4472. struct ata_link *link;
  4473. WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  4474. WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
  4475. ap = qc->ap;
  4476. link = qc->dev->link;
  4477. if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
  4478. ata_sg_clean(qc);
  4479. /* command should be marked inactive atomically with qc completion */
  4480. if (ata_is_ncq(qc->tf.protocol)) {
  4481. link->sactive &= ~(1 << qc->hw_tag);
  4482. if (!link->sactive)
  4483. ap->nr_active_links--;
  4484. } else {
  4485. link->active_tag = ATA_TAG_POISON;
  4486. ap->nr_active_links--;
  4487. }
  4488. /* clear exclusive status */
  4489. if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
  4490. ap->excl_link == link))
  4491. ap->excl_link = NULL;
  4492. /* atapi: mark qc as inactive to prevent the interrupt handler
  4493. * from completing the command twice later, before the error handler
  4494. * is called. (when rc != 0 and atapi request sense is needed)
  4495. */
  4496. qc->flags &= ~ATA_QCFLAG_ACTIVE;
  4497. ap->qc_active &= ~(1ULL << qc->tag);
  4498. /* call completion callback */
  4499. qc->complete_fn(qc);
  4500. }
  4501. static void fill_result_tf(struct ata_queued_cmd *qc)
  4502. {
  4503. struct ata_port *ap = qc->ap;
  4504. qc->result_tf.flags = qc->tf.flags;
  4505. ap->ops->qc_fill_rtf(qc);
  4506. }
  4507. static void ata_verify_xfer(struct ata_queued_cmd *qc)
  4508. {
  4509. struct ata_device *dev = qc->dev;
  4510. if (!ata_is_data(qc->tf.protocol))
  4511. return;
  4512. if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
  4513. return;
  4514. dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
  4515. }
  4516. /**
  4517. * ata_qc_complete - Complete an active ATA command
  4518. * @qc: Command to complete
  4519. *
  4520. * Indicate to the mid and upper layers that an ATA command has
  4521. * completed, with either an ok or not-ok status.
  4522. *
  4523. * Refrain from calling this function multiple times when
  4524. * successfully completing multiple NCQ commands.
  4525. * ata_qc_complete_multiple() should be used instead, which will
  4526. * properly update IRQ expect state.
  4527. *
  4528. * LOCKING:
  4529. * spin_lock_irqsave(host lock)
  4530. */
  4531. void ata_qc_complete(struct ata_queued_cmd *qc)
  4532. {
  4533. struct ata_port *ap = qc->ap;
  4534. /* Trigger the LED (if available) */
  4535. ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
  4536. /* XXX: New EH and old EH use different mechanisms to
  4537. * synchronize EH with regular execution path.
  4538. *
  4539. * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
  4540. * Normal execution path is responsible for not accessing a
  4541. * failed qc. libata core enforces the rule by returning NULL
  4542. * from ata_qc_from_tag() for failed qcs.
  4543. *
  4544. * Old EH depends on ata_qc_complete() nullifying completion
  4545. * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
  4546. * not synchronize with interrupt handler. Only PIO task is
  4547. * taken care of.
  4548. */
  4549. if (ap->ops->error_handler) {
  4550. struct ata_device *dev = qc->dev;
  4551. struct ata_eh_info *ehi = &dev->link->eh_info;
  4552. if (unlikely(qc->err_mask))
  4553. qc->flags |= ATA_QCFLAG_FAILED;
  4554. /*
  4555. * Finish internal commands without any further processing
  4556. * and always with the result TF filled.
  4557. */
  4558. if (unlikely(ata_tag_internal(qc->tag))) {
  4559. fill_result_tf(qc);
  4560. trace_ata_qc_complete_internal(qc);
  4561. __ata_qc_complete(qc);
  4562. return;
  4563. }
  4564. /*
  4565. * Non-internal qc has failed. Fill the result TF and
  4566. * summon EH.
  4567. */
  4568. if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
  4569. fill_result_tf(qc);
  4570. trace_ata_qc_complete_failed(qc);
  4571. ata_qc_schedule_eh(qc);
  4572. return;
  4573. }
  4574. WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
  4575. /* read result TF if requested */
  4576. if (qc->flags & ATA_QCFLAG_RESULT_TF)
  4577. fill_result_tf(qc);
  4578. trace_ata_qc_complete_done(qc);
  4579. /* Some commands need post-processing after successful
  4580. * completion.
  4581. */
  4582. switch (qc->tf.command) {
  4583. case ATA_CMD_SET_FEATURES:
  4584. if (qc->tf.feature != SETFEATURES_WC_ON &&
  4585. qc->tf.feature != SETFEATURES_WC_OFF &&
  4586. qc->tf.feature != SETFEATURES_RA_ON &&
  4587. qc->tf.feature != SETFEATURES_RA_OFF)
  4588. break;
  4589. /* fall through */
  4590. case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
  4591. case ATA_CMD_SET_MULTI: /* multi_count changed */
  4592. /* revalidate device */
  4593. ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
  4594. ata_port_schedule_eh(ap);
  4595. break;
  4596. case ATA_CMD_SLEEP:
  4597. dev->flags |= ATA_DFLAG_SLEEPING;
  4598. break;
  4599. }
  4600. if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
  4601. ata_verify_xfer(qc);
  4602. __ata_qc_complete(qc);
  4603. } else {
  4604. if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
  4605. return;
  4606. /* read result TF if failed or requested */
  4607. if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
  4608. fill_result_tf(qc);
  4609. __ata_qc_complete(qc);
  4610. }
  4611. }
  4612. /**
  4613. * ata_qc_complete_multiple - Complete multiple qcs successfully
  4614. * @ap: port in question
  4615. * @qc_active: new qc_active mask
  4616. *
  4617. * Complete in-flight commands. This functions is meant to be
  4618. * called from low-level driver's interrupt routine to complete
  4619. * requests normally. ap->qc_active and @qc_active is compared
  4620. * and commands are completed accordingly.
  4621. *
  4622. * Always use this function when completing multiple NCQ commands
  4623. * from IRQ handlers instead of calling ata_qc_complete()
  4624. * multiple times to keep IRQ expect status properly in sync.
  4625. *
  4626. * LOCKING:
  4627. * spin_lock_irqsave(host lock)
  4628. *
  4629. * RETURNS:
  4630. * Number of completed commands on success, -errno otherwise.
  4631. */
  4632. int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active)
  4633. {
  4634. u64 done_mask, ap_qc_active = ap->qc_active;
  4635. int nr_done = 0;
  4636. /*
  4637. * If the internal tag is set on ap->qc_active, then we care about
  4638. * bit0 on the passed in qc_active mask. Move that bit up to match
  4639. * the internal tag.
  4640. */
  4641. if (ap_qc_active & (1ULL << ATA_TAG_INTERNAL)) {
  4642. qc_active |= (qc_active & 0x01) << ATA_TAG_INTERNAL;
  4643. qc_active ^= qc_active & 0x01;
  4644. }
  4645. done_mask = ap_qc_active ^ qc_active;
  4646. if (unlikely(done_mask & qc_active)) {
  4647. ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n",
  4648. ap->qc_active, qc_active);
  4649. return -EINVAL;
  4650. }
  4651. while (done_mask) {
  4652. struct ata_queued_cmd *qc;
  4653. unsigned int tag = __ffs64(done_mask);
  4654. qc = ata_qc_from_tag(ap, tag);
  4655. if (qc) {
  4656. ata_qc_complete(qc);
  4657. nr_done++;
  4658. }
  4659. done_mask &= ~(1ULL << tag);
  4660. }
  4661. return nr_done;
  4662. }
  4663. /**
  4664. * ata_qc_issue - issue taskfile to device
  4665. * @qc: command to issue to device
  4666. *
  4667. * Prepare an ATA command to submission to device.
  4668. * This includes mapping the data into a DMA-able
  4669. * area, filling in the S/G table, and finally
  4670. * writing the taskfile to hardware, starting the command.
  4671. *
  4672. * LOCKING:
  4673. * spin_lock_irqsave(host lock)
  4674. */
  4675. void ata_qc_issue(struct ata_queued_cmd *qc)
  4676. {
  4677. struct ata_port *ap = qc->ap;
  4678. struct ata_link *link = qc->dev->link;
  4679. u8 prot = qc->tf.protocol;
  4680. /* Make sure only one non-NCQ command is outstanding. The
  4681. * check is skipped for old EH because it reuses active qc to
  4682. * request ATAPI sense.
  4683. */
  4684. WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
  4685. if (ata_is_ncq(prot)) {
  4686. WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag));
  4687. if (!link->sactive)
  4688. ap->nr_active_links++;
  4689. link->sactive |= 1 << qc->hw_tag;
  4690. } else {
  4691. WARN_ON_ONCE(link->sactive);
  4692. ap->nr_active_links++;
  4693. link->active_tag = qc->tag;
  4694. }
  4695. qc->flags |= ATA_QCFLAG_ACTIVE;
  4696. ap->qc_active |= 1ULL << qc->tag;
  4697. /*
  4698. * We guarantee to LLDs that they will have at least one
  4699. * non-zero sg if the command is a data command.
  4700. */
  4701. if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
  4702. goto sys_err;
  4703. if (ata_is_dma(prot) || (ata_is_pio(prot) &&
  4704. (ap->flags & ATA_FLAG_PIO_DMA)))
  4705. if (ata_sg_setup(qc))
  4706. goto sys_err;
  4707. /* if device is sleeping, schedule reset and abort the link */
  4708. if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
  4709. link->eh_info.action |= ATA_EH_RESET;
  4710. ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
  4711. ata_link_abort(link);
  4712. return;
  4713. }
  4714. ap->ops->qc_prep(qc);
  4715. trace_ata_qc_issue(qc);
  4716. qc->err_mask |= ap->ops->qc_issue(qc);
  4717. if (unlikely(qc->err_mask))
  4718. goto err;
  4719. return;
  4720. sys_err:
  4721. qc->err_mask |= AC_ERR_SYSTEM;
  4722. err:
  4723. ata_qc_complete(qc);
  4724. }
  4725. /**
  4726. * sata_scr_valid - test whether SCRs are accessible
  4727. * @link: ATA link to test SCR accessibility for
  4728. *
  4729. * Test whether SCRs are accessible for @link.
  4730. *
  4731. * LOCKING:
  4732. * None.
  4733. *
  4734. * RETURNS:
  4735. * 1 if SCRs are accessible, 0 otherwise.
  4736. */
  4737. int sata_scr_valid(struct ata_link *link)
  4738. {
  4739. struct ata_port *ap = link->ap;
  4740. return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
  4741. }
  4742. /**
  4743. * sata_scr_read - read SCR register of the specified port
  4744. * @link: ATA link to read SCR for
  4745. * @reg: SCR to read
  4746. * @val: Place to store read value
  4747. *
  4748. * Read SCR register @reg of @link into *@val. This function is
  4749. * guaranteed to succeed if @link is ap->link, the cable type of
  4750. * the port is SATA and the port implements ->scr_read.
  4751. *
  4752. * LOCKING:
  4753. * None if @link is ap->link. Kernel thread context otherwise.
  4754. *
  4755. * RETURNS:
  4756. * 0 on success, negative errno on failure.
  4757. */
  4758. int sata_scr_read(struct ata_link *link, int reg, u32 *val)
  4759. {
  4760. if (ata_is_host_link(link)) {
  4761. if (sata_scr_valid(link))
  4762. return link->ap->ops->scr_read(link, reg, val);
  4763. return -EOPNOTSUPP;
  4764. }
  4765. return sata_pmp_scr_read(link, reg, val);
  4766. }
  4767. /**
  4768. * sata_scr_write - write SCR register of the specified port
  4769. * @link: ATA link to write SCR for
  4770. * @reg: SCR to write
  4771. * @val: value to write
  4772. *
  4773. * Write @val to SCR register @reg of @link. This function is
  4774. * guaranteed to succeed if @link is ap->link, the cable type of
  4775. * the port is SATA and the port implements ->scr_read.
  4776. *
  4777. * LOCKING:
  4778. * None if @link is ap->link. Kernel thread context otherwise.
  4779. *
  4780. * RETURNS:
  4781. * 0 on success, negative errno on failure.
  4782. */
  4783. int sata_scr_write(struct ata_link *link, int reg, u32 val)
  4784. {
  4785. if (ata_is_host_link(link)) {
  4786. if (sata_scr_valid(link))
  4787. return link->ap->ops->scr_write(link, reg, val);
  4788. return -EOPNOTSUPP;
  4789. }
  4790. return sata_pmp_scr_write(link, reg, val);
  4791. }
  4792. /**
  4793. * sata_scr_write_flush - write SCR register of the specified port and flush
  4794. * @link: ATA link to write SCR for
  4795. * @reg: SCR to write
  4796. * @val: value to write
  4797. *
  4798. * This function is identical to sata_scr_write() except that this
  4799. * function performs flush after writing to the register.
  4800. *
  4801. * LOCKING:
  4802. * None if @link is ap->link. Kernel thread context otherwise.
  4803. *
  4804. * RETURNS:
  4805. * 0 on success, negative errno on failure.
  4806. */
  4807. int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
  4808. {
  4809. if (ata_is_host_link(link)) {
  4810. int rc;
  4811. if (sata_scr_valid(link)) {
  4812. rc = link->ap->ops->scr_write(link, reg, val);
  4813. if (rc == 0)
  4814. rc = link->ap->ops->scr_read(link, reg, &val);
  4815. return rc;
  4816. }
  4817. return -EOPNOTSUPP;
  4818. }
  4819. return sata_pmp_scr_write(link, reg, val);
  4820. }
  4821. /**
  4822. * ata_phys_link_online - test whether the given link is online
  4823. * @link: ATA link to test
  4824. *
  4825. * Test whether @link is online. Note that this function returns
  4826. * 0 if online status of @link cannot be obtained, so
  4827. * ata_link_online(link) != !ata_link_offline(link).
  4828. *
  4829. * LOCKING:
  4830. * None.
  4831. *
  4832. * RETURNS:
  4833. * True if the port online status is available and online.
  4834. */
  4835. bool ata_phys_link_online(struct ata_link *link)
  4836. {
  4837. u32 sstatus;
  4838. if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
  4839. ata_sstatus_online(sstatus))
  4840. return true;
  4841. return false;
  4842. }
  4843. /**
  4844. * ata_phys_link_offline - test whether the given link is offline
  4845. * @link: ATA link to test
  4846. *
  4847. * Test whether @link is offline. Note that this function
  4848. * returns 0 if offline status of @link cannot be obtained, so
  4849. * ata_link_online(link) != !ata_link_offline(link).
  4850. *
  4851. * LOCKING:
  4852. * None.
  4853. *
  4854. * RETURNS:
  4855. * True if the port offline status is available and offline.
  4856. */
  4857. bool ata_phys_link_offline(struct ata_link *link)
  4858. {
  4859. u32 sstatus;
  4860. if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
  4861. !ata_sstatus_online(sstatus))
  4862. return true;
  4863. return false;
  4864. }
  4865. /**
  4866. * ata_link_online - test whether the given link is online
  4867. * @link: ATA link to test
  4868. *
  4869. * Test whether @link is online. This is identical to
  4870. * ata_phys_link_online() when there's no slave link. When
  4871. * there's a slave link, this function should only be called on
  4872. * the master link and will return true if any of M/S links is
  4873. * online.
  4874. *
  4875. * LOCKING:
  4876. * None.
  4877. *
  4878. * RETURNS:
  4879. * True if the port online status is available and online.
  4880. */
  4881. bool ata_link_online(struct ata_link *link)
  4882. {
  4883. struct ata_link *slave = link->ap->slave_link;
  4884. WARN_ON(link == slave); /* shouldn't be called on slave link */
  4885. return ata_phys_link_online(link) ||
  4886. (slave && ata_phys_link_online(slave));
  4887. }
  4888. /**
  4889. * ata_link_offline - test whether the given link is offline
  4890. * @link: ATA link to test
  4891. *
  4892. * Test whether @link is offline. This is identical to
  4893. * ata_phys_link_offline() when there's no slave link. When
  4894. * there's a slave link, this function should only be called on
  4895. * the master link and will return true if both M/S links are
  4896. * offline.
  4897. *
  4898. * LOCKING:
  4899. * None.
  4900. *
  4901. * RETURNS:
  4902. * True if the port offline status is available and offline.
  4903. */
  4904. bool ata_link_offline(struct ata_link *link)
  4905. {
  4906. struct ata_link *slave = link->ap->slave_link;
  4907. WARN_ON(link == slave); /* shouldn't be called on slave link */
  4908. return ata_phys_link_offline(link) &&
  4909. (!slave || ata_phys_link_offline(slave));
  4910. }
  4911. #ifdef CONFIG_PM
  4912. static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
  4913. unsigned int action, unsigned int ehi_flags,
  4914. bool async)
  4915. {
  4916. struct ata_link *link;
  4917. unsigned long flags;
  4918. /* Previous resume operation might still be in
  4919. * progress. Wait for PM_PENDING to clear.
  4920. */
  4921. if (ap->pflags & ATA_PFLAG_PM_PENDING) {
  4922. ata_port_wait_eh(ap);
  4923. WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
  4924. }
  4925. /* request PM ops to EH */
  4926. spin_lock_irqsave(ap->lock, flags);
  4927. ap->pm_mesg = mesg;
  4928. ap->pflags |= ATA_PFLAG_PM_PENDING;
  4929. ata_for_each_link(link, ap, HOST_FIRST) {
  4930. link->eh_info.action |= action;
  4931. link->eh_info.flags |= ehi_flags;
  4932. }
  4933. ata_port_schedule_eh(ap);
  4934. spin_unlock_irqrestore(ap->lock, flags);
  4935. if (!async) {
  4936. ata_port_wait_eh(ap);
  4937. WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
  4938. }
  4939. }
  4940. /*
  4941. * On some hardware, device fails to respond after spun down for suspend. As
  4942. * the device won't be used before being resumed, we don't need to touch the
  4943. * device. Ask EH to skip the usual stuff and proceed directly to suspend.
  4944. *
  4945. * http://thread.gmane.org/gmane.linux.ide/46764
  4946. */
  4947. static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
  4948. | ATA_EHI_NO_AUTOPSY
  4949. | ATA_EHI_NO_RECOVERY;
  4950. static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
  4951. {
  4952. ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
  4953. }
  4954. static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
  4955. {
  4956. ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
  4957. }
  4958. static int ata_port_pm_suspend(struct device *dev)
  4959. {
  4960. struct ata_port *ap = to_ata_port(dev);
  4961. if (pm_runtime_suspended(dev))
  4962. return 0;
  4963. ata_port_suspend(ap, PMSG_SUSPEND);
  4964. return 0;
  4965. }
  4966. static int ata_port_pm_freeze(struct device *dev)
  4967. {
  4968. struct ata_port *ap = to_ata_port(dev);
  4969. if (pm_runtime_suspended(dev))
  4970. return 0;
  4971. ata_port_suspend(ap, PMSG_FREEZE);
  4972. return 0;
  4973. }
  4974. static int ata_port_pm_poweroff(struct device *dev)
  4975. {
  4976. ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
  4977. return 0;
  4978. }
  4979. static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
  4980. | ATA_EHI_QUIET;
  4981. static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
  4982. {
  4983. ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
  4984. }
  4985. static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
  4986. {
  4987. ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
  4988. }
  4989. static int ata_port_pm_resume(struct device *dev)
  4990. {
  4991. ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
  4992. pm_runtime_disable(dev);
  4993. pm_runtime_set_active(dev);
  4994. pm_runtime_enable(dev);
  4995. return 0;
  4996. }
  4997. /*
  4998. * For ODDs, the upper layer will poll for media change every few seconds,
  4999. * which will make it enter and leave suspend state every few seconds. And
  5000. * as each suspend will cause a hard/soft reset, the gain of runtime suspend
  5001. * is very little and the ODD may malfunction after constantly being reset.
  5002. * So the idle callback here will not proceed to suspend if a non-ZPODD capable
  5003. * ODD is attached to the port.
  5004. */
  5005. static int ata_port_runtime_idle(struct device *dev)
  5006. {
  5007. struct ata_port *ap = to_ata_port(dev);
  5008. struct ata_link *link;
  5009. struct ata_device *adev;
  5010. ata_for_each_link(link, ap, HOST_FIRST) {
  5011. ata_for_each_dev(adev, link, ENABLED)
  5012. if (adev->class == ATA_DEV_ATAPI &&
  5013. !zpodd_dev_enabled(adev))
  5014. return -EBUSY;
  5015. }
  5016. return 0;
  5017. }
  5018. static int ata_port_runtime_suspend(struct device *dev)
  5019. {
  5020. ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
  5021. return 0;
  5022. }
  5023. static int ata_port_runtime_resume(struct device *dev)
  5024. {
  5025. ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
  5026. return 0;
  5027. }
  5028. static const struct dev_pm_ops ata_port_pm_ops = {
  5029. .suspend = ata_port_pm_suspend,
  5030. .resume = ata_port_pm_resume,
  5031. .freeze = ata_port_pm_freeze,
  5032. .thaw = ata_port_pm_resume,
  5033. .poweroff = ata_port_pm_poweroff,
  5034. .restore = ata_port_pm_resume,
  5035. .runtime_suspend = ata_port_runtime_suspend,
  5036. .runtime_resume = ata_port_runtime_resume,
  5037. .runtime_idle = ata_port_runtime_idle,
  5038. };
  5039. /* sas ports don't participate in pm runtime management of ata_ports,
  5040. * and need to resume ata devices at the domain level, not the per-port
  5041. * level. sas suspend/resume is async to allow parallel port recovery
  5042. * since sas has multiple ata_port instances per Scsi_Host.
  5043. */
  5044. void ata_sas_port_suspend(struct ata_port *ap)
  5045. {
  5046. ata_port_suspend_async(ap, PMSG_SUSPEND);
  5047. }
  5048. EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
  5049. void ata_sas_port_resume(struct ata_port *ap)
  5050. {
  5051. ata_port_resume_async(ap, PMSG_RESUME);
  5052. }
  5053. EXPORT_SYMBOL_GPL(ata_sas_port_resume);
  5054. /**
  5055. * ata_host_suspend - suspend host
  5056. * @host: host to suspend
  5057. * @mesg: PM message
  5058. *
  5059. * Suspend @host. Actual operation is performed by port suspend.
  5060. */
  5061. int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
  5062. {
  5063. host->dev->power.power_state = mesg;
  5064. return 0;
  5065. }
  5066. /**
  5067. * ata_host_resume - resume host
  5068. * @host: host to resume
  5069. *
  5070. * Resume @host. Actual operation is performed by port resume.
  5071. */
  5072. void ata_host_resume(struct ata_host *host)
  5073. {
  5074. host->dev->power.power_state = PMSG_ON;
  5075. }
  5076. #endif
  5077. const struct device_type ata_port_type = {
  5078. .name = "ata_port",
  5079. #ifdef CONFIG_PM
  5080. .pm = &ata_port_pm_ops,
  5081. #endif
  5082. };
  5083. /**
  5084. * ata_dev_init - Initialize an ata_device structure
  5085. * @dev: Device structure to initialize
  5086. *
  5087. * Initialize @dev in preparation for probing.
  5088. *
  5089. * LOCKING:
  5090. * Inherited from caller.
  5091. */
  5092. void ata_dev_init(struct ata_device *dev)
  5093. {
  5094. struct ata_link *link = ata_dev_phys_link(dev);
  5095. struct ata_port *ap = link->ap;
  5096. unsigned long flags;
  5097. /* SATA spd limit is bound to the attached device, reset together */
  5098. link->sata_spd_limit = link->hw_sata_spd_limit;
  5099. link->sata_spd = 0;
  5100. /* High bits of dev->flags are used to record warm plug
  5101. * requests which occur asynchronously. Synchronize using
  5102. * host lock.
  5103. */
  5104. spin_lock_irqsave(ap->lock, flags);
  5105. dev->flags &= ~ATA_DFLAG_INIT_MASK;
  5106. dev->horkage = 0;
  5107. spin_unlock_irqrestore(ap->lock, flags);
  5108. memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
  5109. ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
  5110. dev->pio_mask = UINT_MAX;
  5111. dev->mwdma_mask = UINT_MAX;
  5112. dev->udma_mask = UINT_MAX;
  5113. }
  5114. /**
  5115. * ata_link_init - Initialize an ata_link structure
  5116. * @ap: ATA port link is attached to
  5117. * @link: Link structure to initialize
  5118. * @pmp: Port multiplier port number
  5119. *
  5120. * Initialize @link.
  5121. *
  5122. * LOCKING:
  5123. * Kernel thread context (may sleep)
  5124. */
  5125. void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
  5126. {
  5127. int i;
  5128. /* clear everything except for devices */
  5129. memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
  5130. ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
  5131. link->ap = ap;
  5132. link->pmp = pmp;
  5133. link->active_tag = ATA_TAG_POISON;
  5134. link->hw_sata_spd_limit = UINT_MAX;
  5135. /* can't use iterator, ap isn't initialized yet */
  5136. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  5137. struct ata_device *dev = &link->device[i];
  5138. dev->link = link;
  5139. dev->devno = dev - link->device;
  5140. #ifdef CONFIG_ATA_ACPI
  5141. dev->gtf_filter = ata_acpi_gtf_filter;
  5142. #endif
  5143. ata_dev_init(dev);
  5144. }
  5145. }
  5146. /**
  5147. * sata_link_init_spd - Initialize link->sata_spd_limit
  5148. * @link: Link to configure sata_spd_limit for
  5149. *
  5150. * Initialize @link->[hw_]sata_spd_limit to the currently
  5151. * configured value.
  5152. *
  5153. * LOCKING:
  5154. * Kernel thread context (may sleep).
  5155. *
  5156. * RETURNS:
  5157. * 0 on success, -errno on failure.
  5158. */
  5159. int sata_link_init_spd(struct ata_link *link)
  5160. {
  5161. u8 spd;
  5162. int rc;
  5163. rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
  5164. if (rc)
  5165. return rc;
  5166. spd = (link->saved_scontrol >> 4) & 0xf;
  5167. if (spd)
  5168. link->hw_sata_spd_limit &= (1 << spd) - 1;
  5169. ata_force_link_limits(link);
  5170. link->sata_spd_limit = link->hw_sata_spd_limit;
  5171. return 0;
  5172. }
  5173. /**
  5174. * ata_port_alloc - allocate and initialize basic ATA port resources
  5175. * @host: ATA host this allocated port belongs to
  5176. *
  5177. * Allocate and initialize basic ATA port resources.
  5178. *
  5179. * RETURNS:
  5180. * Allocate ATA port on success, NULL on failure.
  5181. *
  5182. * LOCKING:
  5183. * Inherited from calling layer (may sleep).
  5184. */
  5185. struct ata_port *ata_port_alloc(struct ata_host *host)
  5186. {
  5187. struct ata_port *ap;
  5188. DPRINTK("ENTER\n");
  5189. ap = kzalloc(sizeof(*ap), GFP_KERNEL);
  5190. if (!ap)
  5191. return NULL;
  5192. ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
  5193. ap->lock = &host->lock;
  5194. ap->print_id = -1;
  5195. ap->local_port_no = -1;
  5196. ap->host = host;
  5197. ap->dev = host->dev;
  5198. #if defined(ATA_VERBOSE_DEBUG)
  5199. /* turn on all debugging levels */
  5200. ap->msg_enable = 0x00FF;
  5201. #elif defined(ATA_DEBUG)
  5202. ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
  5203. #else
  5204. ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
  5205. #endif
  5206. mutex_init(&ap->scsi_scan_mutex);
  5207. INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
  5208. INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
  5209. INIT_LIST_HEAD(&ap->eh_done_q);
  5210. init_waitqueue_head(&ap->eh_wait_q);
  5211. init_completion(&ap->park_req_pending);
  5212. timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
  5213. TIMER_DEFERRABLE);
  5214. ap->cbl = ATA_CBL_NONE;
  5215. ata_link_init(ap, &ap->link, 0);
  5216. #ifdef ATA_IRQ_TRAP
  5217. ap->stats.unhandled_irq = 1;
  5218. ap->stats.idle_irq = 1;
  5219. #endif
  5220. ata_sff_port_init(ap);
  5221. return ap;
  5222. }
  5223. static void ata_devres_release(struct device *gendev, void *res)
  5224. {
  5225. struct ata_host *host = dev_get_drvdata(gendev);
  5226. int i;
  5227. for (i = 0; i < host->n_ports; i++) {
  5228. struct ata_port *ap = host->ports[i];
  5229. if (!ap)
  5230. continue;
  5231. if (ap->scsi_host)
  5232. scsi_host_put(ap->scsi_host);
  5233. }
  5234. dev_set_drvdata(gendev, NULL);
  5235. ata_host_put(host);
  5236. }
  5237. static void ata_host_release(struct kref *kref)
  5238. {
  5239. struct ata_host *host = container_of(kref, struct ata_host, kref);
  5240. int i;
  5241. for (i = 0; i < host->n_ports; i++) {
  5242. struct ata_port *ap = host->ports[i];
  5243. kfree(ap->pmp_link);
  5244. kfree(ap->slave_link);
  5245. kfree(ap);
  5246. host->ports[i] = NULL;
  5247. }
  5248. kfree(host);
  5249. }
  5250. void ata_host_get(struct ata_host *host)
  5251. {
  5252. kref_get(&host->kref);
  5253. }
  5254. void ata_host_put(struct ata_host *host)
  5255. {
  5256. kref_put(&host->kref, ata_host_release);
  5257. }
  5258. /**
  5259. * ata_host_alloc - allocate and init basic ATA host resources
  5260. * @dev: generic device this host is associated with
  5261. * @max_ports: maximum number of ATA ports associated with this host
  5262. *
  5263. * Allocate and initialize basic ATA host resources. LLD calls
  5264. * this function to allocate a host, initializes it fully and
  5265. * attaches it using ata_host_register().
  5266. *
  5267. * @max_ports ports are allocated and host->n_ports is
  5268. * initialized to @max_ports. The caller is allowed to decrease
  5269. * host->n_ports before calling ata_host_register(). The unused
  5270. * ports will be automatically freed on registration.
  5271. *
  5272. * RETURNS:
  5273. * Allocate ATA host on success, NULL on failure.
  5274. *
  5275. * LOCKING:
  5276. * Inherited from calling layer (may sleep).
  5277. */
  5278. struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
  5279. {
  5280. struct ata_host *host;
  5281. size_t sz;
  5282. int i;
  5283. void *dr;
  5284. DPRINTK("ENTER\n");
  5285. /* alloc a container for our list of ATA ports (buses) */
  5286. sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
  5287. host = kzalloc(sz, GFP_KERNEL);
  5288. if (!host)
  5289. return NULL;
  5290. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  5291. goto err_free;
  5292. dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
  5293. if (!dr)
  5294. goto err_out;
  5295. devres_add(dev, dr);
  5296. dev_set_drvdata(dev, host);
  5297. spin_lock_init(&host->lock);
  5298. mutex_init(&host->eh_mutex);
  5299. host->dev = dev;
  5300. host->n_ports = max_ports;
  5301. kref_init(&host->kref);
  5302. /* allocate ports bound to this host */
  5303. for (i = 0; i < max_ports; i++) {
  5304. struct ata_port *ap;
  5305. ap = ata_port_alloc(host);
  5306. if (!ap)
  5307. goto err_out;
  5308. ap->port_no = i;
  5309. host->ports[i] = ap;
  5310. }
  5311. devres_remove_group(dev, NULL);
  5312. return host;
  5313. err_out:
  5314. devres_release_group(dev, NULL);
  5315. err_free:
  5316. kfree(host);
  5317. return NULL;
  5318. }
  5319. /**
  5320. * ata_host_alloc_pinfo - alloc host and init with port_info array
  5321. * @dev: generic device this host is associated with
  5322. * @ppi: array of ATA port_info to initialize host with
  5323. * @n_ports: number of ATA ports attached to this host
  5324. *
  5325. * Allocate ATA host and initialize with info from @ppi. If NULL
  5326. * terminated, @ppi may contain fewer entries than @n_ports. The
  5327. * last entry will be used for the remaining ports.
  5328. *
  5329. * RETURNS:
  5330. * Allocate ATA host on success, NULL on failure.
  5331. *
  5332. * LOCKING:
  5333. * Inherited from calling layer (may sleep).
  5334. */
  5335. struct ata_host *ata_host_alloc_pinfo(struct device *dev,
  5336. const struct ata_port_info * const * ppi,
  5337. int n_ports)
  5338. {
  5339. const struct ata_port_info *pi;
  5340. struct ata_host *host;
  5341. int i, j;
  5342. host = ata_host_alloc(dev, n_ports);
  5343. if (!host)
  5344. return NULL;
  5345. for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
  5346. struct ata_port *ap = host->ports[i];
  5347. if (ppi[j])
  5348. pi = ppi[j++];
  5349. ap->pio_mask = pi->pio_mask;
  5350. ap->mwdma_mask = pi->mwdma_mask;
  5351. ap->udma_mask = pi->udma_mask;
  5352. ap->flags |= pi->flags;
  5353. ap->link.flags |= pi->link_flags;
  5354. ap->ops = pi->port_ops;
  5355. if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
  5356. host->ops = pi->port_ops;
  5357. }
  5358. return host;
  5359. }
  5360. /**
  5361. * ata_slave_link_init - initialize slave link
  5362. * @ap: port to initialize slave link for
  5363. *
  5364. * Create and initialize slave link for @ap. This enables slave
  5365. * link handling on the port.
  5366. *
  5367. * In libata, a port contains links and a link contains devices.
  5368. * There is single host link but if a PMP is attached to it,
  5369. * there can be multiple fan-out links. On SATA, there's usually
  5370. * a single device connected to a link but PATA and SATA
  5371. * controllers emulating TF based interface can have two - master
  5372. * and slave.
  5373. *
  5374. * However, there are a few controllers which don't fit into this
  5375. * abstraction too well - SATA controllers which emulate TF
  5376. * interface with both master and slave devices but also have
  5377. * separate SCR register sets for each device. These controllers
  5378. * need separate links for physical link handling
  5379. * (e.g. onlineness, link speed) but should be treated like a
  5380. * traditional M/S controller for everything else (e.g. command
  5381. * issue, softreset).
  5382. *
  5383. * slave_link is libata's way of handling this class of
  5384. * controllers without impacting core layer too much. For
  5385. * anything other than physical link handling, the default host
  5386. * link is used for both master and slave. For physical link
  5387. * handling, separate @ap->slave_link is used. All dirty details
  5388. * are implemented inside libata core layer. From LLD's POV, the
  5389. * only difference is that prereset, hardreset and postreset are
  5390. * called once more for the slave link, so the reset sequence
  5391. * looks like the following.
  5392. *
  5393. * prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
  5394. * softreset(M) -> postreset(M) -> postreset(S)
  5395. *
  5396. * Note that softreset is called only for the master. Softreset
  5397. * resets both M/S by definition, so SRST on master should handle
  5398. * both (the standard method will work just fine).
  5399. *
  5400. * LOCKING:
  5401. * Should be called before host is registered.
  5402. *
  5403. * RETURNS:
  5404. * 0 on success, -errno on failure.
  5405. */
  5406. int ata_slave_link_init(struct ata_port *ap)
  5407. {
  5408. struct ata_link *link;
  5409. WARN_ON(ap->slave_link);
  5410. WARN_ON(ap->flags & ATA_FLAG_PMP);
  5411. link = kzalloc(sizeof(*link), GFP_KERNEL);
  5412. if (!link)
  5413. return -ENOMEM;
  5414. ata_link_init(ap, link, 1);
  5415. ap->slave_link = link;
  5416. return 0;
  5417. }
  5418. static void ata_host_stop(struct device *gendev, void *res)
  5419. {
  5420. struct ata_host *host = dev_get_drvdata(gendev);
  5421. int i;
  5422. WARN_ON(!(host->flags & ATA_HOST_STARTED));
  5423. for (i = 0; i < host->n_ports; i++) {
  5424. struct ata_port *ap = host->ports[i];
  5425. if (ap->ops->port_stop)
  5426. ap->ops->port_stop(ap);
  5427. }
  5428. if (host->ops->host_stop)
  5429. host->ops->host_stop(host);
  5430. }
  5431. /**
  5432. * ata_finalize_port_ops - finalize ata_port_operations
  5433. * @ops: ata_port_operations to finalize
  5434. *
  5435. * An ata_port_operations can inherit from another ops and that
  5436. * ops can again inherit from another. This can go on as many
  5437. * times as necessary as long as there is no loop in the
  5438. * inheritance chain.
  5439. *
  5440. * Ops tables are finalized when the host is started. NULL or
  5441. * unspecified entries are inherited from the closet ancestor
  5442. * which has the method and the entry is populated with it.
  5443. * After finalization, the ops table directly points to all the
  5444. * methods and ->inherits is no longer necessary and cleared.
  5445. *
  5446. * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
  5447. *
  5448. * LOCKING:
  5449. * None.
  5450. */
  5451. static void ata_finalize_port_ops(struct ata_port_operations *ops)
  5452. {
  5453. static DEFINE_SPINLOCK(lock);
  5454. const struct ata_port_operations *cur;
  5455. void **begin = (void **)ops;
  5456. void **end = (void **)&ops->inherits;
  5457. void **pp;
  5458. if (!ops || !ops->inherits)
  5459. return;
  5460. spin_lock(&lock);
  5461. for (cur = ops->inherits; cur; cur = cur->inherits) {
  5462. void **inherit = (void **)cur;
  5463. for (pp = begin; pp < end; pp++, inherit++)
  5464. if (!*pp)
  5465. *pp = *inherit;
  5466. }
  5467. for (pp = begin; pp < end; pp++)
  5468. if (IS_ERR(*pp))
  5469. *pp = NULL;
  5470. ops->inherits = NULL;
  5471. spin_unlock(&lock);
  5472. }
  5473. /**
  5474. * ata_host_start - start and freeze ports of an ATA host
  5475. * @host: ATA host to start ports for
  5476. *
  5477. * Start and then freeze ports of @host. Started status is
  5478. * recorded in host->flags, so this function can be called
  5479. * multiple times. Ports are guaranteed to get started only
  5480. * once. If host->ops isn't initialized yet, its set to the
  5481. * first non-dummy port ops.
  5482. *
  5483. * LOCKING:
  5484. * Inherited from calling layer (may sleep).
  5485. *
  5486. * RETURNS:
  5487. * 0 if all ports are started successfully, -errno otherwise.
  5488. */
  5489. int ata_host_start(struct ata_host *host)
  5490. {
  5491. int have_stop = 0;
  5492. void *start_dr = NULL;
  5493. int i, rc;
  5494. if (host->flags & ATA_HOST_STARTED)
  5495. return 0;
  5496. ata_finalize_port_ops(host->ops);
  5497. for (i = 0; i < host->n_ports; i++) {
  5498. struct ata_port *ap = host->ports[i];
  5499. ata_finalize_port_ops(ap->ops);
  5500. if (!host->ops && !ata_port_is_dummy(ap))
  5501. host->ops = ap->ops;
  5502. if (ap->ops->port_stop)
  5503. have_stop = 1;
  5504. }
  5505. if (host->ops->host_stop)
  5506. have_stop = 1;
  5507. if (have_stop) {
  5508. start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
  5509. if (!start_dr)
  5510. return -ENOMEM;
  5511. }
  5512. for (i = 0; i < host->n_ports; i++) {
  5513. struct ata_port *ap = host->ports[i];
  5514. if (ap->ops->port_start) {
  5515. rc = ap->ops->port_start(ap);
  5516. if (rc) {
  5517. if (rc != -ENODEV)
  5518. dev_err(host->dev,
  5519. "failed to start port %d (errno=%d)\n",
  5520. i, rc);
  5521. goto err_out;
  5522. }
  5523. }
  5524. ata_eh_freeze_port(ap);
  5525. }
  5526. if (start_dr)
  5527. devres_add(host->dev, start_dr);
  5528. host->flags |= ATA_HOST_STARTED;
  5529. return 0;
  5530. err_out:
  5531. while (--i >= 0) {
  5532. struct ata_port *ap = host->ports[i];
  5533. if (ap->ops->port_stop)
  5534. ap->ops->port_stop(ap);
  5535. }
  5536. devres_free(start_dr);
  5537. return rc;
  5538. }
  5539. /**
  5540. * ata_sas_host_init - Initialize a host struct for sas (ipr, libsas)
  5541. * @host: host to initialize
  5542. * @dev: device host is attached to
  5543. * @ops: port_ops
  5544. *
  5545. */
  5546. void ata_host_init(struct ata_host *host, struct device *dev,
  5547. struct ata_port_operations *ops)
  5548. {
  5549. spin_lock_init(&host->lock);
  5550. mutex_init(&host->eh_mutex);
  5551. host->n_tags = ATA_MAX_QUEUE;
  5552. host->dev = dev;
  5553. host->ops = ops;
  5554. kref_init(&host->kref);
  5555. }
  5556. void __ata_port_probe(struct ata_port *ap)
  5557. {
  5558. struct ata_eh_info *ehi = &ap->link.eh_info;
  5559. unsigned long flags;
  5560. /* kick EH for boot probing */
  5561. spin_lock_irqsave(ap->lock, flags);
  5562. ehi->probe_mask |= ATA_ALL_DEVICES;
  5563. ehi->action |= ATA_EH_RESET;
  5564. ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
  5565. ap->pflags &= ~ATA_PFLAG_INITIALIZING;
  5566. ap->pflags |= ATA_PFLAG_LOADING;
  5567. ata_port_schedule_eh(ap);
  5568. spin_unlock_irqrestore(ap->lock, flags);
  5569. }
  5570. int ata_port_probe(struct ata_port *ap)
  5571. {
  5572. int rc = 0;
  5573. if (ap->ops->error_handler) {
  5574. __ata_port_probe(ap);
  5575. ata_port_wait_eh(ap);
  5576. } else {
  5577. DPRINTK("ata%u: bus probe begin\n", ap->print_id);
  5578. rc = ata_bus_probe(ap);
  5579. DPRINTK("ata%u: bus probe end\n", ap->print_id);
  5580. }
  5581. return rc;
  5582. }
  5583. static void async_port_probe(void *data, async_cookie_t cookie)
  5584. {
  5585. struct ata_port *ap = data;
  5586. /*
  5587. * If we're not allowed to scan this host in parallel,
  5588. * we need to wait until all previous scans have completed
  5589. * before going further.
  5590. * Jeff Garzik says this is only within a controller, so we
  5591. * don't need to wait for port 0, only for later ports.
  5592. */
  5593. if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
  5594. async_synchronize_cookie(cookie);
  5595. (void)ata_port_probe(ap);
  5596. /* in order to keep device order, we need to synchronize at this point */
  5597. async_synchronize_cookie(cookie);
  5598. ata_scsi_scan_host(ap, 1);
  5599. }
  5600. /**
  5601. * ata_host_register - register initialized ATA host
  5602. * @host: ATA host to register
  5603. * @sht: template for SCSI host
  5604. *
  5605. * Register initialized ATA host. @host is allocated using
  5606. * ata_host_alloc() and fully initialized by LLD. This function
  5607. * starts ports, registers @host with ATA and SCSI layers and
  5608. * probe registered devices.
  5609. *
  5610. * LOCKING:
  5611. * Inherited from calling layer (may sleep).
  5612. *
  5613. * RETURNS:
  5614. * 0 on success, -errno otherwise.
  5615. */
  5616. int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
  5617. {
  5618. int i, rc;
  5619. host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE);
  5620. /* host must have been started */
  5621. if (!(host->flags & ATA_HOST_STARTED)) {
  5622. dev_err(host->dev, "BUG: trying to register unstarted host\n");
  5623. WARN_ON(1);
  5624. return -EINVAL;
  5625. }
  5626. /* Blow away unused ports. This happens when LLD can't
  5627. * determine the exact number of ports to allocate at
  5628. * allocation time.
  5629. */
  5630. for (i = host->n_ports; host->ports[i]; i++)
  5631. kfree(host->ports[i]);
  5632. /* give ports names and add SCSI hosts */
  5633. for (i = 0; i < host->n_ports; i++) {
  5634. host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
  5635. host->ports[i]->local_port_no = i + 1;
  5636. }
  5637. /* Create associated sysfs transport objects */
  5638. for (i = 0; i < host->n_ports; i++) {
  5639. rc = ata_tport_add(host->dev,host->ports[i]);
  5640. if (rc) {
  5641. goto err_tadd;
  5642. }
  5643. }
  5644. rc = ata_scsi_add_hosts(host, sht);
  5645. if (rc)
  5646. goto err_tadd;
  5647. /* set cable, sata_spd_limit and report */
  5648. for (i = 0; i < host->n_ports; i++) {
  5649. struct ata_port *ap = host->ports[i];
  5650. unsigned long xfer_mask;
  5651. /* set SATA cable type if still unset */
  5652. if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
  5653. ap->cbl = ATA_CBL_SATA;
  5654. /* init sata_spd_limit to the current value */
  5655. sata_link_init_spd(&ap->link);
  5656. if (ap->slave_link)
  5657. sata_link_init_spd(ap->slave_link);
  5658. /* print per-port info to dmesg */
  5659. xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
  5660. ap->udma_mask);
  5661. if (!ata_port_is_dummy(ap)) {
  5662. ata_port_info(ap, "%cATA max %s %s\n",
  5663. (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
  5664. ata_mode_string(xfer_mask),
  5665. ap->link.eh_info.desc);
  5666. ata_ehi_clear_desc(&ap->link.eh_info);
  5667. } else
  5668. ata_port_info(ap, "DUMMY\n");
  5669. }
  5670. /* perform each probe asynchronously */
  5671. for (i = 0; i < host->n_ports; i++) {
  5672. struct ata_port *ap = host->ports[i];
  5673. async_schedule(async_port_probe, ap);
  5674. }
  5675. return 0;
  5676. err_tadd:
  5677. while (--i >= 0) {
  5678. ata_tport_delete(host->ports[i]);
  5679. }
  5680. return rc;
  5681. }
  5682. /**
  5683. * ata_host_activate - start host, request IRQ and register it
  5684. * @host: target ATA host
  5685. * @irq: IRQ to request
  5686. * @irq_handler: irq_handler used when requesting IRQ
  5687. * @irq_flags: irq_flags used when requesting IRQ
  5688. * @sht: scsi_host_template to use when registering the host
  5689. *
  5690. * After allocating an ATA host and initializing it, most libata
  5691. * LLDs perform three steps to activate the host - start host,
  5692. * request IRQ and register it. This helper takes necessary
  5693. * arguments and performs the three steps in one go.
  5694. *
  5695. * An invalid IRQ skips the IRQ registration and expects the host to
  5696. * have set polling mode on the port. In this case, @irq_handler
  5697. * should be NULL.
  5698. *
  5699. * LOCKING:
  5700. * Inherited from calling layer (may sleep).
  5701. *
  5702. * RETURNS:
  5703. * 0 on success, -errno otherwise.
  5704. */
  5705. int ata_host_activate(struct ata_host *host, int irq,
  5706. irq_handler_t irq_handler, unsigned long irq_flags,
  5707. struct scsi_host_template *sht)
  5708. {
  5709. int i, rc;
  5710. char *irq_desc;
  5711. rc = ata_host_start(host);
  5712. if (rc)
  5713. return rc;
  5714. /* Special case for polling mode */
  5715. if (!irq) {
  5716. WARN_ON(irq_handler);
  5717. return ata_host_register(host, sht);
  5718. }
  5719. irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
  5720. dev_driver_string(host->dev),
  5721. dev_name(host->dev));
  5722. if (!irq_desc)
  5723. return -ENOMEM;
  5724. rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
  5725. irq_desc, host);
  5726. if (rc)
  5727. return rc;
  5728. for (i = 0; i < host->n_ports; i++)
  5729. ata_port_desc(host->ports[i], "irq %d", irq);
  5730. rc = ata_host_register(host, sht);
  5731. /* if failed, just free the IRQ and leave ports alone */
  5732. if (rc)
  5733. devm_free_irq(host->dev, irq, host);
  5734. return rc;
  5735. }
  5736. /**
  5737. * ata_port_detach - Detach ATA port in preparation of device removal
  5738. * @ap: ATA port to be detached
  5739. *
  5740. * Detach all ATA devices and the associated SCSI devices of @ap;
  5741. * then, remove the associated SCSI host. @ap is guaranteed to
  5742. * be quiescent on return from this function.
  5743. *
  5744. * LOCKING:
  5745. * Kernel thread context (may sleep).
  5746. */
  5747. static void ata_port_detach(struct ata_port *ap)
  5748. {
  5749. unsigned long flags;
  5750. struct ata_link *link;
  5751. struct ata_device *dev;
  5752. if (!ap->ops->error_handler)
  5753. goto skip_eh;
  5754. /* tell EH we're leaving & flush EH */
  5755. spin_lock_irqsave(ap->lock, flags);
  5756. ap->pflags |= ATA_PFLAG_UNLOADING;
  5757. ata_port_schedule_eh(ap);
  5758. spin_unlock_irqrestore(ap->lock, flags);
  5759. /* wait till EH commits suicide */
  5760. ata_port_wait_eh(ap);
  5761. /* it better be dead now */
  5762. WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
  5763. cancel_delayed_work_sync(&ap->hotplug_task);
  5764. skip_eh:
  5765. /* clean up zpodd on port removal */
  5766. ata_for_each_link(link, ap, HOST_FIRST) {
  5767. ata_for_each_dev(dev, link, ALL) {
  5768. if (zpodd_dev_enabled(dev))
  5769. zpodd_exit(dev);
  5770. }
  5771. }
  5772. if (ap->pmp_link) {
  5773. int i;
  5774. for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
  5775. ata_tlink_delete(&ap->pmp_link[i]);
  5776. }
  5777. /* remove the associated SCSI host */
  5778. scsi_remove_host(ap->scsi_host);
  5779. ata_tport_delete(ap);
  5780. }
  5781. /**
  5782. * ata_host_detach - Detach all ports of an ATA host
  5783. * @host: Host to detach
  5784. *
  5785. * Detach all ports of @host.
  5786. *
  5787. * LOCKING:
  5788. * Kernel thread context (may sleep).
  5789. */
  5790. void ata_host_detach(struct ata_host *host)
  5791. {
  5792. int i;
  5793. for (i = 0; i < host->n_ports; i++)
  5794. ata_port_detach(host->ports[i]);
  5795. /* the host is dead now, dissociate ACPI */
  5796. ata_acpi_dissociate(host);
  5797. }
  5798. #ifdef CONFIG_PCI
  5799. /**
  5800. * ata_pci_remove_one - PCI layer callback for device removal
  5801. * @pdev: PCI device that was removed
  5802. *
  5803. * PCI layer indicates to libata via this hook that hot-unplug or
  5804. * module unload event has occurred. Detach all ports. Resource
  5805. * release is handled via devres.
  5806. *
  5807. * LOCKING:
  5808. * Inherited from PCI layer (may sleep).
  5809. */
  5810. void ata_pci_remove_one(struct pci_dev *pdev)
  5811. {
  5812. struct ata_host *host = pci_get_drvdata(pdev);
  5813. ata_host_detach(host);
  5814. }
  5815. /* move to PCI subsystem */
  5816. int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
  5817. {
  5818. unsigned long tmp = 0;
  5819. switch (bits->width) {
  5820. case 1: {
  5821. u8 tmp8 = 0;
  5822. pci_read_config_byte(pdev, bits->reg, &tmp8);
  5823. tmp = tmp8;
  5824. break;
  5825. }
  5826. case 2: {
  5827. u16 tmp16 = 0;
  5828. pci_read_config_word(pdev, bits->reg, &tmp16);
  5829. tmp = tmp16;
  5830. break;
  5831. }
  5832. case 4: {
  5833. u32 tmp32 = 0;
  5834. pci_read_config_dword(pdev, bits->reg, &tmp32);
  5835. tmp = tmp32;
  5836. break;
  5837. }
  5838. default:
  5839. return -EINVAL;
  5840. }
  5841. tmp &= bits->mask;
  5842. return (tmp == bits->val) ? 1 : 0;
  5843. }
  5844. #ifdef CONFIG_PM
  5845. void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
  5846. {
  5847. pci_save_state(pdev);
  5848. pci_disable_device(pdev);
  5849. if (mesg.event & PM_EVENT_SLEEP)
  5850. pci_set_power_state(pdev, PCI_D3hot);
  5851. }
  5852. int ata_pci_device_do_resume(struct pci_dev *pdev)
  5853. {
  5854. int rc;
  5855. pci_set_power_state(pdev, PCI_D0);
  5856. pci_restore_state(pdev);
  5857. rc = pcim_enable_device(pdev);
  5858. if (rc) {
  5859. dev_err(&pdev->dev,
  5860. "failed to enable device after resume (%d)\n", rc);
  5861. return rc;
  5862. }
  5863. pci_set_master(pdev);
  5864. return 0;
  5865. }
  5866. int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
  5867. {
  5868. struct ata_host *host = pci_get_drvdata(pdev);
  5869. int rc = 0;
  5870. rc = ata_host_suspend(host, mesg);
  5871. if (rc)
  5872. return rc;
  5873. ata_pci_device_do_suspend(pdev, mesg);
  5874. return 0;
  5875. }
  5876. int ata_pci_device_resume(struct pci_dev *pdev)
  5877. {
  5878. struct ata_host *host = pci_get_drvdata(pdev);
  5879. int rc;
  5880. rc = ata_pci_device_do_resume(pdev);
  5881. if (rc == 0)
  5882. ata_host_resume(host);
  5883. return rc;
  5884. }
  5885. #endif /* CONFIG_PM */
  5886. #endif /* CONFIG_PCI */
  5887. /**
  5888. * ata_platform_remove_one - Platform layer callback for device removal
  5889. * @pdev: Platform device that was removed
  5890. *
  5891. * Platform layer indicates to libata via this hook that hot-unplug or
  5892. * module unload event has occurred. Detach all ports. Resource
  5893. * release is handled via devres.
  5894. *
  5895. * LOCKING:
  5896. * Inherited from platform layer (may sleep).
  5897. */
  5898. int ata_platform_remove_one(struct platform_device *pdev)
  5899. {
  5900. struct ata_host *host = platform_get_drvdata(pdev);
  5901. ata_host_detach(host);
  5902. return 0;
  5903. }
  5904. static int __init ata_parse_force_one(char **cur,
  5905. struct ata_force_ent *force_ent,
  5906. const char **reason)
  5907. {
  5908. static const struct ata_force_param force_tbl[] __initconst = {
  5909. { "40c", .cbl = ATA_CBL_PATA40 },
  5910. { "80c", .cbl = ATA_CBL_PATA80 },
  5911. { "short40c", .cbl = ATA_CBL_PATA40_SHORT },
  5912. { "unk", .cbl = ATA_CBL_PATA_UNK },
  5913. { "ign", .cbl = ATA_CBL_PATA_IGN },
  5914. { "sata", .cbl = ATA_CBL_SATA },
  5915. { "1.5Gbps", .spd_limit = 1 },
  5916. { "3.0Gbps", .spd_limit = 2 },
  5917. { "noncq", .horkage_on = ATA_HORKAGE_NONCQ },
  5918. { "ncq", .horkage_off = ATA_HORKAGE_NONCQ },
  5919. { "noncqtrim", .horkage_on = ATA_HORKAGE_NO_NCQ_TRIM },
  5920. { "ncqtrim", .horkage_off = ATA_HORKAGE_NO_NCQ_TRIM },
  5921. { "dump_id", .horkage_on = ATA_HORKAGE_DUMP_ID },
  5922. { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) },
  5923. { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) },
  5924. { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) },
  5925. { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) },
  5926. { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) },
  5927. { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) },
  5928. { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) },
  5929. { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) },
  5930. { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) },
  5931. { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) },
  5932. { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) },
  5933. { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) },
  5934. { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
  5935. { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
  5936. { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
  5937. { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
  5938. { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
  5939. { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
  5940. { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
  5941. { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
  5942. { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
  5943. { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
  5944. { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
  5945. { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
  5946. { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
  5947. { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
  5948. { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
  5949. { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
  5950. { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
  5951. { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
  5952. { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
  5953. { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
  5954. { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
  5955. { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) },
  5956. { "nohrst", .lflags = ATA_LFLAG_NO_HRST },
  5957. { "nosrst", .lflags = ATA_LFLAG_NO_SRST },
  5958. { "norst", .lflags = ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
  5959. { "rstonce", .lflags = ATA_LFLAG_RST_ONCE },
  5960. { "atapi_dmadir", .horkage_on = ATA_HORKAGE_ATAPI_DMADIR },
  5961. { "disable", .horkage_on = ATA_HORKAGE_DISABLE },
  5962. };
  5963. char *start = *cur, *p = *cur;
  5964. char *id, *val, *endp;
  5965. const struct ata_force_param *match_fp = NULL;
  5966. int nr_matches = 0, i;
  5967. /* find where this param ends and update *cur */
  5968. while (*p != '\0' && *p != ',')
  5969. p++;
  5970. if (*p == '\0')
  5971. *cur = p;
  5972. else
  5973. *cur = p + 1;
  5974. *p = '\0';
  5975. /* parse */
  5976. p = strchr(start, ':');
  5977. if (!p) {
  5978. val = strstrip(start);
  5979. goto parse_val;
  5980. }
  5981. *p = '\0';
  5982. id = strstrip(start);
  5983. val = strstrip(p + 1);
  5984. /* parse id */
  5985. p = strchr(id, '.');
  5986. if (p) {
  5987. *p++ = '\0';
  5988. force_ent->device = simple_strtoul(p, &endp, 10);
  5989. if (p == endp || *endp != '\0') {
  5990. *reason = "invalid device";
  5991. return -EINVAL;
  5992. }
  5993. }
  5994. force_ent->port = simple_strtoul(id, &endp, 10);
  5995. if (id == endp || *endp != '\0') {
  5996. *reason = "invalid port/link";
  5997. return -EINVAL;
  5998. }
  5999. parse_val:
  6000. /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
  6001. for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
  6002. const struct ata_force_param *fp = &force_tbl[i];
  6003. if (strncasecmp(val, fp->name, strlen(val)))
  6004. continue;
  6005. nr_matches++;
  6006. match_fp = fp;
  6007. if (strcasecmp(val, fp->name) == 0) {
  6008. nr_matches = 1;
  6009. break;
  6010. }
  6011. }
  6012. if (!nr_matches) {
  6013. *reason = "unknown value";
  6014. return -EINVAL;
  6015. }
  6016. if (nr_matches > 1) {
  6017. *reason = "ambiguous value";
  6018. return -EINVAL;
  6019. }
  6020. force_ent->param = *match_fp;
  6021. return 0;
  6022. }
  6023. static void __init ata_parse_force_param(void)
  6024. {
  6025. int idx = 0, size = 1;
  6026. int last_port = -1, last_device = -1;
  6027. char *p, *cur, *next;
  6028. /* calculate maximum number of params and allocate force_tbl */
  6029. for (p = ata_force_param_buf; *p; p++)
  6030. if (*p == ',')
  6031. size++;
  6032. ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL);
  6033. if (!ata_force_tbl) {
  6034. printk(KERN_WARNING "ata: failed to extend force table, "
  6035. "libata.force ignored\n");
  6036. return;
  6037. }
  6038. /* parse and populate the table */
  6039. for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
  6040. const char *reason = "";
  6041. struct ata_force_ent te = { .port = -1, .device = -1 };
  6042. next = cur;
  6043. if (ata_parse_force_one(&next, &te, &reason)) {
  6044. printk(KERN_WARNING "ata: failed to parse force "
  6045. "parameter \"%s\" (%s)\n",
  6046. cur, reason);
  6047. continue;
  6048. }
  6049. if (te.port == -1) {
  6050. te.port = last_port;
  6051. te.device = last_device;
  6052. }
  6053. ata_force_tbl[idx++] = te;
  6054. last_port = te.port;
  6055. last_device = te.device;
  6056. }
  6057. ata_force_tbl_size = idx;
  6058. }
  6059. static int __init ata_init(void)
  6060. {
  6061. int rc;
  6062. ata_parse_force_param();
  6063. rc = ata_sff_init();
  6064. if (rc) {
  6065. kfree(ata_force_tbl);
  6066. return rc;
  6067. }
  6068. libata_transport_init();
  6069. ata_scsi_transport_template = ata_attach_transport();
  6070. if (!ata_scsi_transport_template) {
  6071. ata_sff_exit();
  6072. rc = -ENOMEM;
  6073. goto err_out;
  6074. }
  6075. printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
  6076. return 0;
  6077. err_out:
  6078. return rc;
  6079. }
  6080. static void __exit ata_exit(void)
  6081. {
  6082. ata_release_transport(ata_scsi_transport_template);
  6083. libata_transport_exit();
  6084. ata_sff_exit();
  6085. kfree(ata_force_tbl);
  6086. }
  6087. subsys_initcall(ata_init);
  6088. module_exit(ata_exit);
  6089. static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
  6090. int ata_ratelimit(void)
  6091. {
  6092. return __ratelimit(&ratelimit);
  6093. }
  6094. /**
  6095. * ata_msleep - ATA EH owner aware msleep
  6096. * @ap: ATA port to attribute the sleep to
  6097. * @msecs: duration to sleep in milliseconds
  6098. *
  6099. * Sleeps @msecs. If the current task is owner of @ap's EH, the
  6100. * ownership is released before going to sleep and reacquired
  6101. * after the sleep is complete. IOW, other ports sharing the
  6102. * @ap->host will be allowed to own the EH while this task is
  6103. * sleeping.
  6104. *
  6105. * LOCKING:
  6106. * Might sleep.
  6107. */
  6108. void ata_msleep(struct ata_port *ap, unsigned int msecs)
  6109. {
  6110. bool owns_eh = ap && ap->host->eh_owner == current;
  6111. if (owns_eh)
  6112. ata_eh_release(ap);
  6113. if (msecs < 20) {
  6114. unsigned long usecs = msecs * USEC_PER_MSEC;
  6115. usleep_range(usecs, usecs + 50);
  6116. } else {
  6117. msleep(msecs);
  6118. }
  6119. if (owns_eh)
  6120. ata_eh_acquire(ap);
  6121. }
  6122. /**
  6123. * ata_wait_register - wait until register value changes
  6124. * @ap: ATA port to wait register for, can be NULL
  6125. * @reg: IO-mapped register
  6126. * @mask: Mask to apply to read register value
  6127. * @val: Wait condition
  6128. * @interval: polling interval in milliseconds
  6129. * @timeout: timeout in milliseconds
  6130. *
  6131. * Waiting for some bits of register to change is a common
  6132. * operation for ATA controllers. This function reads 32bit LE
  6133. * IO-mapped register @reg and tests for the following condition.
  6134. *
  6135. * (*@reg & mask) != val
  6136. *
  6137. * If the condition is met, it returns; otherwise, the process is
  6138. * repeated after @interval_msec until timeout.
  6139. *
  6140. * LOCKING:
  6141. * Kernel thread context (may sleep)
  6142. *
  6143. * RETURNS:
  6144. * The final register value.
  6145. */
  6146. u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
  6147. unsigned long interval, unsigned long timeout)
  6148. {
  6149. unsigned long deadline;
  6150. u32 tmp;
  6151. tmp = ioread32(reg);
  6152. /* Calculate timeout _after_ the first read to make sure
  6153. * preceding writes reach the controller before starting to
  6154. * eat away the timeout.
  6155. */
  6156. deadline = ata_deadline(jiffies, timeout);
  6157. while ((tmp & mask) == val && time_before(jiffies, deadline)) {
  6158. ata_msleep(ap, interval);
  6159. tmp = ioread32(reg);
  6160. }
  6161. return tmp;
  6162. }
  6163. /**
  6164. * sata_lpm_ignore_phy_events - test if PHY event should be ignored
  6165. * @link: Link receiving the event
  6166. *
  6167. * Test whether the received PHY event has to be ignored or not.
  6168. *
  6169. * LOCKING:
  6170. * None:
  6171. *
  6172. * RETURNS:
  6173. * True if the event has to be ignored.
  6174. */
  6175. bool sata_lpm_ignore_phy_events(struct ata_link *link)
  6176. {
  6177. unsigned long lpm_timeout = link->last_lpm_change +
  6178. msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY);
  6179. /* if LPM is enabled, PHYRDY doesn't mean anything */
  6180. if (link->lpm_policy > ATA_LPM_MAX_POWER)
  6181. return true;
  6182. /* ignore the first PHY event after the LPM policy changed
  6183. * as it is might be spurious
  6184. */
  6185. if ((link->flags & ATA_LFLAG_CHANGED) &&
  6186. time_before(jiffies, lpm_timeout))
  6187. return true;
  6188. return false;
  6189. }
  6190. EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events);
  6191. /*
  6192. * Dummy port_ops
  6193. */
  6194. static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
  6195. {
  6196. return AC_ERR_SYSTEM;
  6197. }
  6198. static void ata_dummy_error_handler(struct ata_port *ap)
  6199. {
  6200. /* truly dummy */
  6201. }
  6202. struct ata_port_operations ata_dummy_port_ops = {
  6203. .qc_prep = ata_noop_qc_prep,
  6204. .qc_issue = ata_dummy_qc_issue,
  6205. .error_handler = ata_dummy_error_handler,
  6206. .sched_eh = ata_std_sched_eh,
  6207. .end_eh = ata_std_end_eh,
  6208. };
  6209. const struct ata_port_info ata_dummy_port_info = {
  6210. .port_ops = &ata_dummy_port_ops,
  6211. };
  6212. /*
  6213. * Utility print functions
  6214. */
  6215. void ata_port_printk(const struct ata_port *ap, const char *level,
  6216. const char *fmt, ...)
  6217. {
  6218. struct va_format vaf;
  6219. va_list args;
  6220. va_start(args, fmt);
  6221. vaf.fmt = fmt;
  6222. vaf.va = &args;
  6223. printk("%sata%u: %pV", level, ap->print_id, &vaf);
  6224. va_end(args);
  6225. }
  6226. EXPORT_SYMBOL(ata_port_printk);
  6227. void ata_link_printk(const struct ata_link *link, const char *level,
  6228. const char *fmt, ...)
  6229. {
  6230. struct va_format vaf;
  6231. va_list args;
  6232. va_start(args, fmt);
  6233. vaf.fmt = fmt;
  6234. vaf.va = &args;
  6235. if (sata_pmp_attached(link->ap) || link->ap->slave_link)
  6236. printk("%sata%u.%02u: %pV",
  6237. level, link->ap->print_id, link->pmp, &vaf);
  6238. else
  6239. printk("%sata%u: %pV",
  6240. level, link->ap->print_id, &vaf);
  6241. va_end(args);
  6242. }
  6243. EXPORT_SYMBOL(ata_link_printk);
  6244. void ata_dev_printk(const struct ata_device *dev, const char *level,
  6245. const char *fmt, ...)
  6246. {
  6247. struct va_format vaf;
  6248. va_list args;
  6249. va_start(args, fmt);
  6250. vaf.fmt = fmt;
  6251. vaf.va = &args;
  6252. printk("%sata%u.%02u: %pV",
  6253. level, dev->link->ap->print_id, dev->link->pmp + dev->devno,
  6254. &vaf);
  6255. va_end(args);
  6256. }
  6257. EXPORT_SYMBOL(ata_dev_printk);
  6258. void ata_print_version(const struct device *dev, const char *version)
  6259. {
  6260. dev_printk(KERN_DEBUG, dev, "version %s\n", version);
  6261. }
  6262. EXPORT_SYMBOL(ata_print_version);
  6263. /*
  6264. * libata is essentially a library of internal helper functions for
  6265. * low-level ATA host controller drivers. As such, the API/ABI is
  6266. * likely to change as new drivers are added and updated.
  6267. * Do not depend on ABI/API stability.
  6268. */
  6269. EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
  6270. EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
  6271. EXPORT_SYMBOL_GPL(sata_deb_timing_long);
  6272. EXPORT_SYMBOL_GPL(ata_base_port_ops);
  6273. EXPORT_SYMBOL_GPL(sata_port_ops);
  6274. EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
  6275. EXPORT_SYMBOL_GPL(ata_dummy_port_info);
  6276. EXPORT_SYMBOL_GPL(ata_link_next);
  6277. EXPORT_SYMBOL_GPL(ata_dev_next);
  6278. EXPORT_SYMBOL_GPL(ata_std_bios_param);
  6279. EXPORT_SYMBOL_GPL(ata_scsi_unlock_native_capacity);
  6280. EXPORT_SYMBOL_GPL(ata_host_init);
  6281. EXPORT_SYMBOL_GPL(ata_host_alloc);
  6282. EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
  6283. EXPORT_SYMBOL_GPL(ata_slave_link_init);
  6284. EXPORT_SYMBOL_GPL(ata_host_start);
  6285. EXPORT_SYMBOL_GPL(ata_host_register);
  6286. EXPORT_SYMBOL_GPL(ata_host_activate);
  6287. EXPORT_SYMBOL_GPL(ata_host_detach);
  6288. EXPORT_SYMBOL_GPL(ata_sg_init);
  6289. EXPORT_SYMBOL_GPL(ata_qc_complete);
  6290. EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
  6291. EXPORT_SYMBOL_GPL(atapi_cmd_type);
  6292. EXPORT_SYMBOL_GPL(ata_tf_to_fis);
  6293. EXPORT_SYMBOL_GPL(ata_tf_from_fis);
  6294. EXPORT_SYMBOL_GPL(ata_pack_xfermask);
  6295. EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
  6296. EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
  6297. EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
  6298. EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
  6299. EXPORT_SYMBOL_GPL(ata_mode_string);
  6300. EXPORT_SYMBOL_GPL(ata_id_xfermask);
  6301. EXPORT_SYMBOL_GPL(ata_do_set_mode);
  6302. EXPORT_SYMBOL_GPL(ata_std_qc_defer);
  6303. EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
  6304. EXPORT_SYMBOL_GPL(ata_dev_disable);
  6305. EXPORT_SYMBOL_GPL(sata_set_spd);
  6306. EXPORT_SYMBOL_GPL(ata_wait_after_reset);
  6307. EXPORT_SYMBOL_GPL(sata_link_debounce);
  6308. EXPORT_SYMBOL_GPL(sata_link_resume);
  6309. EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
  6310. EXPORT_SYMBOL_GPL(ata_std_prereset);
  6311. EXPORT_SYMBOL_GPL(sata_link_hardreset);
  6312. EXPORT_SYMBOL_GPL(sata_std_hardreset);
  6313. EXPORT_SYMBOL_GPL(ata_std_postreset);
  6314. EXPORT_SYMBOL_GPL(ata_dev_classify);
  6315. EXPORT_SYMBOL_GPL(ata_dev_pair);
  6316. EXPORT_SYMBOL_GPL(ata_ratelimit);
  6317. EXPORT_SYMBOL_GPL(ata_msleep);
  6318. EXPORT_SYMBOL_GPL(ata_wait_register);
  6319. EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
  6320. EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
  6321. EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
  6322. EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
  6323. EXPORT_SYMBOL_GPL(__ata_change_queue_depth);
  6324. EXPORT_SYMBOL_GPL(sata_scr_valid);
  6325. EXPORT_SYMBOL_GPL(sata_scr_read);
  6326. EXPORT_SYMBOL_GPL(sata_scr_write);
  6327. EXPORT_SYMBOL_GPL(sata_scr_write_flush);
  6328. EXPORT_SYMBOL_GPL(ata_link_online);
  6329. EXPORT_SYMBOL_GPL(ata_link_offline);
  6330. #ifdef CONFIG_PM
  6331. EXPORT_SYMBOL_GPL(ata_host_suspend);
  6332. EXPORT_SYMBOL_GPL(ata_host_resume);
  6333. #endif /* CONFIG_PM */
  6334. EXPORT_SYMBOL_GPL(ata_id_string);
  6335. EXPORT_SYMBOL_GPL(ata_id_c_string);
  6336. EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
  6337. EXPORT_SYMBOL_GPL(ata_scsi_simulate);
  6338. EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
  6339. EXPORT_SYMBOL_GPL(ata_timing_find_mode);
  6340. EXPORT_SYMBOL_GPL(ata_timing_compute);
  6341. EXPORT_SYMBOL_GPL(ata_timing_merge);
  6342. EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
  6343. #ifdef CONFIG_PCI
  6344. EXPORT_SYMBOL_GPL(pci_test_config_bits);
  6345. EXPORT_SYMBOL_GPL(ata_pci_remove_one);
  6346. #ifdef CONFIG_PM
  6347. EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
  6348. EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
  6349. EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
  6350. EXPORT_SYMBOL_GPL(ata_pci_device_resume);
  6351. #endif /* CONFIG_PM */
  6352. #endif /* CONFIG_PCI */
  6353. EXPORT_SYMBOL_GPL(ata_platform_remove_one);
  6354. EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
  6355. EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
  6356. EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
  6357. EXPORT_SYMBOL_GPL(ata_port_desc);
  6358. #ifdef CONFIG_PCI
  6359. EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
  6360. #endif /* CONFIG_PCI */
  6361. EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
  6362. EXPORT_SYMBOL_GPL(ata_link_abort);
  6363. EXPORT_SYMBOL_GPL(ata_port_abort);
  6364. EXPORT_SYMBOL_GPL(ata_port_freeze);
  6365. EXPORT_SYMBOL_GPL(sata_async_notification);
  6366. EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
  6367. EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
  6368. EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
  6369. EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
  6370. EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
  6371. EXPORT_SYMBOL_GPL(ata_do_eh);
  6372. EXPORT_SYMBOL_GPL(ata_std_error_handler);
  6373. EXPORT_SYMBOL_GPL(ata_cable_40wire);
  6374. EXPORT_SYMBOL_GPL(ata_cable_80wire);
  6375. EXPORT_SYMBOL_GPL(ata_cable_unknown);
  6376. EXPORT_SYMBOL_GPL(ata_cable_ignore);
  6377. EXPORT_SYMBOL_GPL(ata_cable_sata);
  6378. EXPORT_SYMBOL_GPL(ata_host_get);
  6379. EXPORT_SYMBOL_GPL(ata_host_put);