libata-core.c 185 KB

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