libata-core.c 191 KB

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