libata-core.c 154 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174
  1. /*
  2. * libata-core.c - helper library for ATA
  3. *
  4. * Maintained by: Jeff Garzik <jgarzik@pobox.com>
  5. * Please ALWAYS copy linux-ide@vger.kernel.org
  6. * on emails.
  7. *
  8. * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
  9. * Copyright 2003-2004 Jeff Garzik
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; see the file COPYING. If not, write to
  24. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. *
  27. * libata documentation is available via 'make {ps|pdf}docs',
  28. * as Documentation/DocBook/libata.*
  29. *
  30. * Hardware documentation available from http://www.t13.org/ and
  31. * http://www.sata-io.org/
  32. *
  33. * Standards documents from:
  34. * http://www.t13.org (ATA standards, PCI DMA IDE spec)
  35. * http://www.t10.org (SCSI MMC - for ATAPI MMC)
  36. * http://www.sata-io.org (SATA)
  37. * http://www.compactflash.org (CF)
  38. * http://www.qic.org (QIC157 - Tape and DSC)
  39. * http://www.ce-ata.org (CE-ATA: not supported)
  40. *
  41. */
  42. #include <linux/kernel.h>
  43. #include <linux/module.h>
  44. #include <linux/pci.h>
  45. #include <linux/init.h>
  46. #include <linux/list.h>
  47. #include <linux/mm.h>
  48. #include <linux/spinlock.h>
  49. #include <linux/blkdev.h>
  50. #include <linux/delay.h>
  51. #include <linux/timer.h>
  52. #include <linux/interrupt.h>
  53. #include <linux/completion.h>
  54. #include <linux/suspend.h>
  55. #include <linux/workqueue.h>
  56. #include <linux/jiffies.h>
  57. #include <linux/scatterlist.h>
  58. #include <linux/io.h>
  59. #include <scsi/scsi.h>
  60. #include <scsi/scsi_cmnd.h>
  61. #include <scsi/scsi_host.h>
  62. #include <linux/libata.h>
  63. #include <asm/semaphore.h>
  64. #include <asm/byteorder.h>
  65. #include <linux/cdrom.h>
  66. #include "libata.h"
  67. /* debounce timing parameters in msecs { interval, duration, timeout } */
  68. const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
  69. const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
  70. const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
  71. const struct ata_port_operations ata_base_port_ops = {
  72. .irq_clear = ata_noop_irq_clear,
  73. .prereset = ata_std_prereset,
  74. .hardreset = sata_std_hardreset,
  75. .postreset = ata_std_postreset,
  76. .error_handler = ata_std_error_handler,
  77. };
  78. const struct ata_port_operations sata_port_ops = {
  79. .inherits = &ata_base_port_ops,
  80. .qc_defer = ata_std_qc_defer,
  81. .dev_select = ata_noop_dev_select,
  82. };
  83. const struct ata_port_operations sata_pmp_port_ops = {
  84. .inherits = &sata_port_ops,
  85. .pmp_prereset = sata_pmp_std_prereset,
  86. .pmp_hardreset = sata_pmp_std_hardreset,
  87. .pmp_postreset = sata_pmp_std_postreset,
  88. .error_handler = sata_pmp_error_handler,
  89. };
  90. static unsigned int ata_dev_init_params(struct ata_device *dev,
  91. u16 heads, u16 sectors);
  92. static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
  93. static unsigned int ata_dev_set_feature(struct ata_device *dev,
  94. u8 enable, u8 feature);
  95. static void ata_dev_xfermask(struct ata_device *dev);
  96. static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
  97. unsigned int ata_print_id = 1;
  98. static struct workqueue_struct *ata_wq;
  99. struct workqueue_struct *ata_aux_wq;
  100. struct ata_force_param {
  101. const char *name;
  102. unsigned int cbl;
  103. int spd_limit;
  104. unsigned long xfer_mask;
  105. unsigned int horkage_on;
  106. unsigned int horkage_off;
  107. };
  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/kernel-parameters.txt for details)");
  119. 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)");
  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, 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; on by default (0=off, 1=on)");
  128. int libata_fua = 0;
  129. module_param_named(fua, libata_fua, int, 0444);
  130. MODULE_PARM_DESC(fua, "FUA support (0=off, 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 = ATA_TMOUT_INTERNAL / HZ;
  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, "Disables the use of ACPI in probe/suspend/resume when set");
  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");
  146. MODULE_AUTHOR("Jeff Garzik");
  147. MODULE_DESCRIPTION("Library module for ATA devices");
  148. MODULE_LICENSE("GPL");
  149. MODULE_VERSION(DRV_VERSION);
  150. /**
  151. * ata_force_cbl - force cable type according to libata.force
  152. * @ap: ATA port of interest
  153. *
  154. * Force cable type according to libata.force and whine about it.
  155. * The last entry which has matching port number is used, so it
  156. * can be specified as part of device force parameters. For
  157. * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
  158. * same effect.
  159. *
  160. * LOCKING:
  161. * EH context.
  162. */
  163. void ata_force_cbl(struct ata_port *ap)
  164. {
  165. int i;
  166. for (i = ata_force_tbl_size - 1; i >= 0; i--) {
  167. const struct ata_force_ent *fe = &ata_force_tbl[i];
  168. if (fe->port != -1 && fe->port != ap->print_id)
  169. continue;
  170. if (fe->param.cbl == ATA_CBL_NONE)
  171. continue;
  172. ap->cbl = fe->param.cbl;
  173. ata_port_printk(ap, KERN_NOTICE,
  174. "FORCE: cable set to %s\n", fe->param.name);
  175. return;
  176. }
  177. }
  178. /**
  179. * ata_force_spd_limit - force SATA spd limit according to libata.force
  180. * @link: ATA link of interest
  181. *
  182. * Force SATA spd limit according to libata.force and whine about
  183. * it. When only the port part is specified (e.g. 1:), the limit
  184. * applies to all links connected to both the host link and all
  185. * fan-out ports connected via PMP. If the device part is
  186. * specified as 0 (e.g. 1.00:), it specifies the first fan-out
  187. * link not the host link. Device number 15 always points to the
  188. * host link whether PMP is attached or not.
  189. *
  190. * LOCKING:
  191. * EH context.
  192. */
  193. static void ata_force_spd_limit(struct ata_link *link)
  194. {
  195. int linkno, i;
  196. if (ata_is_host_link(link))
  197. linkno = 15;
  198. else
  199. linkno = link->pmp;
  200. for (i = ata_force_tbl_size - 1; i >= 0; i--) {
  201. const struct ata_force_ent *fe = &ata_force_tbl[i];
  202. if (fe->port != -1 && fe->port != link->ap->print_id)
  203. continue;
  204. if (fe->device != -1 && fe->device != linkno)
  205. continue;
  206. if (!fe->param.spd_limit)
  207. continue;
  208. link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
  209. ata_link_printk(link, KERN_NOTICE,
  210. "FORCE: PHY spd limit set to %s\n", fe->param.name);
  211. return;
  212. }
  213. }
  214. /**
  215. * ata_force_xfermask - force xfermask according to libata.force
  216. * @dev: ATA device of interest
  217. *
  218. * Force xfer_mask according to libata.force and whine about it.
  219. * For consistency with link selection, device number 15 selects
  220. * the first device connected to the host link.
  221. *
  222. * LOCKING:
  223. * EH context.
  224. */
  225. static void ata_force_xfermask(struct ata_device *dev)
  226. {
  227. int devno = dev->link->pmp + dev->devno;
  228. int alt_devno = devno;
  229. int i;
  230. /* allow n.15 for the first device attached to host port */
  231. if (ata_is_host_link(dev->link) && devno == 0)
  232. alt_devno = 15;
  233. for (i = ata_force_tbl_size - 1; i >= 0; i--) {
  234. const struct ata_force_ent *fe = &ata_force_tbl[i];
  235. unsigned long pio_mask, mwdma_mask, udma_mask;
  236. if (fe->port != -1 && fe->port != dev->link->ap->print_id)
  237. continue;
  238. if (fe->device != -1 && fe->device != devno &&
  239. fe->device != alt_devno)
  240. continue;
  241. if (!fe->param.xfer_mask)
  242. continue;
  243. ata_unpack_xfermask(fe->param.xfer_mask,
  244. &pio_mask, &mwdma_mask, &udma_mask);
  245. if (udma_mask)
  246. dev->udma_mask = udma_mask;
  247. else if (mwdma_mask) {
  248. dev->udma_mask = 0;
  249. dev->mwdma_mask = mwdma_mask;
  250. } else {
  251. dev->udma_mask = 0;
  252. dev->mwdma_mask = 0;
  253. dev->pio_mask = pio_mask;
  254. }
  255. ata_dev_printk(dev, KERN_NOTICE,
  256. "FORCE: xfer_mask set to %s\n", fe->param.name);
  257. return;
  258. }
  259. }
  260. /**
  261. * ata_force_horkage - force horkage according to libata.force
  262. * @dev: ATA device of interest
  263. *
  264. * Force horkage according to libata.force and whine about it.
  265. * For consistency with link selection, device number 15 selects
  266. * the first device connected to the host link.
  267. *
  268. * LOCKING:
  269. * EH context.
  270. */
  271. static void ata_force_horkage(struct ata_device *dev)
  272. {
  273. int devno = dev->link->pmp + dev->devno;
  274. int alt_devno = devno;
  275. int i;
  276. /* allow n.15 for the first device attached to host port */
  277. if (ata_is_host_link(dev->link) && devno == 0)
  278. alt_devno = 15;
  279. for (i = 0; i < ata_force_tbl_size; i++) {
  280. const struct ata_force_ent *fe = &ata_force_tbl[i];
  281. if (fe->port != -1 && fe->port != dev->link->ap->print_id)
  282. continue;
  283. if (fe->device != -1 && fe->device != devno &&
  284. fe->device != alt_devno)
  285. continue;
  286. if (!(~dev->horkage & fe->param.horkage_on) &&
  287. !(dev->horkage & fe->param.horkage_off))
  288. continue;
  289. dev->horkage |= fe->param.horkage_on;
  290. dev->horkage &= ~fe->param.horkage_off;
  291. ata_dev_printk(dev, KERN_NOTICE,
  292. "FORCE: horkage modified (%s)\n", fe->param.name);
  293. }
  294. }
  295. /**
  296. * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
  297. * @opcode: SCSI opcode
  298. *
  299. * Determine ATAPI command type from @opcode.
  300. *
  301. * LOCKING:
  302. * None.
  303. *
  304. * RETURNS:
  305. * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
  306. */
  307. int atapi_cmd_type(u8 opcode)
  308. {
  309. switch (opcode) {
  310. case GPCMD_READ_10:
  311. case GPCMD_READ_12:
  312. return ATAPI_READ;
  313. case GPCMD_WRITE_10:
  314. case GPCMD_WRITE_12:
  315. case GPCMD_WRITE_AND_VERIFY_10:
  316. return ATAPI_WRITE;
  317. case GPCMD_READ_CD:
  318. case GPCMD_READ_CD_MSF:
  319. return ATAPI_READ_CD;
  320. case ATA_16:
  321. case ATA_12:
  322. if (atapi_passthru16)
  323. return ATAPI_PASS_THRU;
  324. /* fall thru */
  325. default:
  326. return ATAPI_MISC;
  327. }
  328. }
  329. /**
  330. * ata_noop_irq_clear - Noop placeholder for irq_clear
  331. * @ap: Port associated with this ATA transaction.
  332. */
  333. void ata_noop_irq_clear(struct ata_port *ap)
  334. {
  335. }
  336. /**
  337. * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
  338. * @tf: Taskfile to convert
  339. * @pmp: Port multiplier port
  340. * @is_cmd: This FIS is for command
  341. * @fis: Buffer into which data will output
  342. *
  343. * Converts a standard ATA taskfile to a Serial ATA
  344. * FIS structure (Register - Host to Device).
  345. *
  346. * LOCKING:
  347. * Inherited from caller.
  348. */
  349. void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
  350. {
  351. fis[0] = 0x27; /* Register - Host to Device FIS */
  352. fis[1] = pmp & 0xf; /* Port multiplier number*/
  353. if (is_cmd)
  354. fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
  355. fis[2] = tf->command;
  356. fis[3] = tf->feature;
  357. fis[4] = tf->lbal;
  358. fis[5] = tf->lbam;
  359. fis[6] = tf->lbah;
  360. fis[7] = tf->device;
  361. fis[8] = tf->hob_lbal;
  362. fis[9] = tf->hob_lbam;
  363. fis[10] = tf->hob_lbah;
  364. fis[11] = tf->hob_feature;
  365. fis[12] = tf->nsect;
  366. fis[13] = tf->hob_nsect;
  367. fis[14] = 0;
  368. fis[15] = tf->ctl;
  369. fis[16] = 0;
  370. fis[17] = 0;
  371. fis[18] = 0;
  372. fis[19] = 0;
  373. }
  374. /**
  375. * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
  376. * @fis: Buffer from which data will be input
  377. * @tf: Taskfile to output
  378. *
  379. * Converts a serial ATA FIS structure to a standard ATA taskfile.
  380. *
  381. * LOCKING:
  382. * Inherited from caller.
  383. */
  384. void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
  385. {
  386. tf->command = fis[2]; /* status */
  387. tf->feature = fis[3]; /* error */
  388. tf->lbal = fis[4];
  389. tf->lbam = fis[5];
  390. tf->lbah = fis[6];
  391. tf->device = fis[7];
  392. tf->hob_lbal = fis[8];
  393. tf->hob_lbam = fis[9];
  394. tf->hob_lbah = fis[10];
  395. tf->nsect = fis[12];
  396. tf->hob_nsect = fis[13];
  397. }
  398. static const u8 ata_rw_cmds[] = {
  399. /* pio multi */
  400. ATA_CMD_READ_MULTI,
  401. ATA_CMD_WRITE_MULTI,
  402. ATA_CMD_READ_MULTI_EXT,
  403. ATA_CMD_WRITE_MULTI_EXT,
  404. 0,
  405. 0,
  406. 0,
  407. ATA_CMD_WRITE_MULTI_FUA_EXT,
  408. /* pio */
  409. ATA_CMD_PIO_READ,
  410. ATA_CMD_PIO_WRITE,
  411. ATA_CMD_PIO_READ_EXT,
  412. ATA_CMD_PIO_WRITE_EXT,
  413. 0,
  414. 0,
  415. 0,
  416. 0,
  417. /* dma */
  418. ATA_CMD_READ,
  419. ATA_CMD_WRITE,
  420. ATA_CMD_READ_EXT,
  421. ATA_CMD_WRITE_EXT,
  422. 0,
  423. 0,
  424. 0,
  425. ATA_CMD_WRITE_FUA_EXT
  426. };
  427. /**
  428. * ata_rwcmd_protocol - set taskfile r/w commands and protocol
  429. * @tf: command to examine and configure
  430. * @dev: device tf belongs to
  431. *
  432. * Examine the device configuration and tf->flags to calculate
  433. * the proper read/write commands and protocol to use.
  434. *
  435. * LOCKING:
  436. * caller.
  437. */
  438. static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
  439. {
  440. u8 cmd;
  441. int index, fua, lba48, write;
  442. fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
  443. lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
  444. write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
  445. if (dev->flags & ATA_DFLAG_PIO) {
  446. tf->protocol = ATA_PROT_PIO;
  447. index = dev->multi_count ? 0 : 8;
  448. } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
  449. /* Unable to use DMA due to host limitation */
  450. tf->protocol = ATA_PROT_PIO;
  451. index = dev->multi_count ? 0 : 8;
  452. } else {
  453. tf->protocol = ATA_PROT_DMA;
  454. index = 16;
  455. }
  456. cmd = ata_rw_cmds[index + fua + lba48 + write];
  457. if (cmd) {
  458. tf->command = cmd;
  459. return 0;
  460. }
  461. return -1;
  462. }
  463. /**
  464. * ata_tf_read_block - Read block address from ATA taskfile
  465. * @tf: ATA taskfile of interest
  466. * @dev: ATA device @tf belongs to
  467. *
  468. * LOCKING:
  469. * None.
  470. *
  471. * Read block address from @tf. This function can handle all
  472. * three address formats - LBA, LBA48 and CHS. tf->protocol and
  473. * flags select the address format to use.
  474. *
  475. * RETURNS:
  476. * Block address read from @tf.
  477. */
  478. u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
  479. {
  480. u64 block = 0;
  481. if (tf->flags & ATA_TFLAG_LBA) {
  482. if (tf->flags & ATA_TFLAG_LBA48) {
  483. block |= (u64)tf->hob_lbah << 40;
  484. block |= (u64)tf->hob_lbam << 32;
  485. block |= tf->hob_lbal << 24;
  486. } else
  487. block |= (tf->device & 0xf) << 24;
  488. block |= tf->lbah << 16;
  489. block |= tf->lbam << 8;
  490. block |= tf->lbal;
  491. } else {
  492. u32 cyl, head, sect;
  493. cyl = tf->lbam | (tf->lbah << 8);
  494. head = tf->device & 0xf;
  495. sect = tf->lbal;
  496. block = (cyl * dev->heads + head) * dev->sectors + sect;
  497. }
  498. return block;
  499. }
  500. /**
  501. * ata_build_rw_tf - Build ATA taskfile for given read/write request
  502. * @tf: Target ATA taskfile
  503. * @dev: ATA device @tf belongs to
  504. * @block: Block address
  505. * @n_block: Number of blocks
  506. * @tf_flags: RW/FUA etc...
  507. * @tag: tag
  508. *
  509. * LOCKING:
  510. * None.
  511. *
  512. * Build ATA taskfile @tf for read/write request described by
  513. * @block, @n_block, @tf_flags and @tag on @dev.
  514. *
  515. * RETURNS:
  516. *
  517. * 0 on success, -ERANGE if the request is too large for @dev,
  518. * -EINVAL if the request is invalid.
  519. */
  520. int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
  521. u64 block, u32 n_block, unsigned int tf_flags,
  522. unsigned int tag)
  523. {
  524. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  525. tf->flags |= tf_flags;
  526. if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
  527. /* yay, NCQ */
  528. if (!lba_48_ok(block, n_block))
  529. return -ERANGE;
  530. tf->protocol = ATA_PROT_NCQ;
  531. tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
  532. if (tf->flags & ATA_TFLAG_WRITE)
  533. tf->command = ATA_CMD_FPDMA_WRITE;
  534. else
  535. tf->command = ATA_CMD_FPDMA_READ;
  536. tf->nsect = tag << 3;
  537. tf->hob_feature = (n_block >> 8) & 0xff;
  538. tf->feature = n_block & 0xff;
  539. tf->hob_lbah = (block >> 40) & 0xff;
  540. tf->hob_lbam = (block >> 32) & 0xff;
  541. tf->hob_lbal = (block >> 24) & 0xff;
  542. tf->lbah = (block >> 16) & 0xff;
  543. tf->lbam = (block >> 8) & 0xff;
  544. tf->lbal = block & 0xff;
  545. tf->device = 1 << 6;
  546. if (tf->flags & ATA_TFLAG_FUA)
  547. tf->device |= 1 << 7;
  548. } else if (dev->flags & ATA_DFLAG_LBA) {
  549. tf->flags |= ATA_TFLAG_LBA;
  550. if (lba_28_ok(block, n_block)) {
  551. /* use LBA28 */
  552. tf->device |= (block >> 24) & 0xf;
  553. } else if (lba_48_ok(block, n_block)) {
  554. if (!(dev->flags & ATA_DFLAG_LBA48))
  555. return -ERANGE;
  556. /* use LBA48 */
  557. tf->flags |= ATA_TFLAG_LBA48;
  558. tf->hob_nsect = (n_block >> 8) & 0xff;
  559. tf->hob_lbah = (block >> 40) & 0xff;
  560. tf->hob_lbam = (block >> 32) & 0xff;
  561. tf->hob_lbal = (block >> 24) & 0xff;
  562. } else
  563. /* request too large even for LBA48 */
  564. return -ERANGE;
  565. if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
  566. return -EINVAL;
  567. tf->nsect = n_block & 0xff;
  568. tf->lbah = (block >> 16) & 0xff;
  569. tf->lbam = (block >> 8) & 0xff;
  570. tf->lbal = block & 0xff;
  571. tf->device |= ATA_LBA;
  572. } else {
  573. /* CHS */
  574. u32 sect, head, cyl, track;
  575. /* The request -may- be too large for CHS addressing. */
  576. if (!lba_28_ok(block, n_block))
  577. return -ERANGE;
  578. if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
  579. return -EINVAL;
  580. /* Convert LBA to CHS */
  581. track = (u32)block / dev->sectors;
  582. cyl = track / dev->heads;
  583. head = track % dev->heads;
  584. sect = (u32)block % dev->sectors + 1;
  585. DPRINTK("block %u track %u cyl %u head %u sect %u\n",
  586. (u32)block, track, cyl, head, sect);
  587. /* Check whether the converted CHS can fit.
  588. Cylinder: 0-65535
  589. Head: 0-15
  590. Sector: 1-255*/
  591. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  592. return -ERANGE;
  593. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  594. tf->lbal = sect;
  595. tf->lbam = cyl;
  596. tf->lbah = cyl >> 8;
  597. tf->device |= head;
  598. }
  599. return 0;
  600. }
  601. /**
  602. * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
  603. * @pio_mask: pio_mask
  604. * @mwdma_mask: mwdma_mask
  605. * @udma_mask: udma_mask
  606. *
  607. * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
  608. * unsigned int xfer_mask.
  609. *
  610. * LOCKING:
  611. * None.
  612. *
  613. * RETURNS:
  614. * Packed xfer_mask.
  615. */
  616. unsigned long ata_pack_xfermask(unsigned long pio_mask,
  617. unsigned long mwdma_mask,
  618. unsigned long udma_mask)
  619. {
  620. return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
  621. ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
  622. ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
  623. }
  624. /**
  625. * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
  626. * @xfer_mask: xfer_mask to unpack
  627. * @pio_mask: resulting pio_mask
  628. * @mwdma_mask: resulting mwdma_mask
  629. * @udma_mask: resulting udma_mask
  630. *
  631. * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
  632. * Any NULL distination masks will be ignored.
  633. */
  634. void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
  635. unsigned long *mwdma_mask, unsigned long *udma_mask)
  636. {
  637. if (pio_mask)
  638. *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
  639. if (mwdma_mask)
  640. *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
  641. if (udma_mask)
  642. *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
  643. }
  644. static const struct ata_xfer_ent {
  645. int shift, bits;
  646. u8 base;
  647. } ata_xfer_tbl[] = {
  648. { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
  649. { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
  650. { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
  651. { -1, },
  652. };
  653. /**
  654. * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
  655. * @xfer_mask: xfer_mask of interest
  656. *
  657. * Return matching XFER_* value for @xfer_mask. Only the highest
  658. * bit of @xfer_mask is considered.
  659. *
  660. * LOCKING:
  661. * None.
  662. *
  663. * RETURNS:
  664. * Matching XFER_* value, 0xff if no match found.
  665. */
  666. u8 ata_xfer_mask2mode(unsigned long xfer_mask)
  667. {
  668. int highbit = fls(xfer_mask) - 1;
  669. const struct ata_xfer_ent *ent;
  670. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  671. if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
  672. return ent->base + highbit - ent->shift;
  673. return 0xff;
  674. }
  675. /**
  676. * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
  677. * @xfer_mode: XFER_* of interest
  678. *
  679. * Return matching xfer_mask for @xfer_mode.
  680. *
  681. * LOCKING:
  682. * None.
  683. *
  684. * RETURNS:
  685. * Matching xfer_mask, 0 if no match found.
  686. */
  687. unsigned long ata_xfer_mode2mask(u8 xfer_mode)
  688. {
  689. const struct ata_xfer_ent *ent;
  690. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  691. if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
  692. return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
  693. & ~((1 << ent->shift) - 1);
  694. return 0;
  695. }
  696. /**
  697. * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
  698. * @xfer_mode: XFER_* of interest
  699. *
  700. * Return matching xfer_shift for @xfer_mode.
  701. *
  702. * LOCKING:
  703. * None.
  704. *
  705. * RETURNS:
  706. * Matching xfer_shift, -1 if no match found.
  707. */
  708. int ata_xfer_mode2shift(unsigned long xfer_mode)
  709. {
  710. const struct ata_xfer_ent *ent;
  711. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  712. if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
  713. return ent->shift;
  714. return -1;
  715. }
  716. /**
  717. * ata_mode_string - convert xfer_mask to string
  718. * @xfer_mask: mask of bits supported; only highest bit counts.
  719. *
  720. * Determine string which represents the highest speed
  721. * (highest bit in @modemask).
  722. *
  723. * LOCKING:
  724. * None.
  725. *
  726. * RETURNS:
  727. * Constant C string representing highest speed listed in
  728. * @mode_mask, or the constant C string "<n/a>".
  729. */
  730. const char *ata_mode_string(unsigned long xfer_mask)
  731. {
  732. static const char * const xfer_mode_str[] = {
  733. "PIO0",
  734. "PIO1",
  735. "PIO2",
  736. "PIO3",
  737. "PIO4",
  738. "PIO5",
  739. "PIO6",
  740. "MWDMA0",
  741. "MWDMA1",
  742. "MWDMA2",
  743. "MWDMA3",
  744. "MWDMA4",
  745. "UDMA/16",
  746. "UDMA/25",
  747. "UDMA/33",
  748. "UDMA/44",
  749. "UDMA/66",
  750. "UDMA/100",
  751. "UDMA/133",
  752. "UDMA7",
  753. };
  754. int highbit;
  755. highbit = fls(xfer_mask) - 1;
  756. if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
  757. return xfer_mode_str[highbit];
  758. return "<n/a>";
  759. }
  760. static const char *sata_spd_string(unsigned int spd)
  761. {
  762. static const char * const spd_str[] = {
  763. "1.5 Gbps",
  764. "3.0 Gbps",
  765. };
  766. if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
  767. return "<unknown>";
  768. return spd_str[spd - 1];
  769. }
  770. void ata_dev_disable(struct ata_device *dev)
  771. {
  772. if (ata_dev_enabled(dev)) {
  773. if (ata_msg_drv(dev->link->ap))
  774. ata_dev_printk(dev, KERN_WARNING, "disabled\n");
  775. ata_acpi_on_disable(dev);
  776. ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 |
  777. ATA_DNXFER_QUIET);
  778. dev->class++;
  779. }
  780. }
  781. static int ata_dev_set_dipm(struct ata_device *dev, enum link_pm policy)
  782. {
  783. struct ata_link *link = dev->link;
  784. struct ata_port *ap = link->ap;
  785. u32 scontrol;
  786. unsigned int err_mask;
  787. int rc;
  788. /*
  789. * disallow DIPM for drivers which haven't set
  790. * ATA_FLAG_IPM. This is because when DIPM is enabled,
  791. * phy ready will be set in the interrupt status on
  792. * state changes, which will cause some drivers to
  793. * think there are errors - additionally drivers will
  794. * need to disable hot plug.
  795. */
  796. if (!(ap->flags & ATA_FLAG_IPM) || !ata_dev_enabled(dev)) {
  797. ap->pm_policy = NOT_AVAILABLE;
  798. return -EINVAL;
  799. }
  800. /*
  801. * For DIPM, we will only enable it for the
  802. * min_power setting.
  803. *
  804. * Why? Because Disks are too stupid to know that
  805. * If the host rejects a request to go to SLUMBER
  806. * they should retry at PARTIAL, and instead it
  807. * just would give up. So, for medium_power to
  808. * work at all, we need to only allow HIPM.
  809. */
  810. rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
  811. if (rc)
  812. return rc;
  813. switch (policy) {
  814. case MIN_POWER:
  815. /* no restrictions on IPM transitions */
  816. scontrol &= ~(0x3 << 8);
  817. rc = sata_scr_write(link, SCR_CONTROL, scontrol);
  818. if (rc)
  819. return rc;
  820. /* enable DIPM */
  821. if (dev->flags & ATA_DFLAG_DIPM)
  822. err_mask = ata_dev_set_feature(dev,
  823. SETFEATURES_SATA_ENABLE, SATA_DIPM);
  824. break;
  825. case MEDIUM_POWER:
  826. /* allow IPM to PARTIAL */
  827. scontrol &= ~(0x1 << 8);
  828. scontrol |= (0x2 << 8);
  829. rc = sata_scr_write(link, SCR_CONTROL, scontrol);
  830. if (rc)
  831. return rc;
  832. /*
  833. * we don't have to disable DIPM since IPM flags
  834. * disallow transitions to SLUMBER, which effectively
  835. * disable DIPM if it does not support PARTIAL
  836. */
  837. break;
  838. case NOT_AVAILABLE:
  839. case MAX_PERFORMANCE:
  840. /* disable all IPM transitions */
  841. scontrol |= (0x3 << 8);
  842. rc = sata_scr_write(link, SCR_CONTROL, scontrol);
  843. if (rc)
  844. return rc;
  845. /*
  846. * we don't have to disable DIPM since IPM flags
  847. * disallow all transitions which effectively
  848. * disable DIPM anyway.
  849. */
  850. break;
  851. }
  852. /* FIXME: handle SET FEATURES failure */
  853. (void) err_mask;
  854. return 0;
  855. }
  856. /**
  857. * ata_dev_enable_pm - enable SATA interface power management
  858. * @dev: device to enable power management
  859. * @policy: the link power management policy
  860. *
  861. * Enable SATA Interface power management. This will enable
  862. * Device Interface Power Management (DIPM) for min_power
  863. * policy, and then call driver specific callbacks for
  864. * enabling Host Initiated Power management.
  865. *
  866. * Locking: Caller.
  867. * Returns: -EINVAL if IPM is not supported, 0 otherwise.
  868. */
  869. void ata_dev_enable_pm(struct ata_device *dev, enum link_pm policy)
  870. {
  871. int rc = 0;
  872. struct ata_port *ap = dev->link->ap;
  873. /* set HIPM first, then DIPM */
  874. if (ap->ops->enable_pm)
  875. rc = ap->ops->enable_pm(ap, policy);
  876. if (rc)
  877. goto enable_pm_out;
  878. rc = ata_dev_set_dipm(dev, policy);
  879. enable_pm_out:
  880. if (rc)
  881. ap->pm_policy = MAX_PERFORMANCE;
  882. else
  883. ap->pm_policy = policy;
  884. return /* rc */; /* hopefully we can use 'rc' eventually */
  885. }
  886. #ifdef CONFIG_PM
  887. /**
  888. * ata_dev_disable_pm - disable SATA interface power management
  889. * @dev: device to disable power management
  890. *
  891. * Disable SATA Interface power management. This will disable
  892. * Device Interface Power Management (DIPM) without changing
  893. * policy, call driver specific callbacks for disabling Host
  894. * Initiated Power management.
  895. *
  896. * Locking: Caller.
  897. * Returns: void
  898. */
  899. static void ata_dev_disable_pm(struct ata_device *dev)
  900. {
  901. struct ata_port *ap = dev->link->ap;
  902. ata_dev_set_dipm(dev, MAX_PERFORMANCE);
  903. if (ap->ops->disable_pm)
  904. ap->ops->disable_pm(ap);
  905. }
  906. #endif /* CONFIG_PM */
  907. void ata_lpm_schedule(struct ata_port *ap, enum link_pm policy)
  908. {
  909. ap->pm_policy = policy;
  910. ap->link.eh_info.action |= ATA_EH_LPM;
  911. ap->link.eh_info.flags |= ATA_EHI_NO_AUTOPSY;
  912. ata_port_schedule_eh(ap);
  913. }
  914. #ifdef CONFIG_PM
  915. static void ata_lpm_enable(struct ata_host *host)
  916. {
  917. struct ata_link *link;
  918. struct ata_port *ap;
  919. struct ata_device *dev;
  920. int i;
  921. for (i = 0; i < host->n_ports; i++) {
  922. ap = host->ports[i];
  923. ata_port_for_each_link(link, ap) {
  924. ata_link_for_each_dev(dev, link)
  925. ata_dev_disable_pm(dev);
  926. }
  927. }
  928. }
  929. static void ata_lpm_disable(struct ata_host *host)
  930. {
  931. int i;
  932. for (i = 0; i < host->n_ports; i++) {
  933. struct ata_port *ap = host->ports[i];
  934. ata_lpm_schedule(ap, ap->pm_policy);
  935. }
  936. }
  937. #endif /* CONFIG_PM */
  938. /**
  939. * ata_dev_classify - determine device type based on ATA-spec signature
  940. * @tf: ATA taskfile register set for device to be identified
  941. *
  942. * Determine from taskfile register contents whether a device is
  943. * ATA or ATAPI, as per "Signature and persistence" section
  944. * of ATA/PI spec (volume 1, sect 5.14).
  945. *
  946. * LOCKING:
  947. * None.
  948. *
  949. * RETURNS:
  950. * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
  951. * %ATA_DEV_UNKNOWN the event of failure.
  952. */
  953. unsigned int ata_dev_classify(const struct ata_taskfile *tf)
  954. {
  955. /* Apple's open source Darwin code hints that some devices only
  956. * put a proper signature into the LBA mid/high registers,
  957. * So, we only check those. It's sufficient for uniqueness.
  958. *
  959. * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
  960. * signatures for ATA and ATAPI devices attached on SerialATA,
  961. * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
  962. * spec has never mentioned about using different signatures
  963. * for ATA/ATAPI devices. Then, Serial ATA II: Port
  964. * Multiplier specification began to use 0x69/0x96 to identify
  965. * port multpliers and 0x3c/0xc3 to identify SEMB device.
  966. * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
  967. * 0x69/0x96 shortly and described them as reserved for
  968. * SerialATA.
  969. *
  970. * We follow the current spec and consider that 0x69/0x96
  971. * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
  972. */
  973. if ((tf->lbam == 0) && (tf->lbah == 0)) {
  974. DPRINTK("found ATA device by sig\n");
  975. return ATA_DEV_ATA;
  976. }
  977. if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
  978. DPRINTK("found ATAPI device by sig\n");
  979. return ATA_DEV_ATAPI;
  980. }
  981. if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
  982. DPRINTK("found PMP device by sig\n");
  983. return ATA_DEV_PMP;
  984. }
  985. if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
  986. printk(KERN_INFO "ata: SEMB device ignored\n");
  987. return ATA_DEV_SEMB_UNSUP; /* not yet */
  988. }
  989. DPRINTK("unknown device\n");
  990. return ATA_DEV_UNKNOWN;
  991. }
  992. /**
  993. * ata_id_string - Convert IDENTIFY DEVICE page into string
  994. * @id: IDENTIFY DEVICE results we will examine
  995. * @s: string into which data is output
  996. * @ofs: offset into identify device page
  997. * @len: length of string to return. must be an even number.
  998. *
  999. * The strings in the IDENTIFY DEVICE page are broken up into
  1000. * 16-bit chunks. Run through the string, and output each
  1001. * 8-bit chunk linearly, regardless of platform.
  1002. *
  1003. * LOCKING:
  1004. * caller.
  1005. */
  1006. void ata_id_string(const u16 *id, unsigned char *s,
  1007. unsigned int ofs, unsigned int len)
  1008. {
  1009. unsigned int c;
  1010. while (len > 0) {
  1011. c = id[ofs] >> 8;
  1012. *s = c;
  1013. s++;
  1014. c = id[ofs] & 0xff;
  1015. *s = c;
  1016. s++;
  1017. ofs++;
  1018. len -= 2;
  1019. }
  1020. }
  1021. /**
  1022. * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
  1023. * @id: IDENTIFY DEVICE results we will examine
  1024. * @s: string into which data is output
  1025. * @ofs: offset into identify device page
  1026. * @len: length of string to return. must be an odd number.
  1027. *
  1028. * This function is identical to ata_id_string except that it
  1029. * trims trailing spaces and terminates the resulting string with
  1030. * null. @len must be actual maximum length (even number) + 1.
  1031. *
  1032. * LOCKING:
  1033. * caller.
  1034. */
  1035. void ata_id_c_string(const u16 *id, unsigned char *s,
  1036. unsigned int ofs, unsigned int len)
  1037. {
  1038. unsigned char *p;
  1039. WARN_ON(!(len & 1));
  1040. ata_id_string(id, s, ofs, len - 1);
  1041. p = s + strnlen(s, len - 1);
  1042. while (p > s && p[-1] == ' ')
  1043. p--;
  1044. *p = '\0';
  1045. }
  1046. static u64 ata_id_n_sectors(const u16 *id)
  1047. {
  1048. if (ata_id_has_lba(id)) {
  1049. if (ata_id_has_lba48(id))
  1050. return ata_id_u64(id, 100);
  1051. else
  1052. return ata_id_u32(id, 60);
  1053. } else {
  1054. if (ata_id_current_chs_valid(id))
  1055. return ata_id_u32(id, 57);
  1056. else
  1057. return id[1] * id[3] * id[6];
  1058. }
  1059. }
  1060. static u64 ata_tf_to_lba48(struct ata_taskfile *tf)
  1061. {
  1062. u64 sectors = 0;
  1063. sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
  1064. sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
  1065. sectors |= (tf->hob_lbal & 0xff) << 24;
  1066. sectors |= (tf->lbah & 0xff) << 16;
  1067. sectors |= (tf->lbam & 0xff) << 8;
  1068. sectors |= (tf->lbal & 0xff);
  1069. return ++sectors;
  1070. }
  1071. static u64 ata_tf_to_lba(struct ata_taskfile *tf)
  1072. {
  1073. u64 sectors = 0;
  1074. sectors |= (tf->device & 0x0f) << 24;
  1075. sectors |= (tf->lbah & 0xff) << 16;
  1076. sectors |= (tf->lbam & 0xff) << 8;
  1077. sectors |= (tf->lbal & 0xff);
  1078. return ++sectors;
  1079. }
  1080. /**
  1081. * ata_read_native_max_address - Read native max address
  1082. * @dev: target device
  1083. * @max_sectors: out parameter for the result native max address
  1084. *
  1085. * Perform an LBA48 or LBA28 native size query upon the device in
  1086. * question.
  1087. *
  1088. * RETURNS:
  1089. * 0 on success, -EACCES if command is aborted by the drive.
  1090. * -EIO on other errors.
  1091. */
  1092. static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
  1093. {
  1094. unsigned int err_mask;
  1095. struct ata_taskfile tf;
  1096. int lba48 = ata_id_has_lba48(dev->id);
  1097. ata_tf_init(dev, &tf);
  1098. /* always clear all address registers */
  1099. tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  1100. if (lba48) {
  1101. tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
  1102. tf.flags |= ATA_TFLAG_LBA48;
  1103. } else
  1104. tf.command = ATA_CMD_READ_NATIVE_MAX;
  1105. tf.protocol |= ATA_PROT_NODATA;
  1106. tf.device |= ATA_LBA;
  1107. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  1108. if (err_mask) {
  1109. ata_dev_printk(dev, KERN_WARNING, "failed to read native "
  1110. "max address (err_mask=0x%x)\n", err_mask);
  1111. if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
  1112. return -EACCES;
  1113. return -EIO;
  1114. }
  1115. if (lba48)
  1116. *max_sectors = ata_tf_to_lba48(&tf);
  1117. else
  1118. *max_sectors = ata_tf_to_lba(&tf);
  1119. if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
  1120. (*max_sectors)--;
  1121. return 0;
  1122. }
  1123. /**
  1124. * ata_set_max_sectors - Set max sectors
  1125. * @dev: target device
  1126. * @new_sectors: new max sectors value to set for the device
  1127. *
  1128. * Set max sectors of @dev to @new_sectors.
  1129. *
  1130. * RETURNS:
  1131. * 0 on success, -EACCES if command is aborted or denied (due to
  1132. * previous non-volatile SET_MAX) by the drive. -EIO on other
  1133. * errors.
  1134. */
  1135. static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
  1136. {
  1137. unsigned int err_mask;
  1138. struct ata_taskfile tf;
  1139. int lba48 = ata_id_has_lba48(dev->id);
  1140. new_sectors--;
  1141. ata_tf_init(dev, &tf);
  1142. tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  1143. if (lba48) {
  1144. tf.command = ATA_CMD_SET_MAX_EXT;
  1145. tf.flags |= ATA_TFLAG_LBA48;
  1146. tf.hob_lbal = (new_sectors >> 24) & 0xff;
  1147. tf.hob_lbam = (new_sectors >> 32) & 0xff;
  1148. tf.hob_lbah = (new_sectors >> 40) & 0xff;
  1149. } else {
  1150. tf.command = ATA_CMD_SET_MAX;
  1151. tf.device |= (new_sectors >> 24) & 0xf;
  1152. }
  1153. tf.protocol |= ATA_PROT_NODATA;
  1154. tf.device |= ATA_LBA;
  1155. tf.lbal = (new_sectors >> 0) & 0xff;
  1156. tf.lbam = (new_sectors >> 8) & 0xff;
  1157. tf.lbah = (new_sectors >> 16) & 0xff;
  1158. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  1159. if (err_mask) {
  1160. ata_dev_printk(dev, KERN_WARNING, "failed to set "
  1161. "max address (err_mask=0x%x)\n", err_mask);
  1162. if (err_mask == AC_ERR_DEV &&
  1163. (tf.feature & (ATA_ABORTED | ATA_IDNF)))
  1164. return -EACCES;
  1165. return -EIO;
  1166. }
  1167. return 0;
  1168. }
  1169. /**
  1170. * ata_hpa_resize - Resize a device with an HPA set
  1171. * @dev: Device to resize
  1172. *
  1173. * Read the size of an LBA28 or LBA48 disk with HPA features and resize
  1174. * it if required to the full size of the media. The caller must check
  1175. * the drive has the HPA feature set enabled.
  1176. *
  1177. * RETURNS:
  1178. * 0 on success, -errno on failure.
  1179. */
  1180. static int ata_hpa_resize(struct ata_device *dev)
  1181. {
  1182. struct ata_eh_context *ehc = &dev->link->eh_context;
  1183. int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
  1184. u64 sectors = ata_id_n_sectors(dev->id);
  1185. u64 native_sectors;
  1186. int rc;
  1187. /* do we need to do it? */
  1188. if (dev->class != ATA_DEV_ATA ||
  1189. !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
  1190. (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
  1191. return 0;
  1192. /* read native max address */
  1193. rc = ata_read_native_max_address(dev, &native_sectors);
  1194. if (rc) {
  1195. /* If device aborted the command or HPA isn't going to
  1196. * be unlocked, skip HPA resizing.
  1197. */
  1198. if (rc == -EACCES || !ata_ignore_hpa) {
  1199. ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
  1200. "broken, skipping HPA handling\n");
  1201. dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
  1202. /* we can continue if device aborted the command */
  1203. if (rc == -EACCES)
  1204. rc = 0;
  1205. }
  1206. return rc;
  1207. }
  1208. /* nothing to do? */
  1209. if (native_sectors <= sectors || !ata_ignore_hpa) {
  1210. if (!print_info || native_sectors == sectors)
  1211. return 0;
  1212. if (native_sectors > sectors)
  1213. ata_dev_printk(dev, KERN_INFO,
  1214. "HPA detected: current %llu, native %llu\n",
  1215. (unsigned long long)sectors,
  1216. (unsigned long long)native_sectors);
  1217. else if (native_sectors < sectors)
  1218. ata_dev_printk(dev, KERN_WARNING,
  1219. "native sectors (%llu) is smaller than "
  1220. "sectors (%llu)\n",
  1221. (unsigned long long)native_sectors,
  1222. (unsigned long long)sectors);
  1223. return 0;
  1224. }
  1225. /* let's unlock HPA */
  1226. rc = ata_set_max_sectors(dev, native_sectors);
  1227. if (rc == -EACCES) {
  1228. /* if device aborted the command, skip HPA resizing */
  1229. ata_dev_printk(dev, KERN_WARNING, "device aborted resize "
  1230. "(%llu -> %llu), skipping HPA handling\n",
  1231. (unsigned long long)sectors,
  1232. (unsigned long long)native_sectors);
  1233. dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
  1234. return 0;
  1235. } else if (rc)
  1236. return rc;
  1237. /* re-read IDENTIFY data */
  1238. rc = ata_dev_reread_id(dev, 0);
  1239. if (rc) {
  1240. ata_dev_printk(dev, KERN_ERR, "failed to re-read IDENTIFY "
  1241. "data after HPA resizing\n");
  1242. return rc;
  1243. }
  1244. if (print_info) {
  1245. u64 new_sectors = ata_id_n_sectors(dev->id);
  1246. ata_dev_printk(dev, KERN_INFO,
  1247. "HPA unlocked: %llu -> %llu, native %llu\n",
  1248. (unsigned long long)sectors,
  1249. (unsigned long long)new_sectors,
  1250. (unsigned long long)native_sectors);
  1251. }
  1252. return 0;
  1253. }
  1254. /**
  1255. * ata_noop_dev_select - Select device 0/1 on ATA bus
  1256. * @ap: ATA channel to manipulate
  1257. * @device: ATA device (numbered from zero) to select
  1258. *
  1259. * This function performs no actual function.
  1260. *
  1261. * May be used as the dev_select() entry in ata_port_operations.
  1262. *
  1263. * LOCKING:
  1264. * caller.
  1265. */
  1266. void ata_noop_dev_select(struct ata_port *ap, unsigned int device)
  1267. {
  1268. }
  1269. /**
  1270. * ata_dump_id - IDENTIFY DEVICE info debugging output
  1271. * @id: IDENTIFY DEVICE page to dump
  1272. *
  1273. * Dump selected 16-bit words from the given IDENTIFY DEVICE
  1274. * page.
  1275. *
  1276. * LOCKING:
  1277. * caller.
  1278. */
  1279. static inline void ata_dump_id(const u16 *id)
  1280. {
  1281. DPRINTK("49==0x%04x "
  1282. "53==0x%04x "
  1283. "63==0x%04x "
  1284. "64==0x%04x "
  1285. "75==0x%04x \n",
  1286. id[49],
  1287. id[53],
  1288. id[63],
  1289. id[64],
  1290. id[75]);
  1291. DPRINTK("80==0x%04x "
  1292. "81==0x%04x "
  1293. "82==0x%04x "
  1294. "83==0x%04x "
  1295. "84==0x%04x \n",
  1296. id[80],
  1297. id[81],
  1298. id[82],
  1299. id[83],
  1300. id[84]);
  1301. DPRINTK("88==0x%04x "
  1302. "93==0x%04x\n",
  1303. id[88],
  1304. id[93]);
  1305. }
  1306. /**
  1307. * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
  1308. * @id: IDENTIFY data to compute xfer mask from
  1309. *
  1310. * Compute the xfermask for this device. This is not as trivial
  1311. * as it seems if we must consider early devices correctly.
  1312. *
  1313. * FIXME: pre IDE drive timing (do we care ?).
  1314. *
  1315. * LOCKING:
  1316. * None.
  1317. *
  1318. * RETURNS:
  1319. * Computed xfermask
  1320. */
  1321. unsigned long ata_id_xfermask(const u16 *id)
  1322. {
  1323. unsigned long pio_mask, mwdma_mask, udma_mask;
  1324. /* Usual case. Word 53 indicates word 64 is valid */
  1325. if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
  1326. pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
  1327. pio_mask <<= 3;
  1328. pio_mask |= 0x7;
  1329. } else {
  1330. /* If word 64 isn't valid then Word 51 high byte holds
  1331. * the PIO timing number for the maximum. Turn it into
  1332. * a mask.
  1333. */
  1334. u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
  1335. if (mode < 5) /* Valid PIO range */
  1336. pio_mask = (2 << mode) - 1;
  1337. else
  1338. pio_mask = 1;
  1339. /* But wait.. there's more. Design your standards by
  1340. * committee and you too can get a free iordy field to
  1341. * process. However its the speeds not the modes that
  1342. * are supported... Note drivers using the timing API
  1343. * will get this right anyway
  1344. */
  1345. }
  1346. mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
  1347. if (ata_id_is_cfa(id)) {
  1348. /*
  1349. * Process compact flash extended modes
  1350. */
  1351. int pio = id[163] & 0x7;
  1352. int dma = (id[163] >> 3) & 7;
  1353. if (pio)
  1354. pio_mask |= (1 << 5);
  1355. if (pio > 1)
  1356. pio_mask |= (1 << 6);
  1357. if (dma)
  1358. mwdma_mask |= (1 << 3);
  1359. if (dma > 1)
  1360. mwdma_mask |= (1 << 4);
  1361. }
  1362. udma_mask = 0;
  1363. if (id[ATA_ID_FIELD_VALID] & (1 << 2))
  1364. udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
  1365. return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
  1366. }
  1367. /**
  1368. * ata_pio_queue_task - Queue port_task
  1369. * @ap: The ata_port to queue port_task for
  1370. * @fn: workqueue function to be scheduled
  1371. * @data: data for @fn to use
  1372. * @delay: delay time for workqueue function
  1373. *
  1374. * Schedule @fn(@data) for execution after @delay jiffies using
  1375. * port_task. There is one port_task per port and it's the
  1376. * user(low level driver)'s responsibility to make sure that only
  1377. * one task is active at any given time.
  1378. *
  1379. * libata core layer takes care of synchronization between
  1380. * port_task and EH. ata_pio_queue_task() may be ignored for EH
  1381. * synchronization.
  1382. *
  1383. * LOCKING:
  1384. * Inherited from caller.
  1385. */
  1386. void ata_pio_queue_task(struct ata_port *ap, void *data, unsigned long delay)
  1387. {
  1388. ap->port_task_data = data;
  1389. /* may fail if ata_port_flush_task() in progress */
  1390. queue_delayed_work(ata_wq, &ap->port_task, delay);
  1391. }
  1392. /**
  1393. * ata_port_flush_task - Flush port_task
  1394. * @ap: The ata_port to flush port_task for
  1395. *
  1396. * After this function completes, port_task is guranteed not to
  1397. * be running or scheduled.
  1398. *
  1399. * LOCKING:
  1400. * Kernel thread context (may sleep)
  1401. */
  1402. void ata_port_flush_task(struct ata_port *ap)
  1403. {
  1404. DPRINTK("ENTER\n");
  1405. cancel_rearming_delayed_work(&ap->port_task);
  1406. if (ata_msg_ctl(ap))
  1407. ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __func__);
  1408. }
  1409. static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
  1410. {
  1411. struct completion *waiting = qc->private_data;
  1412. complete(waiting);
  1413. }
  1414. /**
  1415. * ata_exec_internal_sg - execute libata internal command
  1416. * @dev: Device to which the command is sent
  1417. * @tf: Taskfile registers for the command and the result
  1418. * @cdb: CDB for packet command
  1419. * @dma_dir: Data tranfer direction of the command
  1420. * @sgl: sg list for the data buffer of the command
  1421. * @n_elem: Number of sg entries
  1422. * @timeout: Timeout in msecs (0 for default)
  1423. *
  1424. * Executes libata internal command with timeout. @tf contains
  1425. * command on entry and result on return. Timeout and error
  1426. * conditions are reported via return value. No recovery action
  1427. * is taken after a command times out. It's caller's duty to
  1428. * clean up after timeout.
  1429. *
  1430. * LOCKING:
  1431. * None. Should be called with kernel context, might sleep.
  1432. *
  1433. * RETURNS:
  1434. * Zero on success, AC_ERR_* mask on failure
  1435. */
  1436. unsigned ata_exec_internal_sg(struct ata_device *dev,
  1437. struct ata_taskfile *tf, const u8 *cdb,
  1438. int dma_dir, struct scatterlist *sgl,
  1439. unsigned int n_elem, unsigned long timeout)
  1440. {
  1441. struct ata_link *link = dev->link;
  1442. struct ata_port *ap = link->ap;
  1443. u8 command = tf->command;
  1444. struct ata_queued_cmd *qc;
  1445. unsigned int tag, preempted_tag;
  1446. u32 preempted_sactive, preempted_qc_active;
  1447. int preempted_nr_active_links;
  1448. DECLARE_COMPLETION_ONSTACK(wait);
  1449. unsigned long flags;
  1450. unsigned int err_mask;
  1451. int rc;
  1452. spin_lock_irqsave(ap->lock, flags);
  1453. /* no internal command while frozen */
  1454. if (ap->pflags & ATA_PFLAG_FROZEN) {
  1455. spin_unlock_irqrestore(ap->lock, flags);
  1456. return AC_ERR_SYSTEM;
  1457. }
  1458. /* initialize internal qc */
  1459. /* XXX: Tag 0 is used for drivers with legacy EH as some
  1460. * drivers choke if any other tag is given. This breaks
  1461. * ata_tag_internal() test for those drivers. Don't use new
  1462. * EH stuff without converting to it.
  1463. */
  1464. if (ap->ops->error_handler)
  1465. tag = ATA_TAG_INTERNAL;
  1466. else
  1467. tag = 0;
  1468. if (test_and_set_bit(tag, &ap->qc_allocated))
  1469. BUG();
  1470. qc = __ata_qc_from_tag(ap, tag);
  1471. qc->tag = tag;
  1472. qc->scsicmd = NULL;
  1473. qc->ap = ap;
  1474. qc->dev = dev;
  1475. ata_qc_reinit(qc);
  1476. preempted_tag = link->active_tag;
  1477. preempted_sactive = link->sactive;
  1478. preempted_qc_active = ap->qc_active;
  1479. preempted_nr_active_links = ap->nr_active_links;
  1480. link->active_tag = ATA_TAG_POISON;
  1481. link->sactive = 0;
  1482. ap->qc_active = 0;
  1483. ap->nr_active_links = 0;
  1484. /* prepare & issue qc */
  1485. qc->tf = *tf;
  1486. if (cdb)
  1487. memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
  1488. qc->flags |= ATA_QCFLAG_RESULT_TF;
  1489. qc->dma_dir = dma_dir;
  1490. if (dma_dir != DMA_NONE) {
  1491. unsigned int i, buflen = 0;
  1492. struct scatterlist *sg;
  1493. for_each_sg(sgl, sg, n_elem, i)
  1494. buflen += sg->length;
  1495. ata_sg_init(qc, sgl, n_elem);
  1496. qc->nbytes = buflen;
  1497. }
  1498. qc->private_data = &wait;
  1499. qc->complete_fn = ata_qc_complete_internal;
  1500. ata_qc_issue(qc);
  1501. spin_unlock_irqrestore(ap->lock, flags);
  1502. if (!timeout)
  1503. timeout = ata_probe_timeout * 1000 / HZ;
  1504. rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
  1505. ata_port_flush_task(ap);
  1506. if (!rc) {
  1507. spin_lock_irqsave(ap->lock, flags);
  1508. /* We're racing with irq here. If we lose, the
  1509. * following test prevents us from completing the qc
  1510. * twice. If we win, the port is frozen and will be
  1511. * cleaned up by ->post_internal_cmd().
  1512. */
  1513. if (qc->flags & ATA_QCFLAG_ACTIVE) {
  1514. qc->err_mask |= AC_ERR_TIMEOUT;
  1515. if (ap->ops->error_handler)
  1516. ata_port_freeze(ap);
  1517. else
  1518. ata_qc_complete(qc);
  1519. if (ata_msg_warn(ap))
  1520. ata_dev_printk(dev, KERN_WARNING,
  1521. "qc timeout (cmd 0x%x)\n", command);
  1522. }
  1523. spin_unlock_irqrestore(ap->lock, flags);
  1524. }
  1525. /* do post_internal_cmd */
  1526. if (ap->ops->post_internal_cmd)
  1527. ap->ops->post_internal_cmd(qc);
  1528. /* perform minimal error analysis */
  1529. if (qc->flags & ATA_QCFLAG_FAILED) {
  1530. if (qc->result_tf.command & (ATA_ERR | ATA_DF))
  1531. qc->err_mask |= AC_ERR_DEV;
  1532. if (!qc->err_mask)
  1533. qc->err_mask |= AC_ERR_OTHER;
  1534. if (qc->err_mask & ~AC_ERR_OTHER)
  1535. qc->err_mask &= ~AC_ERR_OTHER;
  1536. }
  1537. /* finish up */
  1538. spin_lock_irqsave(ap->lock, flags);
  1539. *tf = qc->result_tf;
  1540. err_mask = qc->err_mask;
  1541. ata_qc_free(qc);
  1542. link->active_tag = preempted_tag;
  1543. link->sactive = preempted_sactive;
  1544. ap->qc_active = preempted_qc_active;
  1545. ap->nr_active_links = preempted_nr_active_links;
  1546. /* XXX - Some LLDDs (sata_mv) disable port on command failure.
  1547. * Until those drivers are fixed, we detect the condition
  1548. * here, fail the command with AC_ERR_SYSTEM and reenable the
  1549. * port.
  1550. *
  1551. * Note that this doesn't change any behavior as internal
  1552. * command failure results in disabling the device in the
  1553. * higher layer for LLDDs without new reset/EH callbacks.
  1554. *
  1555. * Kill the following code as soon as those drivers are fixed.
  1556. */
  1557. if (ap->flags & ATA_FLAG_DISABLED) {
  1558. err_mask |= AC_ERR_SYSTEM;
  1559. ata_port_probe(ap);
  1560. }
  1561. spin_unlock_irqrestore(ap->lock, flags);
  1562. return err_mask;
  1563. }
  1564. /**
  1565. * ata_exec_internal - execute libata internal command
  1566. * @dev: Device to which the command is sent
  1567. * @tf: Taskfile registers for the command and the result
  1568. * @cdb: CDB for packet command
  1569. * @dma_dir: Data tranfer direction of the command
  1570. * @buf: Data buffer of the command
  1571. * @buflen: Length of data buffer
  1572. * @timeout: Timeout in msecs (0 for default)
  1573. *
  1574. * Wrapper around ata_exec_internal_sg() which takes simple
  1575. * buffer instead of sg list.
  1576. *
  1577. * LOCKING:
  1578. * None. Should be called with kernel context, might sleep.
  1579. *
  1580. * RETURNS:
  1581. * Zero on success, AC_ERR_* mask on failure
  1582. */
  1583. unsigned ata_exec_internal(struct ata_device *dev,
  1584. struct ata_taskfile *tf, const u8 *cdb,
  1585. int dma_dir, void *buf, unsigned int buflen,
  1586. unsigned long timeout)
  1587. {
  1588. struct scatterlist *psg = NULL, sg;
  1589. unsigned int n_elem = 0;
  1590. if (dma_dir != DMA_NONE) {
  1591. WARN_ON(!buf);
  1592. sg_init_one(&sg, buf, buflen);
  1593. psg = &sg;
  1594. n_elem++;
  1595. }
  1596. return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
  1597. timeout);
  1598. }
  1599. /**
  1600. * ata_do_simple_cmd - execute simple internal command
  1601. * @dev: Device to which the command is sent
  1602. * @cmd: Opcode to execute
  1603. *
  1604. * Execute a 'simple' command, that only consists of the opcode
  1605. * 'cmd' itself, without filling any other registers
  1606. *
  1607. * LOCKING:
  1608. * Kernel thread context (may sleep).
  1609. *
  1610. * RETURNS:
  1611. * Zero on success, AC_ERR_* mask on failure
  1612. */
  1613. unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
  1614. {
  1615. struct ata_taskfile tf;
  1616. ata_tf_init(dev, &tf);
  1617. tf.command = cmd;
  1618. tf.flags |= ATA_TFLAG_DEVICE;
  1619. tf.protocol = ATA_PROT_NODATA;
  1620. return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  1621. }
  1622. /**
  1623. * ata_pio_need_iordy - check if iordy needed
  1624. * @adev: ATA device
  1625. *
  1626. * Check if the current speed of the device requires IORDY. Used
  1627. * by various controllers for chip configuration.
  1628. */
  1629. unsigned int ata_pio_need_iordy(const struct ata_device *adev)
  1630. {
  1631. /* Controller doesn't support IORDY. Probably a pointless check
  1632. as the caller should know this */
  1633. if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
  1634. return 0;
  1635. /* PIO3 and higher it is mandatory */
  1636. if (adev->pio_mode > XFER_PIO_2)
  1637. return 1;
  1638. /* We turn it on when possible */
  1639. if (ata_id_has_iordy(adev->id))
  1640. return 1;
  1641. return 0;
  1642. }
  1643. /**
  1644. * ata_pio_mask_no_iordy - Return the non IORDY mask
  1645. * @adev: ATA device
  1646. *
  1647. * Compute the highest mode possible if we are not using iordy. Return
  1648. * -1 if no iordy mode is available.
  1649. */
  1650. static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
  1651. {
  1652. /* If we have no drive specific rule, then PIO 2 is non IORDY */
  1653. if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
  1654. u16 pio = adev->id[ATA_ID_EIDE_PIO];
  1655. /* Is the speed faster than the drive allows non IORDY ? */
  1656. if (pio) {
  1657. /* This is cycle times not frequency - watch the logic! */
  1658. if (pio > 240) /* PIO2 is 240nS per cycle */
  1659. return 3 << ATA_SHIFT_PIO;
  1660. return 7 << ATA_SHIFT_PIO;
  1661. }
  1662. }
  1663. return 3 << ATA_SHIFT_PIO;
  1664. }
  1665. /**
  1666. * ata_dev_read_id - Read ID data from the specified device
  1667. * @dev: target device
  1668. * @p_class: pointer to class of the target device (may be changed)
  1669. * @flags: ATA_READID_* flags
  1670. * @id: buffer to read IDENTIFY data into
  1671. *
  1672. * Read ID data from the specified device. ATA_CMD_ID_ATA is
  1673. * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
  1674. * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
  1675. * for pre-ATA4 drives.
  1676. *
  1677. * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
  1678. * now we abort if we hit that case.
  1679. *
  1680. * LOCKING:
  1681. * Kernel thread context (may sleep)
  1682. *
  1683. * RETURNS:
  1684. * 0 on success, -errno otherwise.
  1685. */
  1686. int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
  1687. unsigned int flags, u16 *id)
  1688. {
  1689. struct ata_port *ap = dev->link->ap;
  1690. unsigned int class = *p_class;
  1691. struct ata_taskfile tf;
  1692. unsigned int err_mask = 0;
  1693. const char *reason;
  1694. int may_fallback = 1, tried_spinup = 0;
  1695. int rc;
  1696. if (ata_msg_ctl(ap))
  1697. ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
  1698. retry:
  1699. ata_tf_init(dev, &tf);
  1700. switch (class) {
  1701. case ATA_DEV_ATA:
  1702. tf.command = ATA_CMD_ID_ATA;
  1703. break;
  1704. case ATA_DEV_ATAPI:
  1705. tf.command = ATA_CMD_ID_ATAPI;
  1706. break;
  1707. default:
  1708. rc = -ENODEV;
  1709. reason = "unsupported class";
  1710. goto err_out;
  1711. }
  1712. tf.protocol = ATA_PROT_PIO;
  1713. /* Some devices choke if TF registers contain garbage. Make
  1714. * sure those are properly initialized.
  1715. */
  1716. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1717. /* Device presence detection is unreliable on some
  1718. * controllers. Always poll IDENTIFY if available.
  1719. */
  1720. tf.flags |= ATA_TFLAG_POLLING;
  1721. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
  1722. id, sizeof(id[0]) * ATA_ID_WORDS, 0);
  1723. if (err_mask) {
  1724. if (err_mask & AC_ERR_NODEV_HINT) {
  1725. ata_dev_printk(dev, KERN_DEBUG,
  1726. "NODEV after polling detection\n");
  1727. return -ENOENT;
  1728. }
  1729. if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
  1730. /* Device or controller might have reported
  1731. * the wrong device class. Give a shot at the
  1732. * other IDENTIFY if the current one is
  1733. * aborted by the device.
  1734. */
  1735. if (may_fallback) {
  1736. may_fallback = 0;
  1737. if (class == ATA_DEV_ATA)
  1738. class = ATA_DEV_ATAPI;
  1739. else
  1740. class = ATA_DEV_ATA;
  1741. goto retry;
  1742. }
  1743. /* Control reaches here iff the device aborted
  1744. * both flavors of IDENTIFYs which happens
  1745. * sometimes with phantom devices.
  1746. */
  1747. ata_dev_printk(dev, KERN_DEBUG,
  1748. "both IDENTIFYs aborted, assuming NODEV\n");
  1749. return -ENOENT;
  1750. }
  1751. rc = -EIO;
  1752. reason = "I/O error";
  1753. goto err_out;
  1754. }
  1755. /* Falling back doesn't make sense if ID data was read
  1756. * successfully at least once.
  1757. */
  1758. may_fallback = 0;
  1759. swap_buf_le16(id, ATA_ID_WORDS);
  1760. /* sanity check */
  1761. rc = -EINVAL;
  1762. reason = "device reports invalid type";
  1763. if (class == ATA_DEV_ATA) {
  1764. if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
  1765. goto err_out;
  1766. } else {
  1767. if (ata_id_is_ata(id))
  1768. goto err_out;
  1769. }
  1770. if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
  1771. tried_spinup = 1;
  1772. /*
  1773. * Drive powered-up in standby mode, and requires a specific
  1774. * SET_FEATURES spin-up subcommand before it will accept
  1775. * anything other than the original IDENTIFY command.
  1776. */
  1777. err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
  1778. if (err_mask && id[2] != 0x738c) {
  1779. rc = -EIO;
  1780. reason = "SPINUP failed";
  1781. goto err_out;
  1782. }
  1783. /*
  1784. * If the drive initially returned incomplete IDENTIFY info,
  1785. * we now must reissue the IDENTIFY command.
  1786. */
  1787. if (id[2] == 0x37c8)
  1788. goto retry;
  1789. }
  1790. if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
  1791. /*
  1792. * The exact sequence expected by certain pre-ATA4 drives is:
  1793. * SRST RESET
  1794. * IDENTIFY (optional in early ATA)
  1795. * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
  1796. * anything else..
  1797. * Some drives were very specific about that exact sequence.
  1798. *
  1799. * Note that ATA4 says lba is mandatory so the second check
  1800. * shoud never trigger.
  1801. */
  1802. if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
  1803. err_mask = ata_dev_init_params(dev, id[3], id[6]);
  1804. if (err_mask) {
  1805. rc = -EIO;
  1806. reason = "INIT_DEV_PARAMS failed";
  1807. goto err_out;
  1808. }
  1809. /* current CHS translation info (id[53-58]) might be
  1810. * changed. reread the identify device info.
  1811. */
  1812. flags &= ~ATA_READID_POSTRESET;
  1813. goto retry;
  1814. }
  1815. }
  1816. *p_class = class;
  1817. return 0;
  1818. err_out:
  1819. if (ata_msg_warn(ap))
  1820. ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
  1821. "(%s, err_mask=0x%x)\n", reason, err_mask);
  1822. return rc;
  1823. }
  1824. static inline u8 ata_dev_knobble(struct ata_device *dev)
  1825. {
  1826. struct ata_port *ap = dev->link->ap;
  1827. return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
  1828. }
  1829. static void ata_dev_config_ncq(struct ata_device *dev,
  1830. char *desc, size_t desc_sz)
  1831. {
  1832. struct ata_port *ap = dev->link->ap;
  1833. int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
  1834. if (!ata_id_has_ncq(dev->id)) {
  1835. desc[0] = '\0';
  1836. return;
  1837. }
  1838. if (dev->horkage & ATA_HORKAGE_NONCQ) {
  1839. snprintf(desc, desc_sz, "NCQ (not used)");
  1840. return;
  1841. }
  1842. if (ap->flags & ATA_FLAG_NCQ) {
  1843. hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
  1844. dev->flags |= ATA_DFLAG_NCQ;
  1845. }
  1846. if (hdepth >= ddepth)
  1847. snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
  1848. else
  1849. snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
  1850. }
  1851. /**
  1852. * ata_dev_configure - Configure the specified ATA/ATAPI device
  1853. * @dev: Target device to configure
  1854. *
  1855. * Configure @dev according to @dev->id. Generic and low-level
  1856. * driver specific fixups are also applied.
  1857. *
  1858. * LOCKING:
  1859. * Kernel thread context (may sleep)
  1860. *
  1861. * RETURNS:
  1862. * 0 on success, -errno otherwise
  1863. */
  1864. int ata_dev_configure(struct ata_device *dev)
  1865. {
  1866. struct ata_port *ap = dev->link->ap;
  1867. struct ata_eh_context *ehc = &dev->link->eh_context;
  1868. int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
  1869. const u16 *id = dev->id;
  1870. unsigned long xfer_mask;
  1871. char revbuf[7]; /* XYZ-99\0 */
  1872. char fwrevbuf[ATA_ID_FW_REV_LEN+1];
  1873. char modelbuf[ATA_ID_PROD_LEN+1];
  1874. int rc;
  1875. if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
  1876. ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
  1877. __func__);
  1878. return 0;
  1879. }
  1880. if (ata_msg_probe(ap))
  1881. ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
  1882. /* set horkage */
  1883. dev->horkage |= ata_dev_blacklisted(dev);
  1884. ata_force_horkage(dev);
  1885. /* let ACPI work its magic */
  1886. rc = ata_acpi_on_devcfg(dev);
  1887. if (rc)
  1888. return rc;
  1889. /* massage HPA, do it early as it might change IDENTIFY data */
  1890. rc = ata_hpa_resize(dev);
  1891. if (rc)
  1892. return rc;
  1893. /* print device capabilities */
  1894. if (ata_msg_probe(ap))
  1895. ata_dev_printk(dev, KERN_DEBUG,
  1896. "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
  1897. "85:%04x 86:%04x 87:%04x 88:%04x\n",
  1898. __func__,
  1899. id[49], id[82], id[83], id[84],
  1900. id[85], id[86], id[87], id[88]);
  1901. /* initialize to-be-configured parameters */
  1902. dev->flags &= ~ATA_DFLAG_CFG_MASK;
  1903. dev->max_sectors = 0;
  1904. dev->cdb_len = 0;
  1905. dev->n_sectors = 0;
  1906. dev->cylinders = 0;
  1907. dev->heads = 0;
  1908. dev->sectors = 0;
  1909. /*
  1910. * common ATA, ATAPI feature tests
  1911. */
  1912. /* find max transfer mode; for printk only */
  1913. xfer_mask = ata_id_xfermask(id);
  1914. if (ata_msg_probe(ap))
  1915. ata_dump_id(id);
  1916. /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
  1917. ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
  1918. sizeof(fwrevbuf));
  1919. ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
  1920. sizeof(modelbuf));
  1921. /* ATA-specific feature tests */
  1922. if (dev->class == ATA_DEV_ATA) {
  1923. if (ata_id_is_cfa(id)) {
  1924. if (id[162] & 1) /* CPRM may make this media unusable */
  1925. ata_dev_printk(dev, KERN_WARNING,
  1926. "supports DRM functions and may "
  1927. "not be fully accessable.\n");
  1928. snprintf(revbuf, 7, "CFA");
  1929. } else {
  1930. snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
  1931. /* Warn the user if the device has TPM extensions */
  1932. if (ata_id_has_tpm(id))
  1933. ata_dev_printk(dev, KERN_WARNING,
  1934. "supports DRM functions and may "
  1935. "not be fully accessable.\n");
  1936. }
  1937. dev->n_sectors = ata_id_n_sectors(id);
  1938. if (dev->id[59] & 0x100)
  1939. dev->multi_count = dev->id[59] & 0xff;
  1940. if (ata_id_has_lba(id)) {
  1941. const char *lba_desc;
  1942. char ncq_desc[20];
  1943. lba_desc = "LBA";
  1944. dev->flags |= ATA_DFLAG_LBA;
  1945. if (ata_id_has_lba48(id)) {
  1946. dev->flags |= ATA_DFLAG_LBA48;
  1947. lba_desc = "LBA48";
  1948. if (dev->n_sectors >= (1UL << 28) &&
  1949. ata_id_has_flush_ext(id))
  1950. dev->flags |= ATA_DFLAG_FLUSH_EXT;
  1951. }
  1952. /* config NCQ */
  1953. ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
  1954. /* print device info to dmesg */
  1955. if (ata_msg_drv(ap) && print_info) {
  1956. ata_dev_printk(dev, KERN_INFO,
  1957. "%s: %s, %s, max %s\n",
  1958. revbuf, modelbuf, fwrevbuf,
  1959. ata_mode_string(xfer_mask));
  1960. ata_dev_printk(dev, KERN_INFO,
  1961. "%Lu sectors, multi %u: %s %s\n",
  1962. (unsigned long long)dev->n_sectors,
  1963. dev->multi_count, lba_desc, ncq_desc);
  1964. }
  1965. } else {
  1966. /* CHS */
  1967. /* Default translation */
  1968. dev->cylinders = id[1];
  1969. dev->heads = id[3];
  1970. dev->sectors = id[6];
  1971. if (ata_id_current_chs_valid(id)) {
  1972. /* Current CHS translation is valid. */
  1973. dev->cylinders = id[54];
  1974. dev->heads = id[55];
  1975. dev->sectors = id[56];
  1976. }
  1977. /* print device info to dmesg */
  1978. if (ata_msg_drv(ap) && print_info) {
  1979. ata_dev_printk(dev, KERN_INFO,
  1980. "%s: %s, %s, max %s\n",
  1981. revbuf, modelbuf, fwrevbuf,
  1982. ata_mode_string(xfer_mask));
  1983. ata_dev_printk(dev, KERN_INFO,
  1984. "%Lu sectors, multi %u, CHS %u/%u/%u\n",
  1985. (unsigned long long)dev->n_sectors,
  1986. dev->multi_count, dev->cylinders,
  1987. dev->heads, dev->sectors);
  1988. }
  1989. }
  1990. dev->cdb_len = 16;
  1991. }
  1992. /* ATAPI-specific feature tests */
  1993. else if (dev->class == ATA_DEV_ATAPI) {
  1994. const char *cdb_intr_string = "";
  1995. const char *atapi_an_string = "";
  1996. const char *dma_dir_string = "";
  1997. u32 sntf;
  1998. rc = atapi_cdb_len(id);
  1999. if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
  2000. if (ata_msg_warn(ap))
  2001. ata_dev_printk(dev, KERN_WARNING,
  2002. "unsupported CDB len\n");
  2003. rc = -EINVAL;
  2004. goto err_out_nosup;
  2005. }
  2006. dev->cdb_len = (unsigned int) rc;
  2007. /* Enable ATAPI AN if both the host and device have
  2008. * the support. If PMP is attached, SNTF is required
  2009. * to enable ATAPI AN to discern between PHY status
  2010. * changed notifications and ATAPI ANs.
  2011. */
  2012. if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
  2013. (!ap->nr_pmp_links ||
  2014. sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
  2015. unsigned int err_mask;
  2016. /* issue SET feature command to turn this on */
  2017. err_mask = ata_dev_set_feature(dev,
  2018. SETFEATURES_SATA_ENABLE, SATA_AN);
  2019. if (err_mask)
  2020. ata_dev_printk(dev, KERN_ERR,
  2021. "failed to enable ATAPI AN "
  2022. "(err_mask=0x%x)\n", err_mask);
  2023. else {
  2024. dev->flags |= ATA_DFLAG_AN;
  2025. atapi_an_string = ", ATAPI AN";
  2026. }
  2027. }
  2028. if (ata_id_cdb_intr(dev->id)) {
  2029. dev->flags |= ATA_DFLAG_CDB_INTR;
  2030. cdb_intr_string = ", CDB intr";
  2031. }
  2032. if (atapi_dmadir || atapi_id_dmadir(dev->id)) {
  2033. dev->flags |= ATA_DFLAG_DMADIR;
  2034. dma_dir_string = ", DMADIR";
  2035. }
  2036. /* print device info to dmesg */
  2037. if (ata_msg_drv(ap) && print_info)
  2038. ata_dev_printk(dev, KERN_INFO,
  2039. "ATAPI: %s, %s, max %s%s%s%s\n",
  2040. modelbuf, fwrevbuf,
  2041. ata_mode_string(xfer_mask),
  2042. cdb_intr_string, atapi_an_string,
  2043. dma_dir_string);
  2044. }
  2045. /* determine max_sectors */
  2046. dev->max_sectors = ATA_MAX_SECTORS;
  2047. if (dev->flags & ATA_DFLAG_LBA48)
  2048. dev->max_sectors = ATA_MAX_SECTORS_LBA48;
  2049. if (!(dev->horkage & ATA_HORKAGE_IPM)) {
  2050. if (ata_id_has_hipm(dev->id))
  2051. dev->flags |= ATA_DFLAG_HIPM;
  2052. if (ata_id_has_dipm(dev->id))
  2053. dev->flags |= ATA_DFLAG_DIPM;
  2054. }
  2055. /* Limit PATA drive on SATA cable bridge transfers to udma5,
  2056. 200 sectors */
  2057. if (ata_dev_knobble(dev)) {
  2058. if (ata_msg_drv(ap) && print_info)
  2059. ata_dev_printk(dev, KERN_INFO,
  2060. "applying bridge limits\n");
  2061. dev->udma_mask &= ATA_UDMA5;
  2062. dev->max_sectors = ATA_MAX_SECTORS;
  2063. }
  2064. if ((dev->class == ATA_DEV_ATAPI) &&
  2065. (atapi_command_packet_set(id) == TYPE_TAPE)) {
  2066. dev->max_sectors = ATA_MAX_SECTORS_TAPE;
  2067. dev->horkage |= ATA_HORKAGE_STUCK_ERR;
  2068. }
  2069. if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
  2070. dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
  2071. dev->max_sectors);
  2072. if (ata_dev_blacklisted(dev) & ATA_HORKAGE_IPM) {
  2073. dev->horkage |= ATA_HORKAGE_IPM;
  2074. /* reset link pm_policy for this port to no pm */
  2075. ap->pm_policy = MAX_PERFORMANCE;
  2076. }
  2077. if (ap->ops->dev_config)
  2078. ap->ops->dev_config(dev);
  2079. if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
  2080. /* Let the user know. We don't want to disallow opens for
  2081. rescue purposes, or in case the vendor is just a blithering
  2082. idiot. Do this after the dev_config call as some controllers
  2083. with buggy firmware may want to avoid reporting false device
  2084. bugs */
  2085. if (print_info) {
  2086. ata_dev_printk(dev, KERN_WARNING,
  2087. "Drive reports diagnostics failure. This may indicate a drive\n");
  2088. ata_dev_printk(dev, KERN_WARNING,
  2089. "fault or invalid emulation. Contact drive vendor for information.\n");
  2090. }
  2091. }
  2092. return 0;
  2093. err_out_nosup:
  2094. if (ata_msg_probe(ap))
  2095. ata_dev_printk(dev, KERN_DEBUG,
  2096. "%s: EXIT, err\n", __func__);
  2097. return rc;
  2098. }
  2099. /**
  2100. * ata_cable_40wire - return 40 wire cable type
  2101. * @ap: port
  2102. *
  2103. * Helper method for drivers which want to hardwire 40 wire cable
  2104. * detection.
  2105. */
  2106. int ata_cable_40wire(struct ata_port *ap)
  2107. {
  2108. return ATA_CBL_PATA40;
  2109. }
  2110. /**
  2111. * ata_cable_80wire - return 80 wire cable type
  2112. * @ap: port
  2113. *
  2114. * Helper method for drivers which want to hardwire 80 wire cable
  2115. * detection.
  2116. */
  2117. int ata_cable_80wire(struct ata_port *ap)
  2118. {
  2119. return ATA_CBL_PATA80;
  2120. }
  2121. /**
  2122. * ata_cable_unknown - return unknown PATA cable.
  2123. * @ap: port
  2124. *
  2125. * Helper method for drivers which have no PATA cable detection.
  2126. */
  2127. int ata_cable_unknown(struct ata_port *ap)
  2128. {
  2129. return ATA_CBL_PATA_UNK;
  2130. }
  2131. /**
  2132. * ata_cable_ignore - return ignored PATA cable.
  2133. * @ap: port
  2134. *
  2135. * Helper method for drivers which don't use cable type to limit
  2136. * transfer mode.
  2137. */
  2138. int ata_cable_ignore(struct ata_port *ap)
  2139. {
  2140. return ATA_CBL_PATA_IGN;
  2141. }
  2142. /**
  2143. * ata_cable_sata - return SATA cable type
  2144. * @ap: port
  2145. *
  2146. * Helper method for drivers which have SATA cables
  2147. */
  2148. int ata_cable_sata(struct ata_port *ap)
  2149. {
  2150. return ATA_CBL_SATA;
  2151. }
  2152. /**
  2153. * ata_bus_probe - Reset and probe ATA bus
  2154. * @ap: Bus to probe
  2155. *
  2156. * Master ATA bus probing function. Initiates a hardware-dependent
  2157. * bus reset, then attempts to identify any devices found on
  2158. * the bus.
  2159. *
  2160. * LOCKING:
  2161. * PCI/etc. bus probe sem.
  2162. *
  2163. * RETURNS:
  2164. * Zero on success, negative errno otherwise.
  2165. */
  2166. int ata_bus_probe(struct ata_port *ap)
  2167. {
  2168. unsigned int classes[ATA_MAX_DEVICES];
  2169. int tries[ATA_MAX_DEVICES];
  2170. int rc;
  2171. struct ata_device *dev;
  2172. ata_port_probe(ap);
  2173. ata_link_for_each_dev(dev, &ap->link)
  2174. tries[dev->devno] = ATA_PROBE_MAX_TRIES;
  2175. retry:
  2176. ata_link_for_each_dev(dev, &ap->link) {
  2177. /* If we issue an SRST then an ATA drive (not ATAPI)
  2178. * may change configuration and be in PIO0 timing. If
  2179. * we do a hard reset (or are coming from power on)
  2180. * this is true for ATA or ATAPI. Until we've set a
  2181. * suitable controller mode we should not touch the
  2182. * bus as we may be talking too fast.
  2183. */
  2184. dev->pio_mode = XFER_PIO_0;
  2185. /* If the controller has a pio mode setup function
  2186. * then use it to set the chipset to rights. Don't
  2187. * touch the DMA setup as that will be dealt with when
  2188. * configuring devices.
  2189. */
  2190. if (ap->ops->set_piomode)
  2191. ap->ops->set_piomode(ap, dev);
  2192. }
  2193. /* reset and determine device classes */
  2194. ap->ops->phy_reset(ap);
  2195. ata_link_for_each_dev(dev, &ap->link) {
  2196. if (!(ap->flags & ATA_FLAG_DISABLED) &&
  2197. dev->class != ATA_DEV_UNKNOWN)
  2198. classes[dev->devno] = dev->class;
  2199. else
  2200. classes[dev->devno] = ATA_DEV_NONE;
  2201. dev->class = ATA_DEV_UNKNOWN;
  2202. }
  2203. ata_port_probe(ap);
  2204. /* read IDENTIFY page and configure devices. We have to do the identify
  2205. specific sequence bass-ackwards so that PDIAG- is released by
  2206. the slave device */
  2207. ata_link_for_each_dev_reverse(dev, &ap->link) {
  2208. if (tries[dev->devno])
  2209. dev->class = classes[dev->devno];
  2210. if (!ata_dev_enabled(dev))
  2211. continue;
  2212. rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
  2213. dev->id);
  2214. if (rc)
  2215. goto fail;
  2216. }
  2217. /* Now ask for the cable type as PDIAG- should have been released */
  2218. if (ap->ops->cable_detect)
  2219. ap->cbl = ap->ops->cable_detect(ap);
  2220. /* We may have SATA bridge glue hiding here irrespective of the
  2221. reported cable types and sensed types */
  2222. ata_link_for_each_dev(dev, &ap->link) {
  2223. if (!ata_dev_enabled(dev))
  2224. continue;
  2225. /* SATA drives indicate we have a bridge. We don't know which
  2226. end of the link the bridge is which is a problem */
  2227. if (ata_id_is_sata(dev->id))
  2228. ap->cbl = ATA_CBL_SATA;
  2229. }
  2230. /* After the identify sequence we can now set up the devices. We do
  2231. this in the normal order so that the user doesn't get confused */
  2232. ata_link_for_each_dev(dev, &ap->link) {
  2233. if (!ata_dev_enabled(dev))
  2234. continue;
  2235. ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
  2236. rc = ata_dev_configure(dev);
  2237. ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
  2238. if (rc)
  2239. goto fail;
  2240. }
  2241. /* configure transfer mode */
  2242. rc = ata_set_mode(&ap->link, &dev);
  2243. if (rc)
  2244. goto fail;
  2245. ata_link_for_each_dev(dev, &ap->link)
  2246. if (ata_dev_enabled(dev))
  2247. return 0;
  2248. /* no device present, disable port */
  2249. ata_port_disable(ap);
  2250. return -ENODEV;
  2251. fail:
  2252. tries[dev->devno]--;
  2253. switch (rc) {
  2254. case -EINVAL:
  2255. /* eeek, something went very wrong, give up */
  2256. tries[dev->devno] = 0;
  2257. break;
  2258. case -ENODEV:
  2259. /* give it just one more chance */
  2260. tries[dev->devno] = min(tries[dev->devno], 1);
  2261. case -EIO:
  2262. if (tries[dev->devno] == 1) {
  2263. /* This is the last chance, better to slow
  2264. * down than lose it.
  2265. */
  2266. sata_down_spd_limit(&ap->link);
  2267. ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
  2268. }
  2269. }
  2270. if (!tries[dev->devno])
  2271. ata_dev_disable(dev);
  2272. goto retry;
  2273. }
  2274. /**
  2275. * ata_port_probe - Mark port as enabled
  2276. * @ap: Port for which we indicate enablement
  2277. *
  2278. * Modify @ap data structure such that the system
  2279. * thinks that the entire port is enabled.
  2280. *
  2281. * LOCKING: host lock, or some other form of
  2282. * serialization.
  2283. */
  2284. void ata_port_probe(struct ata_port *ap)
  2285. {
  2286. ap->flags &= ~ATA_FLAG_DISABLED;
  2287. }
  2288. /**
  2289. * sata_print_link_status - Print SATA link status
  2290. * @link: SATA link to printk link status about
  2291. *
  2292. * This function prints link speed and status of a SATA link.
  2293. *
  2294. * LOCKING:
  2295. * None.
  2296. */
  2297. void sata_print_link_status(struct ata_link *link)
  2298. {
  2299. u32 sstatus, scontrol, tmp;
  2300. if (sata_scr_read(link, SCR_STATUS, &sstatus))
  2301. return;
  2302. sata_scr_read(link, SCR_CONTROL, &scontrol);
  2303. if (ata_link_online(link)) {
  2304. tmp = (sstatus >> 4) & 0xf;
  2305. ata_link_printk(link, KERN_INFO,
  2306. "SATA link up %s (SStatus %X SControl %X)\n",
  2307. sata_spd_string(tmp), sstatus, scontrol);
  2308. } else {
  2309. ata_link_printk(link, KERN_INFO,
  2310. "SATA link down (SStatus %X SControl %X)\n",
  2311. sstatus, scontrol);
  2312. }
  2313. }
  2314. /**
  2315. * ata_dev_pair - return other device on cable
  2316. * @adev: device
  2317. *
  2318. * Obtain the other device on the same cable, or if none is
  2319. * present NULL is returned
  2320. */
  2321. struct ata_device *ata_dev_pair(struct ata_device *adev)
  2322. {
  2323. struct ata_link *link = adev->link;
  2324. struct ata_device *pair = &link->device[1 - adev->devno];
  2325. if (!ata_dev_enabled(pair))
  2326. return NULL;
  2327. return pair;
  2328. }
  2329. /**
  2330. * ata_port_disable - Disable port.
  2331. * @ap: Port to be disabled.
  2332. *
  2333. * Modify @ap data structure such that the system
  2334. * thinks that the entire port is disabled, and should
  2335. * never attempt to probe or communicate with devices
  2336. * on this port.
  2337. *
  2338. * LOCKING: host lock, or some other form of
  2339. * serialization.
  2340. */
  2341. void ata_port_disable(struct ata_port *ap)
  2342. {
  2343. ap->link.device[0].class = ATA_DEV_NONE;
  2344. ap->link.device[1].class = ATA_DEV_NONE;
  2345. ap->flags |= ATA_FLAG_DISABLED;
  2346. }
  2347. /**
  2348. * sata_down_spd_limit - adjust SATA spd limit downward
  2349. * @link: Link to adjust SATA spd limit for
  2350. *
  2351. * Adjust SATA spd limit of @link downward. Note that this
  2352. * function only adjusts the limit. The change must be applied
  2353. * using sata_set_spd().
  2354. *
  2355. * LOCKING:
  2356. * Inherited from caller.
  2357. *
  2358. * RETURNS:
  2359. * 0 on success, negative errno on failure
  2360. */
  2361. int sata_down_spd_limit(struct ata_link *link)
  2362. {
  2363. u32 sstatus, spd, mask;
  2364. int rc, highbit;
  2365. if (!sata_scr_valid(link))
  2366. return -EOPNOTSUPP;
  2367. /* If SCR can be read, use it to determine the current SPD.
  2368. * If not, use cached value in link->sata_spd.
  2369. */
  2370. rc = sata_scr_read(link, SCR_STATUS, &sstatus);
  2371. if (rc == 0)
  2372. spd = (sstatus >> 4) & 0xf;
  2373. else
  2374. spd = link->sata_spd;
  2375. mask = link->sata_spd_limit;
  2376. if (mask <= 1)
  2377. return -EINVAL;
  2378. /* unconditionally mask off the highest bit */
  2379. highbit = fls(mask) - 1;
  2380. mask &= ~(1 << highbit);
  2381. /* Mask off all speeds higher than or equal to the current
  2382. * one. Force 1.5Gbps if current SPD is not available.
  2383. */
  2384. if (spd > 1)
  2385. mask &= (1 << (spd - 1)) - 1;
  2386. else
  2387. mask &= 1;
  2388. /* were we already at the bottom? */
  2389. if (!mask)
  2390. return -EINVAL;
  2391. link->sata_spd_limit = mask;
  2392. ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
  2393. sata_spd_string(fls(mask)));
  2394. return 0;
  2395. }
  2396. static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
  2397. {
  2398. struct ata_link *host_link = &link->ap->link;
  2399. u32 limit, target, spd;
  2400. limit = link->sata_spd_limit;
  2401. /* Don't configure downstream link faster than upstream link.
  2402. * It doesn't speed up anything and some PMPs choke on such
  2403. * configuration.
  2404. */
  2405. if (!ata_is_host_link(link) && host_link->sata_spd)
  2406. limit &= (1 << host_link->sata_spd) - 1;
  2407. if (limit == UINT_MAX)
  2408. target = 0;
  2409. else
  2410. target = fls(limit);
  2411. spd = (*scontrol >> 4) & 0xf;
  2412. *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
  2413. return spd != target;
  2414. }
  2415. /**
  2416. * sata_set_spd_needed - is SATA spd configuration needed
  2417. * @link: Link in question
  2418. *
  2419. * Test whether the spd limit in SControl matches
  2420. * @link->sata_spd_limit. This function is used to determine
  2421. * whether hardreset is necessary to apply SATA spd
  2422. * configuration.
  2423. *
  2424. * LOCKING:
  2425. * Inherited from caller.
  2426. *
  2427. * RETURNS:
  2428. * 1 if SATA spd configuration is needed, 0 otherwise.
  2429. */
  2430. int sata_set_spd_needed(struct ata_link *link)
  2431. {
  2432. u32 scontrol;
  2433. if (sata_scr_read(link, SCR_CONTROL, &scontrol))
  2434. return 1;
  2435. return __sata_set_spd_needed(link, &scontrol);
  2436. }
  2437. /**
  2438. * sata_set_spd - set SATA spd according to spd limit
  2439. * @link: Link to set SATA spd for
  2440. *
  2441. * Set SATA spd of @link according to sata_spd_limit.
  2442. *
  2443. * LOCKING:
  2444. * Inherited from caller.
  2445. *
  2446. * RETURNS:
  2447. * 0 if spd doesn't need to be changed, 1 if spd has been
  2448. * changed. Negative errno if SCR registers are inaccessible.
  2449. */
  2450. int sata_set_spd(struct ata_link *link)
  2451. {
  2452. u32 scontrol;
  2453. int rc;
  2454. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  2455. return rc;
  2456. if (!__sata_set_spd_needed(link, &scontrol))
  2457. return 0;
  2458. if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
  2459. return rc;
  2460. return 1;
  2461. }
  2462. /*
  2463. * This mode timing computation functionality is ported over from
  2464. * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
  2465. */
  2466. /*
  2467. * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
  2468. * These were taken from ATA/ATAPI-6 standard, rev 0a, except
  2469. * for UDMA6, which is currently supported only by Maxtor drives.
  2470. *
  2471. * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
  2472. */
  2473. static const struct ata_timing ata_timing[] = {
  2474. /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
  2475. { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
  2476. { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
  2477. { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
  2478. { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
  2479. { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
  2480. { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
  2481. { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
  2482. { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
  2483. { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
  2484. { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
  2485. { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
  2486. { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
  2487. { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
  2488. { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
  2489. { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
  2490. /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
  2491. { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
  2492. { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
  2493. { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
  2494. { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
  2495. { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
  2496. { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
  2497. { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
  2498. { 0xFF }
  2499. };
  2500. #define ENOUGH(v, unit) (((v)-1)/(unit)+1)
  2501. #define EZ(v, unit) ((v)?ENOUGH(v, unit):0)
  2502. static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
  2503. {
  2504. q->setup = EZ(t->setup * 1000, T);
  2505. q->act8b = EZ(t->act8b * 1000, T);
  2506. q->rec8b = EZ(t->rec8b * 1000, T);
  2507. q->cyc8b = EZ(t->cyc8b * 1000, T);
  2508. q->active = EZ(t->active * 1000, T);
  2509. q->recover = EZ(t->recover * 1000, T);
  2510. q->cycle = EZ(t->cycle * 1000, T);
  2511. q->udma = EZ(t->udma * 1000, UT);
  2512. }
  2513. void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
  2514. struct ata_timing *m, unsigned int what)
  2515. {
  2516. if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
  2517. if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
  2518. if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
  2519. if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
  2520. if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
  2521. if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
  2522. if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
  2523. if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
  2524. }
  2525. const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
  2526. {
  2527. const struct ata_timing *t = ata_timing;
  2528. while (xfer_mode > t->mode)
  2529. t++;
  2530. if (xfer_mode == t->mode)
  2531. return t;
  2532. return NULL;
  2533. }
  2534. int ata_timing_compute(struct ata_device *adev, unsigned short speed,
  2535. struct ata_timing *t, int T, int UT)
  2536. {
  2537. const struct ata_timing *s;
  2538. struct ata_timing p;
  2539. /*
  2540. * Find the mode.
  2541. */
  2542. if (!(s = ata_timing_find_mode(speed)))
  2543. return -EINVAL;
  2544. memcpy(t, s, sizeof(*s));
  2545. /*
  2546. * If the drive is an EIDE drive, it can tell us it needs extended
  2547. * PIO/MW_DMA cycle timing.
  2548. */
  2549. if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
  2550. memset(&p, 0, sizeof(p));
  2551. if (speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
  2552. if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
  2553. else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
  2554. } else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
  2555. p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
  2556. }
  2557. ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
  2558. }
  2559. /*
  2560. * Convert the timing to bus clock counts.
  2561. */
  2562. ata_timing_quantize(t, t, T, UT);
  2563. /*
  2564. * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
  2565. * S.M.A.R.T * and some other commands. We have to ensure that the
  2566. * DMA cycle timing is slower/equal than the fastest PIO timing.
  2567. */
  2568. if (speed > XFER_PIO_6) {
  2569. ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
  2570. ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
  2571. }
  2572. /*
  2573. * Lengthen active & recovery time so that cycle time is correct.
  2574. */
  2575. if (t->act8b + t->rec8b < t->cyc8b) {
  2576. t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
  2577. t->rec8b = t->cyc8b - t->act8b;
  2578. }
  2579. if (t->active + t->recover < t->cycle) {
  2580. t->active += (t->cycle - (t->active + t->recover)) / 2;
  2581. t->recover = t->cycle - t->active;
  2582. }
  2583. /* In a few cases quantisation may produce enough errors to
  2584. leave t->cycle too low for the sum of active and recovery
  2585. if so we must correct this */
  2586. if (t->active + t->recover > t->cycle)
  2587. t->cycle = t->active + t->recover;
  2588. return 0;
  2589. }
  2590. /**
  2591. * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
  2592. * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
  2593. * @cycle: cycle duration in ns
  2594. *
  2595. * Return matching xfer mode for @cycle. The returned mode is of
  2596. * the transfer type specified by @xfer_shift. If @cycle is too
  2597. * slow for @xfer_shift, 0xff is returned. If @cycle is faster
  2598. * than the fastest known mode, the fasted mode is returned.
  2599. *
  2600. * LOCKING:
  2601. * None.
  2602. *
  2603. * RETURNS:
  2604. * Matching xfer_mode, 0xff if no match found.
  2605. */
  2606. u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
  2607. {
  2608. u8 base_mode = 0xff, last_mode = 0xff;
  2609. const struct ata_xfer_ent *ent;
  2610. const struct ata_timing *t;
  2611. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  2612. if (ent->shift == xfer_shift)
  2613. base_mode = ent->base;
  2614. for (t = ata_timing_find_mode(base_mode);
  2615. t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
  2616. unsigned short this_cycle;
  2617. switch (xfer_shift) {
  2618. case ATA_SHIFT_PIO:
  2619. case ATA_SHIFT_MWDMA:
  2620. this_cycle = t->cycle;
  2621. break;
  2622. case ATA_SHIFT_UDMA:
  2623. this_cycle = t->udma;
  2624. break;
  2625. default:
  2626. return 0xff;
  2627. }
  2628. if (cycle > this_cycle)
  2629. break;
  2630. last_mode = t->mode;
  2631. }
  2632. return last_mode;
  2633. }
  2634. /**
  2635. * ata_down_xfermask_limit - adjust dev xfer masks downward
  2636. * @dev: Device to adjust xfer masks
  2637. * @sel: ATA_DNXFER_* selector
  2638. *
  2639. * Adjust xfer masks of @dev downward. Note that this function
  2640. * does not apply the change. Invoking ata_set_mode() afterwards
  2641. * will apply the limit.
  2642. *
  2643. * LOCKING:
  2644. * Inherited from caller.
  2645. *
  2646. * RETURNS:
  2647. * 0 on success, negative errno on failure
  2648. */
  2649. int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
  2650. {
  2651. char buf[32];
  2652. unsigned long orig_mask, xfer_mask;
  2653. unsigned long pio_mask, mwdma_mask, udma_mask;
  2654. int quiet, highbit;
  2655. quiet = !!(sel & ATA_DNXFER_QUIET);
  2656. sel &= ~ATA_DNXFER_QUIET;
  2657. xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
  2658. dev->mwdma_mask,
  2659. dev->udma_mask);
  2660. ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
  2661. switch (sel) {
  2662. case ATA_DNXFER_PIO:
  2663. highbit = fls(pio_mask) - 1;
  2664. pio_mask &= ~(1 << highbit);
  2665. break;
  2666. case ATA_DNXFER_DMA:
  2667. if (udma_mask) {
  2668. highbit = fls(udma_mask) - 1;
  2669. udma_mask &= ~(1 << highbit);
  2670. if (!udma_mask)
  2671. return -ENOENT;
  2672. } else if (mwdma_mask) {
  2673. highbit = fls(mwdma_mask) - 1;
  2674. mwdma_mask &= ~(1 << highbit);
  2675. if (!mwdma_mask)
  2676. return -ENOENT;
  2677. }
  2678. break;
  2679. case ATA_DNXFER_40C:
  2680. udma_mask &= ATA_UDMA_MASK_40C;
  2681. break;
  2682. case ATA_DNXFER_FORCE_PIO0:
  2683. pio_mask &= 1;
  2684. case ATA_DNXFER_FORCE_PIO:
  2685. mwdma_mask = 0;
  2686. udma_mask = 0;
  2687. break;
  2688. default:
  2689. BUG();
  2690. }
  2691. xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
  2692. if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
  2693. return -ENOENT;
  2694. if (!quiet) {
  2695. if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
  2696. snprintf(buf, sizeof(buf), "%s:%s",
  2697. ata_mode_string(xfer_mask),
  2698. ata_mode_string(xfer_mask & ATA_MASK_PIO));
  2699. else
  2700. snprintf(buf, sizeof(buf), "%s",
  2701. ata_mode_string(xfer_mask));
  2702. ata_dev_printk(dev, KERN_WARNING,
  2703. "limiting speed to %s\n", buf);
  2704. }
  2705. ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
  2706. &dev->udma_mask);
  2707. return 0;
  2708. }
  2709. static int ata_dev_set_mode(struct ata_device *dev)
  2710. {
  2711. struct ata_eh_context *ehc = &dev->link->eh_context;
  2712. const char *dev_err_whine = "";
  2713. int ign_dev_err = 0;
  2714. unsigned int err_mask;
  2715. int rc;
  2716. dev->flags &= ~ATA_DFLAG_PIO;
  2717. if (dev->xfer_shift == ATA_SHIFT_PIO)
  2718. dev->flags |= ATA_DFLAG_PIO;
  2719. err_mask = ata_dev_set_xfermode(dev);
  2720. if (err_mask & ~AC_ERR_DEV)
  2721. goto fail;
  2722. /* revalidate */
  2723. ehc->i.flags |= ATA_EHI_POST_SETMODE;
  2724. rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
  2725. ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
  2726. if (rc)
  2727. return rc;
  2728. /* Old CFA may refuse this command, which is just fine */
  2729. if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
  2730. ign_dev_err = 1;
  2731. /* Some very old devices and some bad newer ones fail any kind of
  2732. SET_XFERMODE request but support PIO0-2 timings and no IORDY */
  2733. if (dev->xfer_shift == ATA_SHIFT_PIO && !ata_id_has_iordy(dev->id) &&
  2734. dev->pio_mode <= XFER_PIO_2)
  2735. ign_dev_err = 1;
  2736. /* Early MWDMA devices do DMA but don't allow DMA mode setting.
  2737. Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
  2738. if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
  2739. dev->dma_mode == XFER_MW_DMA_0 &&
  2740. (dev->id[63] >> 8) & 1)
  2741. ign_dev_err = 1;
  2742. /* if the device is actually configured correctly, ignore dev err */
  2743. if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
  2744. ign_dev_err = 1;
  2745. if (err_mask & AC_ERR_DEV) {
  2746. if (!ign_dev_err)
  2747. goto fail;
  2748. else
  2749. dev_err_whine = " (device error ignored)";
  2750. }
  2751. DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
  2752. dev->xfer_shift, (int)dev->xfer_mode);
  2753. ata_dev_printk(dev, KERN_INFO, "configured for %s%s\n",
  2754. ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
  2755. dev_err_whine);
  2756. return 0;
  2757. fail:
  2758. ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
  2759. "(err_mask=0x%x)\n", err_mask);
  2760. return -EIO;
  2761. }
  2762. /**
  2763. * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
  2764. * @link: link on which timings will be programmed
  2765. * @r_failed_dev: out parameter for failed device
  2766. *
  2767. * Standard implementation of the function used to tune and set
  2768. * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
  2769. * ata_dev_set_mode() fails, pointer to the failing device is
  2770. * returned in @r_failed_dev.
  2771. *
  2772. * LOCKING:
  2773. * PCI/etc. bus probe sem.
  2774. *
  2775. * RETURNS:
  2776. * 0 on success, negative errno otherwise
  2777. */
  2778. int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
  2779. {
  2780. struct ata_port *ap = link->ap;
  2781. struct ata_device *dev;
  2782. int rc = 0, used_dma = 0, found = 0;
  2783. /* step 1: calculate xfer_mask */
  2784. ata_link_for_each_dev(dev, link) {
  2785. unsigned long pio_mask, dma_mask;
  2786. unsigned int mode_mask;
  2787. if (!ata_dev_enabled(dev))
  2788. continue;
  2789. mode_mask = ATA_DMA_MASK_ATA;
  2790. if (dev->class == ATA_DEV_ATAPI)
  2791. mode_mask = ATA_DMA_MASK_ATAPI;
  2792. else if (ata_id_is_cfa(dev->id))
  2793. mode_mask = ATA_DMA_MASK_CFA;
  2794. ata_dev_xfermask(dev);
  2795. ata_force_xfermask(dev);
  2796. pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
  2797. dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
  2798. if (libata_dma_mask & mode_mask)
  2799. dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
  2800. else
  2801. dma_mask = 0;
  2802. dev->pio_mode = ata_xfer_mask2mode(pio_mask);
  2803. dev->dma_mode = ata_xfer_mask2mode(dma_mask);
  2804. found = 1;
  2805. if (dev->dma_mode != 0xff)
  2806. used_dma = 1;
  2807. }
  2808. if (!found)
  2809. goto out;
  2810. /* step 2: always set host PIO timings */
  2811. ata_link_for_each_dev(dev, link) {
  2812. if (!ata_dev_enabled(dev))
  2813. continue;
  2814. if (dev->pio_mode == 0xff) {
  2815. ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
  2816. rc = -EINVAL;
  2817. goto out;
  2818. }
  2819. dev->xfer_mode = dev->pio_mode;
  2820. dev->xfer_shift = ATA_SHIFT_PIO;
  2821. if (ap->ops->set_piomode)
  2822. ap->ops->set_piomode(ap, dev);
  2823. }
  2824. /* step 3: set host DMA timings */
  2825. ata_link_for_each_dev(dev, link) {
  2826. if (!ata_dev_enabled(dev) || dev->dma_mode == 0xff)
  2827. continue;
  2828. dev->xfer_mode = dev->dma_mode;
  2829. dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
  2830. if (ap->ops->set_dmamode)
  2831. ap->ops->set_dmamode(ap, dev);
  2832. }
  2833. /* step 4: update devices' xfer mode */
  2834. ata_link_for_each_dev(dev, link) {
  2835. /* don't update suspended devices' xfer mode */
  2836. if (!ata_dev_enabled(dev))
  2837. continue;
  2838. rc = ata_dev_set_mode(dev);
  2839. if (rc)
  2840. goto out;
  2841. }
  2842. /* Record simplex status. If we selected DMA then the other
  2843. * host channels are not permitted to do so.
  2844. */
  2845. if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
  2846. ap->host->simplex_claimed = ap;
  2847. out:
  2848. if (rc)
  2849. *r_failed_dev = dev;
  2850. return rc;
  2851. }
  2852. /**
  2853. * sata_link_debounce - debounce SATA phy status
  2854. * @link: ATA link to debounce SATA phy status for
  2855. * @params: timing parameters { interval, duratinon, timeout } in msec
  2856. * @deadline: deadline jiffies for the operation
  2857. *
  2858. * Make sure SStatus of @link reaches stable state, determined by
  2859. * holding the same value where DET is not 1 for @duration polled
  2860. * every @interval, before @timeout. Timeout constraints the
  2861. * beginning of the stable state. Because DET gets stuck at 1 on
  2862. * some controllers after hot unplugging, this functions waits
  2863. * until timeout then returns 0 if DET is stable at 1.
  2864. *
  2865. * @timeout is further limited by @deadline. The sooner of the
  2866. * two is used.
  2867. *
  2868. * LOCKING:
  2869. * Kernel thread context (may sleep)
  2870. *
  2871. * RETURNS:
  2872. * 0 on success, -errno on failure.
  2873. */
  2874. int sata_link_debounce(struct ata_link *link, const unsigned long *params,
  2875. unsigned long deadline)
  2876. {
  2877. unsigned long interval_msec = params[0];
  2878. unsigned long duration = msecs_to_jiffies(params[1]);
  2879. unsigned long last_jiffies, t;
  2880. u32 last, cur;
  2881. int rc;
  2882. t = jiffies + msecs_to_jiffies(params[2]);
  2883. if (time_before(t, deadline))
  2884. deadline = t;
  2885. if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
  2886. return rc;
  2887. cur &= 0xf;
  2888. last = cur;
  2889. last_jiffies = jiffies;
  2890. while (1) {
  2891. msleep(interval_msec);
  2892. if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
  2893. return rc;
  2894. cur &= 0xf;
  2895. /* DET stable? */
  2896. if (cur == last) {
  2897. if (cur == 1 && time_before(jiffies, deadline))
  2898. continue;
  2899. if (time_after(jiffies, last_jiffies + duration))
  2900. return 0;
  2901. continue;
  2902. }
  2903. /* unstable, start over */
  2904. last = cur;
  2905. last_jiffies = jiffies;
  2906. /* Check deadline. If debouncing failed, return
  2907. * -EPIPE to tell upper layer to lower link speed.
  2908. */
  2909. if (time_after(jiffies, deadline))
  2910. return -EPIPE;
  2911. }
  2912. }
  2913. /**
  2914. * sata_link_resume - resume SATA link
  2915. * @link: ATA link to resume SATA
  2916. * @params: timing parameters { interval, duratinon, timeout } in msec
  2917. * @deadline: deadline jiffies for the operation
  2918. *
  2919. * Resume SATA phy @link and debounce it.
  2920. *
  2921. * LOCKING:
  2922. * Kernel thread context (may sleep)
  2923. *
  2924. * RETURNS:
  2925. * 0 on success, -errno on failure.
  2926. */
  2927. int sata_link_resume(struct ata_link *link, const unsigned long *params,
  2928. unsigned long deadline)
  2929. {
  2930. u32 scontrol;
  2931. int rc;
  2932. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  2933. return rc;
  2934. scontrol = (scontrol & 0x0f0) | 0x300;
  2935. if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
  2936. return rc;
  2937. /* Some PHYs react badly if SStatus is pounded immediately
  2938. * after resuming. Delay 200ms before debouncing.
  2939. */
  2940. msleep(200);
  2941. return sata_link_debounce(link, params, deadline);
  2942. }
  2943. /**
  2944. * ata_std_prereset - prepare for reset
  2945. * @link: ATA link to be reset
  2946. * @deadline: deadline jiffies for the operation
  2947. *
  2948. * @link is about to be reset. Initialize it. Failure from
  2949. * prereset makes libata abort whole reset sequence and give up
  2950. * that port, so prereset should be best-effort. It does its
  2951. * best to prepare for reset sequence but if things go wrong, it
  2952. * should just whine, not fail.
  2953. *
  2954. * LOCKING:
  2955. * Kernel thread context (may sleep)
  2956. *
  2957. * RETURNS:
  2958. * 0 on success, -errno otherwise.
  2959. */
  2960. int ata_std_prereset(struct ata_link *link, unsigned long deadline)
  2961. {
  2962. struct ata_port *ap = link->ap;
  2963. struct ata_eh_context *ehc = &link->eh_context;
  2964. const unsigned long *timing = sata_ehc_deb_timing(ehc);
  2965. int rc;
  2966. /* if we're about to do hardreset, nothing more to do */
  2967. if (ehc->i.action & ATA_EH_HARDRESET)
  2968. return 0;
  2969. /* if SATA, resume link */
  2970. if (ap->flags & ATA_FLAG_SATA) {
  2971. rc = sata_link_resume(link, timing, deadline);
  2972. /* whine about phy resume failure but proceed */
  2973. if (rc && rc != -EOPNOTSUPP)
  2974. ata_link_printk(link, KERN_WARNING, "failed to resume "
  2975. "link for reset (errno=%d)\n", rc);
  2976. }
  2977. /* wait for !BSY if we don't know that no device is attached */
  2978. if (!ata_link_offline(link)) {
  2979. rc = ata_wait_ready(ap, deadline);
  2980. if (rc && rc != -ENODEV) {
  2981. ata_link_printk(link, KERN_WARNING, "device not ready "
  2982. "(errno=%d), forcing hardreset\n", rc);
  2983. ehc->i.action |= ATA_EH_HARDRESET;
  2984. }
  2985. }
  2986. return 0;
  2987. }
  2988. /**
  2989. * sata_link_hardreset - reset link via SATA phy reset
  2990. * @link: link to reset
  2991. * @timing: timing parameters { interval, duratinon, timeout } in msec
  2992. * @deadline: deadline jiffies for the operation
  2993. *
  2994. * SATA phy-reset @link using DET bits of SControl register.
  2995. *
  2996. * LOCKING:
  2997. * Kernel thread context (may sleep)
  2998. *
  2999. * RETURNS:
  3000. * 0 on success, -errno otherwise.
  3001. */
  3002. int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
  3003. unsigned long deadline)
  3004. {
  3005. u32 scontrol;
  3006. int rc;
  3007. DPRINTK("ENTER\n");
  3008. if (sata_set_spd_needed(link)) {
  3009. /* SATA spec says nothing about how to reconfigure
  3010. * spd. To be on the safe side, turn off phy during
  3011. * reconfiguration. This works for at least ICH7 AHCI
  3012. * and Sil3124.
  3013. */
  3014. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  3015. goto out;
  3016. scontrol = (scontrol & 0x0f0) | 0x304;
  3017. if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
  3018. goto out;
  3019. sata_set_spd(link);
  3020. }
  3021. /* issue phy wake/reset */
  3022. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  3023. goto out;
  3024. scontrol = (scontrol & 0x0f0) | 0x301;
  3025. if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
  3026. goto out;
  3027. /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
  3028. * 10.4.2 says at least 1 ms.
  3029. */
  3030. msleep(1);
  3031. /* bring link back */
  3032. rc = sata_link_resume(link, timing, deadline);
  3033. out:
  3034. DPRINTK("EXIT, rc=%d\n", rc);
  3035. return rc;
  3036. }
  3037. /**
  3038. * ata_std_postreset - standard postreset callback
  3039. * @link: the target ata_link
  3040. * @classes: classes of attached devices
  3041. *
  3042. * This function is invoked after a successful reset. Note that
  3043. * the device might have been reset more than once using
  3044. * different reset methods before postreset is invoked.
  3045. *
  3046. * LOCKING:
  3047. * Kernel thread context (may sleep)
  3048. */
  3049. void ata_std_postreset(struct ata_link *link, unsigned int *classes)
  3050. {
  3051. struct ata_port *ap = link->ap;
  3052. u32 serror;
  3053. DPRINTK("ENTER\n");
  3054. /* print link status */
  3055. sata_print_link_status(link);
  3056. /* clear SError */
  3057. if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
  3058. sata_scr_write(link, SCR_ERROR, serror);
  3059. link->eh_info.serror = 0;
  3060. /* is double-select really necessary? */
  3061. if (classes[0] != ATA_DEV_NONE)
  3062. ap->ops->dev_select(ap, 1);
  3063. if (classes[1] != ATA_DEV_NONE)
  3064. ap->ops->dev_select(ap, 0);
  3065. /* bail out if no device is present */
  3066. if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
  3067. DPRINTK("EXIT, no device\n");
  3068. return;
  3069. }
  3070. /* set up device control */
  3071. if (ap->ioaddr.ctl_addr)
  3072. iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
  3073. DPRINTK("EXIT\n");
  3074. }
  3075. /**
  3076. * ata_dev_same_device - Determine whether new ID matches configured device
  3077. * @dev: device to compare against
  3078. * @new_class: class of the new device
  3079. * @new_id: IDENTIFY page of the new device
  3080. *
  3081. * Compare @new_class and @new_id against @dev and determine
  3082. * whether @dev is the device indicated by @new_class and
  3083. * @new_id.
  3084. *
  3085. * LOCKING:
  3086. * None.
  3087. *
  3088. * RETURNS:
  3089. * 1 if @dev matches @new_class and @new_id, 0 otherwise.
  3090. */
  3091. static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
  3092. const u16 *new_id)
  3093. {
  3094. const u16 *old_id = dev->id;
  3095. unsigned char model[2][ATA_ID_PROD_LEN + 1];
  3096. unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
  3097. if (dev->class != new_class) {
  3098. ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
  3099. dev->class, new_class);
  3100. return 0;
  3101. }
  3102. ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
  3103. ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
  3104. ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
  3105. ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
  3106. if (strcmp(model[0], model[1])) {
  3107. ata_dev_printk(dev, KERN_INFO, "model number mismatch "
  3108. "'%s' != '%s'\n", model[0], model[1]);
  3109. return 0;
  3110. }
  3111. if (strcmp(serial[0], serial[1])) {
  3112. ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
  3113. "'%s' != '%s'\n", serial[0], serial[1]);
  3114. return 0;
  3115. }
  3116. return 1;
  3117. }
  3118. /**
  3119. * ata_dev_reread_id - Re-read IDENTIFY data
  3120. * @dev: target ATA device
  3121. * @readid_flags: read ID flags
  3122. *
  3123. * Re-read IDENTIFY page and make sure @dev is still attached to
  3124. * the port.
  3125. *
  3126. * LOCKING:
  3127. * Kernel thread context (may sleep)
  3128. *
  3129. * RETURNS:
  3130. * 0 on success, negative errno otherwise
  3131. */
  3132. int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
  3133. {
  3134. unsigned int class = dev->class;
  3135. u16 *id = (void *)dev->link->ap->sector_buf;
  3136. int rc;
  3137. /* read ID data */
  3138. rc = ata_dev_read_id(dev, &class, readid_flags, id);
  3139. if (rc)
  3140. return rc;
  3141. /* is the device still there? */
  3142. if (!ata_dev_same_device(dev, class, id))
  3143. return -ENODEV;
  3144. memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
  3145. return 0;
  3146. }
  3147. /**
  3148. * ata_dev_revalidate - Revalidate ATA device
  3149. * @dev: device to revalidate
  3150. * @new_class: new class code
  3151. * @readid_flags: read ID flags
  3152. *
  3153. * Re-read IDENTIFY page, make sure @dev is still attached to the
  3154. * port and reconfigure it according to the new IDENTIFY page.
  3155. *
  3156. * LOCKING:
  3157. * Kernel thread context (may sleep)
  3158. *
  3159. * RETURNS:
  3160. * 0 on success, negative errno otherwise
  3161. */
  3162. int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
  3163. unsigned int readid_flags)
  3164. {
  3165. u64 n_sectors = dev->n_sectors;
  3166. int rc;
  3167. if (!ata_dev_enabled(dev))
  3168. return -ENODEV;
  3169. /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
  3170. if (ata_class_enabled(new_class) &&
  3171. new_class != ATA_DEV_ATA && new_class != ATA_DEV_ATAPI) {
  3172. ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
  3173. dev->class, new_class);
  3174. rc = -ENODEV;
  3175. goto fail;
  3176. }
  3177. /* re-read ID */
  3178. rc = ata_dev_reread_id(dev, readid_flags);
  3179. if (rc)
  3180. goto fail;
  3181. /* configure device according to the new ID */
  3182. rc = ata_dev_configure(dev);
  3183. if (rc)
  3184. goto fail;
  3185. /* verify n_sectors hasn't changed */
  3186. if (dev->class == ATA_DEV_ATA && n_sectors &&
  3187. dev->n_sectors != n_sectors) {
  3188. ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
  3189. "%llu != %llu\n",
  3190. (unsigned long long)n_sectors,
  3191. (unsigned long long)dev->n_sectors);
  3192. /* restore original n_sectors */
  3193. dev->n_sectors = n_sectors;
  3194. rc = -ENODEV;
  3195. goto fail;
  3196. }
  3197. return 0;
  3198. fail:
  3199. ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
  3200. return rc;
  3201. }
  3202. struct ata_blacklist_entry {
  3203. const char *model_num;
  3204. const char *model_rev;
  3205. unsigned long horkage;
  3206. };
  3207. static const struct ata_blacklist_entry ata_device_blacklist [] = {
  3208. /* Devices with DMA related problems under Linux */
  3209. { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
  3210. { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
  3211. { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
  3212. { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
  3213. { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
  3214. { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
  3215. { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
  3216. { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
  3217. { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
  3218. { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
  3219. { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
  3220. { "CRD-84", NULL, ATA_HORKAGE_NODMA },
  3221. { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
  3222. { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
  3223. { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
  3224. { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
  3225. { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
  3226. { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
  3227. { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
  3228. { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
  3229. { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
  3230. { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
  3231. { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
  3232. { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
  3233. { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
  3234. { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
  3235. { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
  3236. { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
  3237. { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
  3238. { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
  3239. /* Odd clown on sil3726/4726 PMPs */
  3240. { "Config Disk", NULL, ATA_HORKAGE_NODMA |
  3241. ATA_HORKAGE_SKIP_PM },
  3242. /* Weird ATAPI devices */
  3243. { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
  3244. /* Devices we expect to fail diagnostics */
  3245. /* Devices where NCQ should be avoided */
  3246. /* NCQ is slow */
  3247. { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
  3248. { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
  3249. /* http://thread.gmane.org/gmane.linux.ide/14907 */
  3250. { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
  3251. /* NCQ is broken */
  3252. { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
  3253. { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
  3254. { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
  3255. { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
  3256. /* Blacklist entries taken from Silicon Image 3124/3132
  3257. Windows driver .inf file - also several Linux problem reports */
  3258. { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
  3259. { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
  3260. { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
  3261. /* devices which puke on READ_NATIVE_MAX */
  3262. { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
  3263. { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
  3264. { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
  3265. { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
  3266. /* Devices which report 1 sector over size HPA */
  3267. { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
  3268. { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
  3269. { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
  3270. /* Devices which get the IVB wrong */
  3271. { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
  3272. { "TSSTcorp CDDVDW SH-S202J", "SB00", ATA_HORKAGE_IVB, },
  3273. { "TSSTcorp CDDVDW SH-S202J", "SB01", ATA_HORKAGE_IVB, },
  3274. { "TSSTcorp CDDVDW SH-S202N", "SB00", ATA_HORKAGE_IVB, },
  3275. { "TSSTcorp CDDVDW SH-S202N", "SB01", ATA_HORKAGE_IVB, },
  3276. /* End Marker */
  3277. { }
  3278. };
  3279. static int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
  3280. {
  3281. const char *p;
  3282. int len;
  3283. /*
  3284. * check for trailing wildcard: *\0
  3285. */
  3286. p = strchr(patt, wildchar);
  3287. if (p && ((*(p + 1)) == 0))
  3288. len = p - patt;
  3289. else {
  3290. len = strlen(name);
  3291. if (!len) {
  3292. if (!*patt)
  3293. return 0;
  3294. return -1;
  3295. }
  3296. }
  3297. return strncmp(patt, name, len);
  3298. }
  3299. static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
  3300. {
  3301. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  3302. unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
  3303. const struct ata_blacklist_entry *ad = ata_device_blacklist;
  3304. ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
  3305. ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
  3306. while (ad->model_num) {
  3307. if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
  3308. if (ad->model_rev == NULL)
  3309. return ad->horkage;
  3310. if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
  3311. return ad->horkage;
  3312. }
  3313. ad++;
  3314. }
  3315. return 0;
  3316. }
  3317. static int ata_dma_blacklisted(const struct ata_device *dev)
  3318. {
  3319. /* We don't support polling DMA.
  3320. * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
  3321. * if the LLDD handles only interrupts in the HSM_ST_LAST state.
  3322. */
  3323. if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
  3324. (dev->flags & ATA_DFLAG_CDB_INTR))
  3325. return 1;
  3326. return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
  3327. }
  3328. /**
  3329. * ata_is_40wire - check drive side detection
  3330. * @dev: device
  3331. *
  3332. * Perform drive side detection decoding, allowing for device vendors
  3333. * who can't follow the documentation.
  3334. */
  3335. static int ata_is_40wire(struct ata_device *dev)
  3336. {
  3337. if (dev->horkage & ATA_HORKAGE_IVB)
  3338. return ata_drive_40wire_relaxed(dev->id);
  3339. return ata_drive_40wire(dev->id);
  3340. }
  3341. /**
  3342. * ata_dev_xfermask - Compute supported xfermask of the given device
  3343. * @dev: Device to compute xfermask for
  3344. *
  3345. * Compute supported xfermask of @dev and store it in
  3346. * dev->*_mask. This function is responsible for applying all
  3347. * known limits including host controller limits, device
  3348. * blacklist, etc...
  3349. *
  3350. * LOCKING:
  3351. * None.
  3352. */
  3353. static void ata_dev_xfermask(struct ata_device *dev)
  3354. {
  3355. struct ata_link *link = dev->link;
  3356. struct ata_port *ap = link->ap;
  3357. struct ata_host *host = ap->host;
  3358. unsigned long xfer_mask;
  3359. /* controller modes available */
  3360. xfer_mask = ata_pack_xfermask(ap->pio_mask,
  3361. ap->mwdma_mask, ap->udma_mask);
  3362. /* drive modes available */
  3363. xfer_mask &= ata_pack_xfermask(dev->pio_mask,
  3364. dev->mwdma_mask, dev->udma_mask);
  3365. xfer_mask &= ata_id_xfermask(dev->id);
  3366. /*
  3367. * CFA Advanced TrueIDE timings are not allowed on a shared
  3368. * cable
  3369. */
  3370. if (ata_dev_pair(dev)) {
  3371. /* No PIO5 or PIO6 */
  3372. xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
  3373. /* No MWDMA3 or MWDMA 4 */
  3374. xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
  3375. }
  3376. if (ata_dma_blacklisted(dev)) {
  3377. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  3378. ata_dev_printk(dev, KERN_WARNING,
  3379. "device is on DMA blacklist, disabling DMA\n");
  3380. }
  3381. if ((host->flags & ATA_HOST_SIMPLEX) &&
  3382. host->simplex_claimed && host->simplex_claimed != ap) {
  3383. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  3384. ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
  3385. "other device, disabling DMA\n");
  3386. }
  3387. if (ap->flags & ATA_FLAG_NO_IORDY)
  3388. xfer_mask &= ata_pio_mask_no_iordy(dev);
  3389. if (ap->ops->mode_filter)
  3390. xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
  3391. /* Apply cable rule here. Don't apply it early because when
  3392. * we handle hot plug the cable type can itself change.
  3393. * Check this last so that we know if the transfer rate was
  3394. * solely limited by the cable.
  3395. * Unknown or 80 wire cables reported host side are checked
  3396. * drive side as well. Cases where we know a 40wire cable
  3397. * is used safely for 80 are not checked here.
  3398. */
  3399. if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
  3400. /* UDMA/44 or higher would be available */
  3401. if ((ap->cbl == ATA_CBL_PATA40) ||
  3402. (ata_is_40wire(dev) &&
  3403. (ap->cbl == ATA_CBL_PATA_UNK ||
  3404. ap->cbl == ATA_CBL_PATA80))) {
  3405. ata_dev_printk(dev, KERN_WARNING,
  3406. "limited to UDMA/33 due to 40-wire cable\n");
  3407. xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
  3408. }
  3409. ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
  3410. &dev->mwdma_mask, &dev->udma_mask);
  3411. }
  3412. /**
  3413. * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
  3414. * @dev: Device to which command will be sent
  3415. *
  3416. * Issue SET FEATURES - XFER MODE command to device @dev
  3417. * on port @ap.
  3418. *
  3419. * LOCKING:
  3420. * PCI/etc. bus probe sem.
  3421. *
  3422. * RETURNS:
  3423. * 0 on success, AC_ERR_* mask otherwise.
  3424. */
  3425. static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
  3426. {
  3427. struct ata_taskfile tf;
  3428. unsigned int err_mask;
  3429. /* set up set-features taskfile */
  3430. DPRINTK("set features - xfer mode\n");
  3431. /* Some controllers and ATAPI devices show flaky interrupt
  3432. * behavior after setting xfer mode. Use polling instead.
  3433. */
  3434. ata_tf_init(dev, &tf);
  3435. tf.command = ATA_CMD_SET_FEATURES;
  3436. tf.feature = SETFEATURES_XFER;
  3437. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
  3438. tf.protocol = ATA_PROT_NODATA;
  3439. /* If we are using IORDY we must send the mode setting command */
  3440. if (ata_pio_need_iordy(dev))
  3441. tf.nsect = dev->xfer_mode;
  3442. /* If the device has IORDY and the controller does not - turn it off */
  3443. else if (ata_id_has_iordy(dev->id))
  3444. tf.nsect = 0x01;
  3445. else /* In the ancient relic department - skip all of this */
  3446. return 0;
  3447. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  3448. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  3449. return err_mask;
  3450. }
  3451. /**
  3452. * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
  3453. * @dev: Device to which command will be sent
  3454. * @enable: Whether to enable or disable the feature
  3455. * @feature: The sector count represents the feature to set
  3456. *
  3457. * Issue SET FEATURES - SATA FEATURES command to device @dev
  3458. * on port @ap with sector count
  3459. *
  3460. * LOCKING:
  3461. * PCI/etc. bus probe sem.
  3462. *
  3463. * RETURNS:
  3464. * 0 on success, AC_ERR_* mask otherwise.
  3465. */
  3466. static unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable,
  3467. u8 feature)
  3468. {
  3469. struct ata_taskfile tf;
  3470. unsigned int err_mask;
  3471. /* set up set-features taskfile */
  3472. DPRINTK("set features - SATA features\n");
  3473. ata_tf_init(dev, &tf);
  3474. tf.command = ATA_CMD_SET_FEATURES;
  3475. tf.feature = enable;
  3476. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  3477. tf.protocol = ATA_PROT_NODATA;
  3478. tf.nsect = feature;
  3479. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  3480. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  3481. return err_mask;
  3482. }
  3483. /**
  3484. * ata_dev_init_params - Issue INIT DEV PARAMS command
  3485. * @dev: Device to which command will be sent
  3486. * @heads: Number of heads (taskfile parameter)
  3487. * @sectors: Number of sectors (taskfile parameter)
  3488. *
  3489. * LOCKING:
  3490. * Kernel thread context (may sleep)
  3491. *
  3492. * RETURNS:
  3493. * 0 on success, AC_ERR_* mask otherwise.
  3494. */
  3495. static unsigned int ata_dev_init_params(struct ata_device *dev,
  3496. u16 heads, u16 sectors)
  3497. {
  3498. struct ata_taskfile tf;
  3499. unsigned int err_mask;
  3500. /* Number of sectors per track 1-255. Number of heads 1-16 */
  3501. if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
  3502. return AC_ERR_INVALID;
  3503. /* set up init dev params taskfile */
  3504. DPRINTK("init dev params \n");
  3505. ata_tf_init(dev, &tf);
  3506. tf.command = ATA_CMD_INIT_DEV_PARAMS;
  3507. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  3508. tf.protocol = ATA_PROT_NODATA;
  3509. tf.nsect = sectors;
  3510. tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
  3511. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  3512. /* A clean abort indicates an original or just out of spec drive
  3513. and we should continue as we issue the setup based on the
  3514. drive reported working geometry */
  3515. if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
  3516. err_mask = 0;
  3517. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  3518. return err_mask;
  3519. }
  3520. /**
  3521. * ata_sg_clean - Unmap DMA memory associated with command
  3522. * @qc: Command containing DMA memory to be released
  3523. *
  3524. * Unmap all mapped DMA memory associated with this command.
  3525. *
  3526. * LOCKING:
  3527. * spin_lock_irqsave(host lock)
  3528. */
  3529. void ata_sg_clean(struct ata_queued_cmd *qc)
  3530. {
  3531. struct ata_port *ap = qc->ap;
  3532. struct scatterlist *sg = qc->sg;
  3533. int dir = qc->dma_dir;
  3534. WARN_ON(sg == NULL);
  3535. VPRINTK("unmapping %u sg elements\n", qc->n_elem);
  3536. if (qc->n_elem)
  3537. dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
  3538. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  3539. qc->sg = NULL;
  3540. }
  3541. /**
  3542. * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
  3543. * @qc: Metadata associated with taskfile to check
  3544. *
  3545. * Allow low-level driver to filter ATA PACKET commands, returning
  3546. * a status indicating whether or not it is OK to use DMA for the
  3547. * supplied PACKET command.
  3548. *
  3549. * LOCKING:
  3550. * spin_lock_irqsave(host lock)
  3551. *
  3552. * RETURNS: 0 when ATAPI DMA can be used
  3553. * nonzero otherwise
  3554. */
  3555. int ata_check_atapi_dma(struct ata_queued_cmd *qc)
  3556. {
  3557. struct ata_port *ap = qc->ap;
  3558. /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
  3559. * few ATAPI devices choke on such DMA requests.
  3560. */
  3561. if (unlikely(qc->nbytes & 15))
  3562. return 1;
  3563. if (ap->ops->check_atapi_dma)
  3564. return ap->ops->check_atapi_dma(qc);
  3565. return 0;
  3566. }
  3567. /**
  3568. * ata_std_qc_defer - Check whether a qc needs to be deferred
  3569. * @qc: ATA command in question
  3570. *
  3571. * Non-NCQ commands cannot run with any other command, NCQ or
  3572. * not. As upper layer only knows the queue depth, we are
  3573. * responsible for maintaining exclusion. This function checks
  3574. * whether a new command @qc can be issued.
  3575. *
  3576. * LOCKING:
  3577. * spin_lock_irqsave(host lock)
  3578. *
  3579. * RETURNS:
  3580. * ATA_DEFER_* if deferring is needed, 0 otherwise.
  3581. */
  3582. int ata_std_qc_defer(struct ata_queued_cmd *qc)
  3583. {
  3584. struct ata_link *link = qc->dev->link;
  3585. if (qc->tf.protocol == ATA_PROT_NCQ) {
  3586. if (!ata_tag_valid(link->active_tag))
  3587. return 0;
  3588. } else {
  3589. if (!ata_tag_valid(link->active_tag) && !link->sactive)
  3590. return 0;
  3591. }
  3592. return ATA_DEFER_LINK;
  3593. }
  3594. void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
  3595. /**
  3596. * ata_sg_init - Associate command with scatter-gather table.
  3597. * @qc: Command to be associated
  3598. * @sg: Scatter-gather table.
  3599. * @n_elem: Number of elements in s/g table.
  3600. *
  3601. * Initialize the data-related elements of queued_cmd @qc
  3602. * to point to a scatter-gather table @sg, containing @n_elem
  3603. * elements.
  3604. *
  3605. * LOCKING:
  3606. * spin_lock_irqsave(host lock)
  3607. */
  3608. void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
  3609. unsigned int n_elem)
  3610. {
  3611. qc->sg = sg;
  3612. qc->n_elem = n_elem;
  3613. qc->cursg = qc->sg;
  3614. }
  3615. /**
  3616. * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
  3617. * @qc: Command with scatter-gather table to be mapped.
  3618. *
  3619. * DMA-map the scatter-gather table associated with queued_cmd @qc.
  3620. *
  3621. * LOCKING:
  3622. * spin_lock_irqsave(host lock)
  3623. *
  3624. * RETURNS:
  3625. * Zero on success, negative on error.
  3626. *
  3627. */
  3628. static int ata_sg_setup(struct ata_queued_cmd *qc)
  3629. {
  3630. struct ata_port *ap = qc->ap;
  3631. unsigned int n_elem;
  3632. VPRINTK("ENTER, ata%u\n", ap->print_id);
  3633. n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
  3634. if (n_elem < 1)
  3635. return -1;
  3636. DPRINTK("%d sg elements mapped\n", n_elem);
  3637. qc->n_elem = n_elem;
  3638. qc->flags |= ATA_QCFLAG_DMAMAP;
  3639. return 0;
  3640. }
  3641. /**
  3642. * swap_buf_le16 - swap halves of 16-bit words in place
  3643. * @buf: Buffer to swap
  3644. * @buf_words: Number of 16-bit words in buffer.
  3645. *
  3646. * Swap halves of 16-bit words if needed to convert from
  3647. * little-endian byte order to native cpu byte order, or
  3648. * vice-versa.
  3649. *
  3650. * LOCKING:
  3651. * Inherited from caller.
  3652. */
  3653. void swap_buf_le16(u16 *buf, unsigned int buf_words)
  3654. {
  3655. #ifdef __BIG_ENDIAN
  3656. unsigned int i;
  3657. for (i = 0; i < buf_words; i++)
  3658. buf[i] = le16_to_cpu(buf[i]);
  3659. #endif /* __BIG_ENDIAN */
  3660. }
  3661. /**
  3662. * ata_qc_new - Request an available ATA command, for queueing
  3663. * @ap: Port associated with device @dev
  3664. * @dev: Device from whom we request an available command structure
  3665. *
  3666. * LOCKING:
  3667. * None.
  3668. */
  3669. static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
  3670. {
  3671. struct ata_queued_cmd *qc = NULL;
  3672. unsigned int i;
  3673. /* no command while frozen */
  3674. if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
  3675. return NULL;
  3676. /* the last tag is reserved for internal command. */
  3677. for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
  3678. if (!test_and_set_bit(i, &ap->qc_allocated)) {
  3679. qc = __ata_qc_from_tag(ap, i);
  3680. break;
  3681. }
  3682. if (qc)
  3683. qc->tag = i;
  3684. return qc;
  3685. }
  3686. /**
  3687. * ata_qc_new_init - Request an available ATA command, and initialize it
  3688. * @dev: Device from whom we request an available command structure
  3689. *
  3690. * LOCKING:
  3691. * None.
  3692. */
  3693. struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
  3694. {
  3695. struct ata_port *ap = dev->link->ap;
  3696. struct ata_queued_cmd *qc;
  3697. qc = ata_qc_new(ap);
  3698. if (qc) {
  3699. qc->scsicmd = NULL;
  3700. qc->ap = ap;
  3701. qc->dev = dev;
  3702. ata_qc_reinit(qc);
  3703. }
  3704. return qc;
  3705. }
  3706. /**
  3707. * ata_qc_free - free unused ata_queued_cmd
  3708. * @qc: Command to complete
  3709. *
  3710. * Designed to free unused ata_queued_cmd object
  3711. * in case something prevents using it.
  3712. *
  3713. * LOCKING:
  3714. * spin_lock_irqsave(host lock)
  3715. */
  3716. void ata_qc_free(struct ata_queued_cmd *qc)
  3717. {
  3718. struct ata_port *ap = qc->ap;
  3719. unsigned int tag;
  3720. WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  3721. qc->flags = 0;
  3722. tag = qc->tag;
  3723. if (likely(ata_tag_valid(tag))) {
  3724. qc->tag = ATA_TAG_POISON;
  3725. clear_bit(tag, &ap->qc_allocated);
  3726. }
  3727. }
  3728. void __ata_qc_complete(struct ata_queued_cmd *qc)
  3729. {
  3730. struct ata_port *ap = qc->ap;
  3731. struct ata_link *link = qc->dev->link;
  3732. WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  3733. WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
  3734. if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
  3735. ata_sg_clean(qc);
  3736. /* command should be marked inactive atomically with qc completion */
  3737. if (qc->tf.protocol == ATA_PROT_NCQ) {
  3738. link->sactive &= ~(1 << qc->tag);
  3739. if (!link->sactive)
  3740. ap->nr_active_links--;
  3741. } else {
  3742. link->active_tag = ATA_TAG_POISON;
  3743. ap->nr_active_links--;
  3744. }
  3745. /* clear exclusive status */
  3746. if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
  3747. ap->excl_link == link))
  3748. ap->excl_link = NULL;
  3749. /* atapi: mark qc as inactive to prevent the interrupt handler
  3750. * from completing the command twice later, before the error handler
  3751. * is called. (when rc != 0 and atapi request sense is needed)
  3752. */
  3753. qc->flags &= ~ATA_QCFLAG_ACTIVE;
  3754. ap->qc_active &= ~(1 << qc->tag);
  3755. /* call completion callback */
  3756. qc->complete_fn(qc);
  3757. }
  3758. static void fill_result_tf(struct ata_queued_cmd *qc)
  3759. {
  3760. struct ata_port *ap = qc->ap;
  3761. qc->result_tf.flags = qc->tf.flags;
  3762. ap->ops->tf_read(ap, &qc->result_tf);
  3763. }
  3764. static void ata_verify_xfer(struct ata_queued_cmd *qc)
  3765. {
  3766. struct ata_device *dev = qc->dev;
  3767. if (ata_tag_internal(qc->tag))
  3768. return;
  3769. if (ata_is_nodata(qc->tf.protocol))
  3770. return;
  3771. if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
  3772. return;
  3773. dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
  3774. }
  3775. /**
  3776. * ata_qc_complete - Complete an active ATA command
  3777. * @qc: Command to complete
  3778. * @err_mask: ATA Status register contents
  3779. *
  3780. * Indicate to the mid and upper layers that an ATA
  3781. * command has completed, with either an ok or not-ok status.
  3782. *
  3783. * LOCKING:
  3784. * spin_lock_irqsave(host lock)
  3785. */
  3786. void ata_qc_complete(struct ata_queued_cmd *qc)
  3787. {
  3788. struct ata_port *ap = qc->ap;
  3789. /* XXX: New EH and old EH use different mechanisms to
  3790. * synchronize EH with regular execution path.
  3791. *
  3792. * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
  3793. * Normal execution path is responsible for not accessing a
  3794. * failed qc. libata core enforces the rule by returning NULL
  3795. * from ata_qc_from_tag() for failed qcs.
  3796. *
  3797. * Old EH depends on ata_qc_complete() nullifying completion
  3798. * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
  3799. * not synchronize with interrupt handler. Only PIO task is
  3800. * taken care of.
  3801. */
  3802. if (ap->ops->error_handler) {
  3803. struct ata_device *dev = qc->dev;
  3804. struct ata_eh_info *ehi = &dev->link->eh_info;
  3805. WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
  3806. if (unlikely(qc->err_mask))
  3807. qc->flags |= ATA_QCFLAG_FAILED;
  3808. if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
  3809. if (!ata_tag_internal(qc->tag)) {
  3810. /* always fill result TF for failed qc */
  3811. fill_result_tf(qc);
  3812. ata_qc_schedule_eh(qc);
  3813. return;
  3814. }
  3815. }
  3816. /* read result TF if requested */
  3817. if (qc->flags & ATA_QCFLAG_RESULT_TF)
  3818. fill_result_tf(qc);
  3819. /* Some commands need post-processing after successful
  3820. * completion.
  3821. */
  3822. switch (qc->tf.command) {
  3823. case ATA_CMD_SET_FEATURES:
  3824. if (qc->tf.feature != SETFEATURES_WC_ON &&
  3825. qc->tf.feature != SETFEATURES_WC_OFF)
  3826. break;
  3827. /* fall through */
  3828. case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
  3829. case ATA_CMD_SET_MULTI: /* multi_count changed */
  3830. /* revalidate device */
  3831. ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
  3832. ata_port_schedule_eh(ap);
  3833. break;
  3834. case ATA_CMD_SLEEP:
  3835. dev->flags |= ATA_DFLAG_SLEEPING;
  3836. break;
  3837. }
  3838. if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
  3839. ata_verify_xfer(qc);
  3840. __ata_qc_complete(qc);
  3841. } else {
  3842. if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
  3843. return;
  3844. /* read result TF if failed or requested */
  3845. if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
  3846. fill_result_tf(qc);
  3847. __ata_qc_complete(qc);
  3848. }
  3849. }
  3850. /**
  3851. * ata_qc_complete_multiple - Complete multiple qcs successfully
  3852. * @ap: port in question
  3853. * @qc_active: new qc_active mask
  3854. * @finish_qc: LLDD callback invoked before completing a qc
  3855. *
  3856. * Complete in-flight commands. This functions is meant to be
  3857. * called from low-level driver's interrupt routine to complete
  3858. * requests normally. ap->qc_active and @qc_active is compared
  3859. * and commands are completed accordingly.
  3860. *
  3861. * LOCKING:
  3862. * spin_lock_irqsave(host lock)
  3863. *
  3864. * RETURNS:
  3865. * Number of completed commands on success, -errno otherwise.
  3866. */
  3867. int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
  3868. void (*finish_qc)(struct ata_queued_cmd *))
  3869. {
  3870. int nr_done = 0;
  3871. u32 done_mask;
  3872. int i;
  3873. done_mask = ap->qc_active ^ qc_active;
  3874. if (unlikely(done_mask & qc_active)) {
  3875. ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
  3876. "(%08x->%08x)\n", ap->qc_active, qc_active);
  3877. return -EINVAL;
  3878. }
  3879. for (i = 0; i < ATA_MAX_QUEUE; i++) {
  3880. struct ata_queued_cmd *qc;
  3881. if (!(done_mask & (1 << i)))
  3882. continue;
  3883. if ((qc = ata_qc_from_tag(ap, i))) {
  3884. if (finish_qc)
  3885. finish_qc(qc);
  3886. ata_qc_complete(qc);
  3887. nr_done++;
  3888. }
  3889. }
  3890. return nr_done;
  3891. }
  3892. /**
  3893. * ata_qc_issue - issue taskfile to device
  3894. * @qc: command to issue to device
  3895. *
  3896. * Prepare an ATA command to submission to device.
  3897. * This includes mapping the data into a DMA-able
  3898. * area, filling in the S/G table, and finally
  3899. * writing the taskfile to hardware, starting the command.
  3900. *
  3901. * LOCKING:
  3902. * spin_lock_irqsave(host lock)
  3903. */
  3904. void ata_qc_issue(struct ata_queued_cmd *qc)
  3905. {
  3906. struct ata_port *ap = qc->ap;
  3907. struct ata_link *link = qc->dev->link;
  3908. u8 prot = qc->tf.protocol;
  3909. /* Make sure only one non-NCQ command is outstanding. The
  3910. * check is skipped for old EH because it reuses active qc to
  3911. * request ATAPI sense.
  3912. */
  3913. WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
  3914. if (ata_is_ncq(prot)) {
  3915. WARN_ON(link->sactive & (1 << qc->tag));
  3916. if (!link->sactive)
  3917. ap->nr_active_links++;
  3918. link->sactive |= 1 << qc->tag;
  3919. } else {
  3920. WARN_ON(link->sactive);
  3921. ap->nr_active_links++;
  3922. link->active_tag = qc->tag;
  3923. }
  3924. qc->flags |= ATA_QCFLAG_ACTIVE;
  3925. ap->qc_active |= 1 << qc->tag;
  3926. /* We guarantee to LLDs that they will have at least one
  3927. * non-zero sg if the command is a data command.
  3928. */
  3929. BUG_ON(ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes));
  3930. if (ata_is_dma(prot) || (ata_is_pio(prot) &&
  3931. (ap->flags & ATA_FLAG_PIO_DMA)))
  3932. if (ata_sg_setup(qc))
  3933. goto sg_err;
  3934. /* if device is sleeping, schedule reset and abort the link */
  3935. if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
  3936. link->eh_info.action |= ATA_EH_RESET;
  3937. ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
  3938. ata_link_abort(link);
  3939. return;
  3940. }
  3941. ap->ops->qc_prep(qc);
  3942. qc->err_mask |= ap->ops->qc_issue(qc);
  3943. if (unlikely(qc->err_mask))
  3944. goto err;
  3945. return;
  3946. sg_err:
  3947. qc->err_mask |= AC_ERR_SYSTEM;
  3948. err:
  3949. ata_qc_complete(qc);
  3950. }
  3951. /**
  3952. * sata_scr_valid - test whether SCRs are accessible
  3953. * @link: ATA link to test SCR accessibility for
  3954. *
  3955. * Test whether SCRs are accessible for @link.
  3956. *
  3957. * LOCKING:
  3958. * None.
  3959. *
  3960. * RETURNS:
  3961. * 1 if SCRs are accessible, 0 otherwise.
  3962. */
  3963. int sata_scr_valid(struct ata_link *link)
  3964. {
  3965. struct ata_port *ap = link->ap;
  3966. return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
  3967. }
  3968. /**
  3969. * sata_scr_read - read SCR register of the specified port
  3970. * @link: ATA link to read SCR for
  3971. * @reg: SCR to read
  3972. * @val: Place to store read value
  3973. *
  3974. * Read SCR register @reg of @link into *@val. This function is
  3975. * guaranteed to succeed if @link is ap->link, the cable type of
  3976. * the port is SATA and the port implements ->scr_read.
  3977. *
  3978. * LOCKING:
  3979. * None if @link is ap->link. Kernel thread context otherwise.
  3980. *
  3981. * RETURNS:
  3982. * 0 on success, negative errno on failure.
  3983. */
  3984. int sata_scr_read(struct ata_link *link, int reg, u32 *val)
  3985. {
  3986. if (ata_is_host_link(link)) {
  3987. struct ata_port *ap = link->ap;
  3988. if (sata_scr_valid(link))
  3989. return ap->ops->scr_read(ap, reg, val);
  3990. return -EOPNOTSUPP;
  3991. }
  3992. return sata_pmp_scr_read(link, reg, val);
  3993. }
  3994. /**
  3995. * sata_scr_write - write SCR register of the specified port
  3996. * @link: ATA link to write SCR for
  3997. * @reg: SCR to write
  3998. * @val: value to write
  3999. *
  4000. * Write @val to SCR register @reg of @link. This function is
  4001. * guaranteed to succeed if @link is ap->link, the cable type of
  4002. * the port is SATA and the port implements ->scr_read.
  4003. *
  4004. * LOCKING:
  4005. * None if @link is ap->link. Kernel thread context otherwise.
  4006. *
  4007. * RETURNS:
  4008. * 0 on success, negative errno on failure.
  4009. */
  4010. int sata_scr_write(struct ata_link *link, int reg, u32 val)
  4011. {
  4012. if (ata_is_host_link(link)) {
  4013. struct ata_port *ap = link->ap;
  4014. if (sata_scr_valid(link))
  4015. return ap->ops->scr_write(ap, reg, val);
  4016. return -EOPNOTSUPP;
  4017. }
  4018. return sata_pmp_scr_write(link, reg, val);
  4019. }
  4020. /**
  4021. * sata_scr_write_flush - write SCR register of the specified port and flush
  4022. * @link: ATA link to write SCR for
  4023. * @reg: SCR to write
  4024. * @val: value to write
  4025. *
  4026. * This function is identical to sata_scr_write() except that this
  4027. * function performs flush after writing to the register.
  4028. *
  4029. * LOCKING:
  4030. * None if @link is ap->link. Kernel thread context otherwise.
  4031. *
  4032. * RETURNS:
  4033. * 0 on success, negative errno on failure.
  4034. */
  4035. int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
  4036. {
  4037. if (ata_is_host_link(link)) {
  4038. struct ata_port *ap = link->ap;
  4039. int rc;
  4040. if (sata_scr_valid(link)) {
  4041. rc = ap->ops->scr_write(ap, reg, val);
  4042. if (rc == 0)
  4043. rc = ap->ops->scr_read(ap, reg, &val);
  4044. return rc;
  4045. }
  4046. return -EOPNOTSUPP;
  4047. }
  4048. return sata_pmp_scr_write(link, reg, val);
  4049. }
  4050. /**
  4051. * ata_link_online - test whether the given link is online
  4052. * @link: ATA link to test
  4053. *
  4054. * Test whether @link is online. Note that this function returns
  4055. * 0 if online status of @link cannot be obtained, so
  4056. * ata_link_online(link) != !ata_link_offline(link).
  4057. *
  4058. * LOCKING:
  4059. * None.
  4060. *
  4061. * RETURNS:
  4062. * 1 if the port online status is available and online.
  4063. */
  4064. int ata_link_online(struct ata_link *link)
  4065. {
  4066. u32 sstatus;
  4067. if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
  4068. (sstatus & 0xf) == 0x3)
  4069. return 1;
  4070. return 0;
  4071. }
  4072. /**
  4073. * ata_link_offline - test whether the given link is offline
  4074. * @link: ATA link to test
  4075. *
  4076. * Test whether @link is offline. Note that this function
  4077. * returns 0 if offline status of @link cannot be obtained, so
  4078. * ata_link_online(link) != !ata_link_offline(link).
  4079. *
  4080. * LOCKING:
  4081. * None.
  4082. *
  4083. * RETURNS:
  4084. * 1 if the port offline status is available and offline.
  4085. */
  4086. int ata_link_offline(struct ata_link *link)
  4087. {
  4088. u32 sstatus;
  4089. if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
  4090. (sstatus & 0xf) != 0x3)
  4091. return 1;
  4092. return 0;
  4093. }
  4094. int ata_flush_cache(struct ata_device *dev)
  4095. {
  4096. unsigned int err_mask;
  4097. u8 cmd;
  4098. if (!ata_try_flush_cache(dev))
  4099. return 0;
  4100. if (dev->flags & ATA_DFLAG_FLUSH_EXT)
  4101. cmd = ATA_CMD_FLUSH_EXT;
  4102. else
  4103. cmd = ATA_CMD_FLUSH;
  4104. /* This is wrong. On a failed flush we get back the LBA of the lost
  4105. sector and we should (assuming it wasn't aborted as unknown) issue
  4106. a further flush command to continue the writeback until it
  4107. does not error */
  4108. err_mask = ata_do_simple_cmd(dev, cmd);
  4109. if (err_mask) {
  4110. ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
  4111. return -EIO;
  4112. }
  4113. return 0;
  4114. }
  4115. #ifdef CONFIG_PM
  4116. static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
  4117. unsigned int action, unsigned int ehi_flags,
  4118. int wait)
  4119. {
  4120. unsigned long flags;
  4121. int i, rc;
  4122. for (i = 0; i < host->n_ports; i++) {
  4123. struct ata_port *ap = host->ports[i];
  4124. struct ata_link *link;
  4125. /* Previous resume operation might still be in
  4126. * progress. Wait for PM_PENDING to clear.
  4127. */
  4128. if (ap->pflags & ATA_PFLAG_PM_PENDING) {
  4129. ata_port_wait_eh(ap);
  4130. WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
  4131. }
  4132. /* request PM ops to EH */
  4133. spin_lock_irqsave(ap->lock, flags);
  4134. ap->pm_mesg = mesg;
  4135. if (wait) {
  4136. rc = 0;
  4137. ap->pm_result = &rc;
  4138. }
  4139. ap->pflags |= ATA_PFLAG_PM_PENDING;
  4140. __ata_port_for_each_link(link, ap) {
  4141. link->eh_info.action |= action;
  4142. link->eh_info.flags |= ehi_flags;
  4143. }
  4144. ata_port_schedule_eh(ap);
  4145. spin_unlock_irqrestore(ap->lock, flags);
  4146. /* wait and check result */
  4147. if (wait) {
  4148. ata_port_wait_eh(ap);
  4149. WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
  4150. if (rc)
  4151. return rc;
  4152. }
  4153. }
  4154. return 0;
  4155. }
  4156. /**
  4157. * ata_host_suspend - suspend host
  4158. * @host: host to suspend
  4159. * @mesg: PM message
  4160. *
  4161. * Suspend @host. Actual operation is performed by EH. This
  4162. * function requests EH to perform PM operations and waits for EH
  4163. * to finish.
  4164. *
  4165. * LOCKING:
  4166. * Kernel thread context (may sleep).
  4167. *
  4168. * RETURNS:
  4169. * 0 on success, -errno on failure.
  4170. */
  4171. int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
  4172. {
  4173. int rc;
  4174. /*
  4175. * disable link pm on all ports before requesting
  4176. * any pm activity
  4177. */
  4178. ata_lpm_enable(host);
  4179. rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
  4180. if (rc == 0)
  4181. host->dev->power.power_state = mesg;
  4182. return rc;
  4183. }
  4184. /**
  4185. * ata_host_resume - resume host
  4186. * @host: host to resume
  4187. *
  4188. * Resume @host. Actual operation is performed by EH. This
  4189. * function requests EH to perform PM operations and returns.
  4190. * Note that all resume operations are performed parallely.
  4191. *
  4192. * LOCKING:
  4193. * Kernel thread context (may sleep).
  4194. */
  4195. void ata_host_resume(struct ata_host *host)
  4196. {
  4197. ata_host_request_pm(host, PMSG_ON, ATA_EH_RESET,
  4198. ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
  4199. host->dev->power.power_state = PMSG_ON;
  4200. /* reenable link pm */
  4201. ata_lpm_disable(host);
  4202. }
  4203. #endif
  4204. /**
  4205. * ata_port_start - Set port up for dma.
  4206. * @ap: Port to initialize
  4207. *
  4208. * Called just after data structures for each port are
  4209. * initialized. Allocates space for PRD table.
  4210. *
  4211. * May be used as the port_start() entry in ata_port_operations.
  4212. *
  4213. * LOCKING:
  4214. * Inherited from caller.
  4215. */
  4216. int ata_port_start(struct ata_port *ap)
  4217. {
  4218. struct device *dev = ap->dev;
  4219. ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
  4220. GFP_KERNEL);
  4221. if (!ap->prd)
  4222. return -ENOMEM;
  4223. return 0;
  4224. }
  4225. /**
  4226. * ata_dev_init - Initialize an ata_device structure
  4227. * @dev: Device structure to initialize
  4228. *
  4229. * Initialize @dev in preparation for probing.
  4230. *
  4231. * LOCKING:
  4232. * Inherited from caller.
  4233. */
  4234. void ata_dev_init(struct ata_device *dev)
  4235. {
  4236. struct ata_link *link = dev->link;
  4237. struct ata_port *ap = link->ap;
  4238. unsigned long flags;
  4239. /* SATA spd limit is bound to the first device */
  4240. link->sata_spd_limit = link->hw_sata_spd_limit;
  4241. link->sata_spd = 0;
  4242. /* High bits of dev->flags are used to record warm plug
  4243. * requests which occur asynchronously. Synchronize using
  4244. * host lock.
  4245. */
  4246. spin_lock_irqsave(ap->lock, flags);
  4247. dev->flags &= ~ATA_DFLAG_INIT_MASK;
  4248. dev->horkage = 0;
  4249. spin_unlock_irqrestore(ap->lock, flags);
  4250. memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
  4251. sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
  4252. dev->pio_mask = UINT_MAX;
  4253. dev->mwdma_mask = UINT_MAX;
  4254. dev->udma_mask = UINT_MAX;
  4255. }
  4256. /**
  4257. * ata_link_init - Initialize an ata_link structure
  4258. * @ap: ATA port link is attached to
  4259. * @link: Link structure to initialize
  4260. * @pmp: Port multiplier port number
  4261. *
  4262. * Initialize @link.
  4263. *
  4264. * LOCKING:
  4265. * Kernel thread context (may sleep)
  4266. */
  4267. void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
  4268. {
  4269. int i;
  4270. /* clear everything except for devices */
  4271. memset(link, 0, offsetof(struct ata_link, device[0]));
  4272. link->ap = ap;
  4273. link->pmp = pmp;
  4274. link->active_tag = ATA_TAG_POISON;
  4275. link->hw_sata_spd_limit = UINT_MAX;
  4276. /* can't use iterator, ap isn't initialized yet */
  4277. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  4278. struct ata_device *dev = &link->device[i];
  4279. dev->link = link;
  4280. dev->devno = dev - link->device;
  4281. ata_dev_init(dev);
  4282. }
  4283. }
  4284. /**
  4285. * sata_link_init_spd - Initialize link->sata_spd_limit
  4286. * @link: Link to configure sata_spd_limit for
  4287. *
  4288. * Initialize @link->[hw_]sata_spd_limit to the currently
  4289. * configured value.
  4290. *
  4291. * LOCKING:
  4292. * Kernel thread context (may sleep).
  4293. *
  4294. * RETURNS:
  4295. * 0 on success, -errno on failure.
  4296. */
  4297. int sata_link_init_spd(struct ata_link *link)
  4298. {
  4299. u32 scontrol;
  4300. u8 spd;
  4301. int rc;
  4302. rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
  4303. if (rc)
  4304. return rc;
  4305. spd = (scontrol >> 4) & 0xf;
  4306. if (spd)
  4307. link->hw_sata_spd_limit &= (1 << spd) - 1;
  4308. ata_force_spd_limit(link);
  4309. link->sata_spd_limit = link->hw_sata_spd_limit;
  4310. return 0;
  4311. }
  4312. /**
  4313. * ata_port_alloc - allocate and initialize basic ATA port resources
  4314. * @host: ATA host this allocated port belongs to
  4315. *
  4316. * Allocate and initialize basic ATA port resources.
  4317. *
  4318. * RETURNS:
  4319. * Allocate ATA port on success, NULL on failure.
  4320. *
  4321. * LOCKING:
  4322. * Inherited from calling layer (may sleep).
  4323. */
  4324. struct ata_port *ata_port_alloc(struct ata_host *host)
  4325. {
  4326. struct ata_port *ap;
  4327. DPRINTK("ENTER\n");
  4328. ap = kzalloc(sizeof(*ap), GFP_KERNEL);
  4329. if (!ap)
  4330. return NULL;
  4331. ap->pflags |= ATA_PFLAG_INITIALIZING;
  4332. ap->lock = &host->lock;
  4333. ap->flags = ATA_FLAG_DISABLED;
  4334. ap->print_id = -1;
  4335. ap->ctl = ATA_DEVCTL_OBS;
  4336. ap->host = host;
  4337. ap->dev = host->dev;
  4338. ap->last_ctl = 0xFF;
  4339. #if defined(ATA_VERBOSE_DEBUG)
  4340. /* turn on all debugging levels */
  4341. ap->msg_enable = 0x00FF;
  4342. #elif defined(ATA_DEBUG)
  4343. ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
  4344. #else
  4345. ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
  4346. #endif
  4347. INIT_DELAYED_WORK(&ap->port_task, ata_pio_task);
  4348. INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
  4349. INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
  4350. INIT_LIST_HEAD(&ap->eh_done_q);
  4351. init_waitqueue_head(&ap->eh_wait_q);
  4352. init_timer_deferrable(&ap->fastdrain_timer);
  4353. ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
  4354. ap->fastdrain_timer.data = (unsigned long)ap;
  4355. ap->cbl = ATA_CBL_NONE;
  4356. ata_link_init(ap, &ap->link, 0);
  4357. #ifdef ATA_IRQ_TRAP
  4358. ap->stats.unhandled_irq = 1;
  4359. ap->stats.idle_irq = 1;
  4360. #endif
  4361. return ap;
  4362. }
  4363. static void ata_host_release(struct device *gendev, void *res)
  4364. {
  4365. struct ata_host *host = dev_get_drvdata(gendev);
  4366. int i;
  4367. for (i = 0; i < host->n_ports; i++) {
  4368. struct ata_port *ap = host->ports[i];
  4369. if (!ap)
  4370. continue;
  4371. if (ap->scsi_host)
  4372. scsi_host_put(ap->scsi_host);
  4373. kfree(ap->pmp_link);
  4374. kfree(ap);
  4375. host->ports[i] = NULL;
  4376. }
  4377. dev_set_drvdata(gendev, NULL);
  4378. }
  4379. /**
  4380. * ata_host_alloc - allocate and init basic ATA host resources
  4381. * @dev: generic device this host is associated with
  4382. * @max_ports: maximum number of ATA ports associated with this host
  4383. *
  4384. * Allocate and initialize basic ATA host resources. LLD calls
  4385. * this function to allocate a host, initializes it fully and
  4386. * attaches it using ata_host_register().
  4387. *
  4388. * @max_ports ports are allocated and host->n_ports is
  4389. * initialized to @max_ports. The caller is allowed to decrease
  4390. * host->n_ports before calling ata_host_register(). The unused
  4391. * ports will be automatically freed on registration.
  4392. *
  4393. * RETURNS:
  4394. * Allocate ATA host on success, NULL on failure.
  4395. *
  4396. * LOCKING:
  4397. * Inherited from calling layer (may sleep).
  4398. */
  4399. struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
  4400. {
  4401. struct ata_host *host;
  4402. size_t sz;
  4403. int i;
  4404. DPRINTK("ENTER\n");
  4405. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  4406. return NULL;
  4407. /* alloc a container for our list of ATA ports (buses) */
  4408. sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
  4409. /* alloc a container for our list of ATA ports (buses) */
  4410. host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
  4411. if (!host)
  4412. goto err_out;
  4413. devres_add(dev, host);
  4414. dev_set_drvdata(dev, host);
  4415. spin_lock_init(&host->lock);
  4416. host->dev = dev;
  4417. host->n_ports = max_ports;
  4418. /* allocate ports bound to this host */
  4419. for (i = 0; i < max_ports; i++) {
  4420. struct ata_port *ap;
  4421. ap = ata_port_alloc(host);
  4422. if (!ap)
  4423. goto err_out;
  4424. ap->port_no = i;
  4425. host->ports[i] = ap;
  4426. }
  4427. devres_remove_group(dev, NULL);
  4428. return host;
  4429. err_out:
  4430. devres_release_group(dev, NULL);
  4431. return NULL;
  4432. }
  4433. /**
  4434. * ata_host_alloc_pinfo - alloc host and init with port_info array
  4435. * @dev: generic device this host is associated with
  4436. * @ppi: array of ATA port_info to initialize host with
  4437. * @n_ports: number of ATA ports attached to this host
  4438. *
  4439. * Allocate ATA host and initialize with info from @ppi. If NULL
  4440. * terminated, @ppi may contain fewer entries than @n_ports. The
  4441. * last entry will be used for the remaining ports.
  4442. *
  4443. * RETURNS:
  4444. * Allocate ATA host on success, NULL on failure.
  4445. *
  4446. * LOCKING:
  4447. * Inherited from calling layer (may sleep).
  4448. */
  4449. struct ata_host *ata_host_alloc_pinfo(struct device *dev,
  4450. const struct ata_port_info * const * ppi,
  4451. int n_ports)
  4452. {
  4453. const struct ata_port_info *pi;
  4454. struct ata_host *host;
  4455. int i, j;
  4456. host = ata_host_alloc(dev, n_ports);
  4457. if (!host)
  4458. return NULL;
  4459. for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
  4460. struct ata_port *ap = host->ports[i];
  4461. if (ppi[j])
  4462. pi = ppi[j++];
  4463. ap->pio_mask = pi->pio_mask;
  4464. ap->mwdma_mask = pi->mwdma_mask;
  4465. ap->udma_mask = pi->udma_mask;
  4466. ap->flags |= pi->flags;
  4467. ap->link.flags |= pi->link_flags;
  4468. ap->ops = pi->port_ops;
  4469. if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
  4470. host->ops = pi->port_ops;
  4471. }
  4472. return host;
  4473. }
  4474. static void ata_host_stop(struct device *gendev, void *res)
  4475. {
  4476. struct ata_host *host = dev_get_drvdata(gendev);
  4477. int i;
  4478. WARN_ON(!(host->flags & ATA_HOST_STARTED));
  4479. for (i = 0; i < host->n_ports; i++) {
  4480. struct ata_port *ap = host->ports[i];
  4481. if (ap->ops->port_stop)
  4482. ap->ops->port_stop(ap);
  4483. }
  4484. if (host->ops->host_stop)
  4485. host->ops->host_stop(host);
  4486. }
  4487. /**
  4488. * ata_finalize_port_ops - finalize ata_port_operations
  4489. * @ops: ata_port_operations to finalize
  4490. *
  4491. * An ata_port_operations can inherit from another ops and that
  4492. * ops can again inherit from another. This can go on as many
  4493. * times as necessary as long as there is no loop in the
  4494. * inheritance chain.
  4495. *
  4496. * Ops tables are finalized when the host is started. NULL or
  4497. * unspecified entries are inherited from the closet ancestor
  4498. * which has the method and the entry is populated with it.
  4499. * After finalization, the ops table directly points to all the
  4500. * methods and ->inherits is no longer necessary and cleared.
  4501. *
  4502. * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
  4503. *
  4504. * LOCKING:
  4505. * None.
  4506. */
  4507. static void ata_finalize_port_ops(struct ata_port_operations *ops)
  4508. {
  4509. static spinlock_t lock = SPIN_LOCK_UNLOCKED;
  4510. const struct ata_port_operations *cur;
  4511. void **begin = (void **)ops;
  4512. void **end = (void **)&ops->inherits;
  4513. void **pp;
  4514. if (!ops || !ops->inherits)
  4515. return;
  4516. spin_lock(&lock);
  4517. for (cur = ops->inherits; cur; cur = cur->inherits) {
  4518. void **inherit = (void **)cur;
  4519. for (pp = begin; pp < end; pp++, inherit++)
  4520. if (!*pp)
  4521. *pp = *inherit;
  4522. }
  4523. for (pp = begin; pp < end; pp++)
  4524. if (IS_ERR(*pp))
  4525. *pp = NULL;
  4526. ops->inherits = NULL;
  4527. spin_unlock(&lock);
  4528. }
  4529. /**
  4530. * ata_host_start - start and freeze ports of an ATA host
  4531. * @host: ATA host to start ports for
  4532. *
  4533. * Start and then freeze ports of @host. Started status is
  4534. * recorded in host->flags, so this function can be called
  4535. * multiple times. Ports are guaranteed to get started only
  4536. * once. If host->ops isn't initialized yet, its set to the
  4537. * first non-dummy port ops.
  4538. *
  4539. * LOCKING:
  4540. * Inherited from calling layer (may sleep).
  4541. *
  4542. * RETURNS:
  4543. * 0 if all ports are started successfully, -errno otherwise.
  4544. */
  4545. int ata_host_start(struct ata_host *host)
  4546. {
  4547. int have_stop = 0;
  4548. void *start_dr = NULL;
  4549. int i, rc;
  4550. if (host->flags & ATA_HOST_STARTED)
  4551. return 0;
  4552. ata_finalize_port_ops(host->ops);
  4553. for (i = 0; i < host->n_ports; i++) {
  4554. struct ata_port *ap = host->ports[i];
  4555. ata_finalize_port_ops(ap->ops);
  4556. if (!host->ops && !ata_port_is_dummy(ap))
  4557. host->ops = ap->ops;
  4558. if (ap->ops->port_stop)
  4559. have_stop = 1;
  4560. }
  4561. if (host->ops->host_stop)
  4562. have_stop = 1;
  4563. if (have_stop) {
  4564. start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
  4565. if (!start_dr)
  4566. return -ENOMEM;
  4567. }
  4568. for (i = 0; i < host->n_ports; i++) {
  4569. struct ata_port *ap = host->ports[i];
  4570. if (ap->ops->port_start) {
  4571. rc = ap->ops->port_start(ap);
  4572. if (rc) {
  4573. if (rc != -ENODEV)
  4574. dev_printk(KERN_ERR, host->dev,
  4575. "failed to start port %d "
  4576. "(errno=%d)\n", i, rc);
  4577. goto err_out;
  4578. }
  4579. }
  4580. ata_eh_freeze_port(ap);
  4581. }
  4582. if (start_dr)
  4583. devres_add(host->dev, start_dr);
  4584. host->flags |= ATA_HOST_STARTED;
  4585. return 0;
  4586. err_out:
  4587. while (--i >= 0) {
  4588. struct ata_port *ap = host->ports[i];
  4589. if (ap->ops->port_stop)
  4590. ap->ops->port_stop(ap);
  4591. }
  4592. devres_free(start_dr);
  4593. return rc;
  4594. }
  4595. /**
  4596. * ata_sas_host_init - Initialize a host struct
  4597. * @host: host to initialize
  4598. * @dev: device host is attached to
  4599. * @flags: host flags
  4600. * @ops: port_ops
  4601. *
  4602. * LOCKING:
  4603. * PCI/etc. bus probe sem.
  4604. *
  4605. */
  4606. /* KILLME - the only user left is ipr */
  4607. void ata_host_init(struct ata_host *host, struct device *dev,
  4608. unsigned long flags, struct ata_port_operations *ops)
  4609. {
  4610. spin_lock_init(&host->lock);
  4611. host->dev = dev;
  4612. host->flags = flags;
  4613. host->ops = ops;
  4614. }
  4615. /**
  4616. * ata_host_register - register initialized ATA host
  4617. * @host: ATA host to register
  4618. * @sht: template for SCSI host
  4619. *
  4620. * Register initialized ATA host. @host is allocated using
  4621. * ata_host_alloc() and fully initialized by LLD. This function
  4622. * starts ports, registers @host with ATA and SCSI layers and
  4623. * probe registered devices.
  4624. *
  4625. * LOCKING:
  4626. * Inherited from calling layer (may sleep).
  4627. *
  4628. * RETURNS:
  4629. * 0 on success, -errno otherwise.
  4630. */
  4631. int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
  4632. {
  4633. int i, rc;
  4634. /* host must have been started */
  4635. if (!(host->flags & ATA_HOST_STARTED)) {
  4636. dev_printk(KERN_ERR, host->dev,
  4637. "BUG: trying to register unstarted host\n");
  4638. WARN_ON(1);
  4639. return -EINVAL;
  4640. }
  4641. /* Blow away unused ports. This happens when LLD can't
  4642. * determine the exact number of ports to allocate at
  4643. * allocation time.
  4644. */
  4645. for (i = host->n_ports; host->ports[i]; i++)
  4646. kfree(host->ports[i]);
  4647. /* give ports names and add SCSI hosts */
  4648. for (i = 0; i < host->n_ports; i++)
  4649. host->ports[i]->print_id = ata_print_id++;
  4650. rc = ata_scsi_add_hosts(host, sht);
  4651. if (rc)
  4652. return rc;
  4653. /* associate with ACPI nodes */
  4654. ata_acpi_associate(host);
  4655. /* set cable, sata_spd_limit and report */
  4656. for (i = 0; i < host->n_ports; i++) {
  4657. struct ata_port *ap = host->ports[i];
  4658. unsigned long xfer_mask;
  4659. /* set SATA cable type if still unset */
  4660. if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
  4661. ap->cbl = ATA_CBL_SATA;
  4662. /* init sata_spd_limit to the current value */
  4663. sata_link_init_spd(&ap->link);
  4664. /* print per-port info to dmesg */
  4665. xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
  4666. ap->udma_mask);
  4667. if (!ata_port_is_dummy(ap)) {
  4668. ata_port_printk(ap, KERN_INFO,
  4669. "%cATA max %s %s\n",
  4670. (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
  4671. ata_mode_string(xfer_mask),
  4672. ap->link.eh_info.desc);
  4673. ata_ehi_clear_desc(&ap->link.eh_info);
  4674. } else
  4675. ata_port_printk(ap, KERN_INFO, "DUMMY\n");
  4676. }
  4677. /* perform each probe synchronously */
  4678. DPRINTK("probe begin\n");
  4679. for (i = 0; i < host->n_ports; i++) {
  4680. struct ata_port *ap = host->ports[i];
  4681. /* probe */
  4682. if (ap->ops->error_handler) {
  4683. struct ata_eh_info *ehi = &ap->link.eh_info;
  4684. unsigned long flags;
  4685. ata_port_probe(ap);
  4686. /* kick EH for boot probing */
  4687. spin_lock_irqsave(ap->lock, flags);
  4688. ehi->probe_mask |= ATA_ALL_DEVICES;
  4689. ehi->action |= ATA_EH_RESET;
  4690. ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
  4691. ap->pflags &= ~ATA_PFLAG_INITIALIZING;
  4692. ap->pflags |= ATA_PFLAG_LOADING;
  4693. ata_port_schedule_eh(ap);
  4694. spin_unlock_irqrestore(ap->lock, flags);
  4695. /* wait for EH to finish */
  4696. ata_port_wait_eh(ap);
  4697. } else {
  4698. DPRINTK("ata%u: bus probe begin\n", ap->print_id);
  4699. rc = ata_bus_probe(ap);
  4700. DPRINTK("ata%u: bus probe end\n", ap->print_id);
  4701. if (rc) {
  4702. /* FIXME: do something useful here?
  4703. * Current libata behavior will
  4704. * tear down everything when
  4705. * the module is removed
  4706. * or the h/w is unplugged.
  4707. */
  4708. }
  4709. }
  4710. }
  4711. /* probes are done, now scan each port's disk(s) */
  4712. DPRINTK("host probe begin\n");
  4713. for (i = 0; i < host->n_ports; i++) {
  4714. struct ata_port *ap = host->ports[i];
  4715. ata_scsi_scan_host(ap, 1);
  4716. ata_lpm_schedule(ap, ap->pm_policy);
  4717. }
  4718. return 0;
  4719. }
  4720. /**
  4721. * ata_host_activate - start host, request IRQ and register it
  4722. * @host: target ATA host
  4723. * @irq: IRQ to request
  4724. * @irq_handler: irq_handler used when requesting IRQ
  4725. * @irq_flags: irq_flags used when requesting IRQ
  4726. * @sht: scsi_host_template to use when registering the host
  4727. *
  4728. * After allocating an ATA host and initializing it, most libata
  4729. * LLDs perform three steps to activate the host - start host,
  4730. * request IRQ and register it. This helper takes necessasry
  4731. * arguments and performs the three steps in one go.
  4732. *
  4733. * An invalid IRQ skips the IRQ registration and expects the host to
  4734. * have set polling mode on the port. In this case, @irq_handler
  4735. * should be NULL.
  4736. *
  4737. * LOCKING:
  4738. * Inherited from calling layer (may sleep).
  4739. *
  4740. * RETURNS:
  4741. * 0 on success, -errno otherwise.
  4742. */
  4743. int ata_host_activate(struct ata_host *host, int irq,
  4744. irq_handler_t irq_handler, unsigned long irq_flags,
  4745. struct scsi_host_template *sht)
  4746. {
  4747. int i, rc;
  4748. rc = ata_host_start(host);
  4749. if (rc)
  4750. return rc;
  4751. /* Special case for polling mode */
  4752. if (!irq) {
  4753. WARN_ON(irq_handler);
  4754. return ata_host_register(host, sht);
  4755. }
  4756. rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
  4757. dev_driver_string(host->dev), host);
  4758. if (rc)
  4759. return rc;
  4760. for (i = 0; i < host->n_ports; i++)
  4761. ata_port_desc(host->ports[i], "irq %d", irq);
  4762. rc = ata_host_register(host, sht);
  4763. /* if failed, just free the IRQ and leave ports alone */
  4764. if (rc)
  4765. devm_free_irq(host->dev, irq, host);
  4766. return rc;
  4767. }
  4768. /**
  4769. * ata_port_detach - Detach ATA port in prepration of device removal
  4770. * @ap: ATA port to be detached
  4771. *
  4772. * Detach all ATA devices and the associated SCSI devices of @ap;
  4773. * then, remove the associated SCSI host. @ap is guaranteed to
  4774. * be quiescent on return from this function.
  4775. *
  4776. * LOCKING:
  4777. * Kernel thread context (may sleep).
  4778. */
  4779. static void ata_port_detach(struct ata_port *ap)
  4780. {
  4781. unsigned long flags;
  4782. struct ata_link *link;
  4783. struct ata_device *dev;
  4784. if (!ap->ops->error_handler)
  4785. goto skip_eh;
  4786. /* tell EH we're leaving & flush EH */
  4787. spin_lock_irqsave(ap->lock, flags);
  4788. ap->pflags |= ATA_PFLAG_UNLOADING;
  4789. spin_unlock_irqrestore(ap->lock, flags);
  4790. ata_port_wait_eh(ap);
  4791. /* EH is now guaranteed to see UNLOADING - EH context belongs
  4792. * to us. Disable all existing devices.
  4793. */
  4794. ata_port_for_each_link(link, ap) {
  4795. ata_link_for_each_dev(dev, link)
  4796. ata_dev_disable(dev);
  4797. }
  4798. /* Final freeze & EH. All in-flight commands are aborted. EH
  4799. * will be skipped and retrials will be terminated with bad
  4800. * target.
  4801. */
  4802. spin_lock_irqsave(ap->lock, flags);
  4803. ata_port_freeze(ap); /* won't be thawed */
  4804. spin_unlock_irqrestore(ap->lock, flags);
  4805. ata_port_wait_eh(ap);
  4806. cancel_rearming_delayed_work(&ap->hotplug_task);
  4807. skip_eh:
  4808. /* remove the associated SCSI host */
  4809. scsi_remove_host(ap->scsi_host);
  4810. }
  4811. /**
  4812. * ata_host_detach - Detach all ports of an ATA host
  4813. * @host: Host to detach
  4814. *
  4815. * Detach all ports of @host.
  4816. *
  4817. * LOCKING:
  4818. * Kernel thread context (may sleep).
  4819. */
  4820. void ata_host_detach(struct ata_host *host)
  4821. {
  4822. int i;
  4823. for (i = 0; i < host->n_ports; i++)
  4824. ata_port_detach(host->ports[i]);
  4825. /* the host is dead now, dissociate ACPI */
  4826. ata_acpi_dissociate(host);
  4827. }
  4828. #ifdef CONFIG_PCI
  4829. /**
  4830. * ata_pci_remove_one - PCI layer callback for device removal
  4831. * @pdev: PCI device that was removed
  4832. *
  4833. * PCI layer indicates to libata via this hook that hot-unplug or
  4834. * module unload event has occurred. Detach all ports. Resource
  4835. * release is handled via devres.
  4836. *
  4837. * LOCKING:
  4838. * Inherited from PCI layer (may sleep).
  4839. */
  4840. void ata_pci_remove_one(struct pci_dev *pdev)
  4841. {
  4842. struct device *dev = &pdev->dev;
  4843. struct ata_host *host = dev_get_drvdata(dev);
  4844. ata_host_detach(host);
  4845. }
  4846. /* move to PCI subsystem */
  4847. int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
  4848. {
  4849. unsigned long tmp = 0;
  4850. switch (bits->width) {
  4851. case 1: {
  4852. u8 tmp8 = 0;
  4853. pci_read_config_byte(pdev, bits->reg, &tmp8);
  4854. tmp = tmp8;
  4855. break;
  4856. }
  4857. case 2: {
  4858. u16 tmp16 = 0;
  4859. pci_read_config_word(pdev, bits->reg, &tmp16);
  4860. tmp = tmp16;
  4861. break;
  4862. }
  4863. case 4: {
  4864. u32 tmp32 = 0;
  4865. pci_read_config_dword(pdev, bits->reg, &tmp32);
  4866. tmp = tmp32;
  4867. break;
  4868. }
  4869. default:
  4870. return -EINVAL;
  4871. }
  4872. tmp &= bits->mask;
  4873. return (tmp == bits->val) ? 1 : 0;
  4874. }
  4875. #ifdef CONFIG_PM
  4876. void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
  4877. {
  4878. pci_save_state(pdev);
  4879. pci_disable_device(pdev);
  4880. if (mesg.event & PM_EVENT_SLEEP)
  4881. pci_set_power_state(pdev, PCI_D3hot);
  4882. }
  4883. int ata_pci_device_do_resume(struct pci_dev *pdev)
  4884. {
  4885. int rc;
  4886. pci_set_power_state(pdev, PCI_D0);
  4887. pci_restore_state(pdev);
  4888. rc = pcim_enable_device(pdev);
  4889. if (rc) {
  4890. dev_printk(KERN_ERR, &pdev->dev,
  4891. "failed to enable device after resume (%d)\n", rc);
  4892. return rc;
  4893. }
  4894. pci_set_master(pdev);
  4895. return 0;
  4896. }
  4897. int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
  4898. {
  4899. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  4900. int rc = 0;
  4901. rc = ata_host_suspend(host, mesg);
  4902. if (rc)
  4903. return rc;
  4904. ata_pci_device_do_suspend(pdev, mesg);
  4905. return 0;
  4906. }
  4907. int ata_pci_device_resume(struct pci_dev *pdev)
  4908. {
  4909. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  4910. int rc;
  4911. rc = ata_pci_device_do_resume(pdev);
  4912. if (rc == 0)
  4913. ata_host_resume(host);
  4914. return rc;
  4915. }
  4916. #endif /* CONFIG_PM */
  4917. #endif /* CONFIG_PCI */
  4918. static int __init ata_parse_force_one(char **cur,
  4919. struct ata_force_ent *force_ent,
  4920. const char **reason)
  4921. {
  4922. /* FIXME: Currently, there's no way to tag init const data and
  4923. * using __initdata causes build failure on some versions of
  4924. * gcc. Once __initdataconst is implemented, add const to the
  4925. * following structure.
  4926. */
  4927. static struct ata_force_param force_tbl[] __initdata = {
  4928. { "40c", .cbl = ATA_CBL_PATA40 },
  4929. { "80c", .cbl = ATA_CBL_PATA80 },
  4930. { "short40c", .cbl = ATA_CBL_PATA40_SHORT },
  4931. { "unk", .cbl = ATA_CBL_PATA_UNK },
  4932. { "ign", .cbl = ATA_CBL_PATA_IGN },
  4933. { "sata", .cbl = ATA_CBL_SATA },
  4934. { "1.5Gbps", .spd_limit = 1 },
  4935. { "3.0Gbps", .spd_limit = 2 },
  4936. { "noncq", .horkage_on = ATA_HORKAGE_NONCQ },
  4937. { "ncq", .horkage_off = ATA_HORKAGE_NONCQ },
  4938. { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) },
  4939. { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) },
  4940. { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) },
  4941. { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) },
  4942. { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) },
  4943. { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) },
  4944. { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) },
  4945. { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) },
  4946. { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) },
  4947. { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) },
  4948. { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) },
  4949. { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) },
  4950. { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
  4951. { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
  4952. { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
  4953. { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
  4954. { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
  4955. { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
  4956. { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
  4957. { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
  4958. { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
  4959. { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
  4960. { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
  4961. { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
  4962. { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
  4963. { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
  4964. { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
  4965. { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
  4966. { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
  4967. { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
  4968. { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
  4969. { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
  4970. { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
  4971. { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) },
  4972. };
  4973. char *start = *cur, *p = *cur;
  4974. char *id, *val, *endp;
  4975. const struct ata_force_param *match_fp = NULL;
  4976. int nr_matches = 0, i;
  4977. /* find where this param ends and update *cur */
  4978. while (*p != '\0' && *p != ',')
  4979. p++;
  4980. if (*p == '\0')
  4981. *cur = p;
  4982. else
  4983. *cur = p + 1;
  4984. *p = '\0';
  4985. /* parse */
  4986. p = strchr(start, ':');
  4987. if (!p) {
  4988. val = strstrip(start);
  4989. goto parse_val;
  4990. }
  4991. *p = '\0';
  4992. id = strstrip(start);
  4993. val = strstrip(p + 1);
  4994. /* parse id */
  4995. p = strchr(id, '.');
  4996. if (p) {
  4997. *p++ = '\0';
  4998. force_ent->device = simple_strtoul(p, &endp, 10);
  4999. if (p == endp || *endp != '\0') {
  5000. *reason = "invalid device";
  5001. return -EINVAL;
  5002. }
  5003. }
  5004. force_ent->port = simple_strtoul(id, &endp, 10);
  5005. if (p == endp || *endp != '\0') {
  5006. *reason = "invalid port/link";
  5007. return -EINVAL;
  5008. }
  5009. parse_val:
  5010. /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
  5011. for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
  5012. const struct ata_force_param *fp = &force_tbl[i];
  5013. if (strncasecmp(val, fp->name, strlen(val)))
  5014. continue;
  5015. nr_matches++;
  5016. match_fp = fp;
  5017. if (strcasecmp(val, fp->name) == 0) {
  5018. nr_matches = 1;
  5019. break;
  5020. }
  5021. }
  5022. if (!nr_matches) {
  5023. *reason = "unknown value";
  5024. return -EINVAL;
  5025. }
  5026. if (nr_matches > 1) {
  5027. *reason = "ambigious value";
  5028. return -EINVAL;
  5029. }
  5030. force_ent->param = *match_fp;
  5031. return 0;
  5032. }
  5033. static void __init ata_parse_force_param(void)
  5034. {
  5035. int idx = 0, size = 1;
  5036. int last_port = -1, last_device = -1;
  5037. char *p, *cur, *next;
  5038. /* calculate maximum number of params and allocate force_tbl */
  5039. for (p = ata_force_param_buf; *p; p++)
  5040. if (*p == ',')
  5041. size++;
  5042. ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
  5043. if (!ata_force_tbl) {
  5044. printk(KERN_WARNING "ata: failed to extend force table, "
  5045. "libata.force ignored\n");
  5046. return;
  5047. }
  5048. /* parse and populate the table */
  5049. for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
  5050. const char *reason = "";
  5051. struct ata_force_ent te = { .port = -1, .device = -1 };
  5052. next = cur;
  5053. if (ata_parse_force_one(&next, &te, &reason)) {
  5054. printk(KERN_WARNING "ata: failed to parse force "
  5055. "parameter \"%s\" (%s)\n",
  5056. cur, reason);
  5057. continue;
  5058. }
  5059. if (te.port == -1) {
  5060. te.port = last_port;
  5061. te.device = last_device;
  5062. }
  5063. ata_force_tbl[idx++] = te;
  5064. last_port = te.port;
  5065. last_device = te.device;
  5066. }
  5067. ata_force_tbl_size = idx;
  5068. }
  5069. static int __init ata_init(void)
  5070. {
  5071. ata_probe_timeout *= HZ;
  5072. ata_parse_force_param();
  5073. ata_wq = create_workqueue("ata");
  5074. if (!ata_wq)
  5075. return -ENOMEM;
  5076. ata_aux_wq = create_singlethread_workqueue("ata_aux");
  5077. if (!ata_aux_wq) {
  5078. destroy_workqueue(ata_wq);
  5079. return -ENOMEM;
  5080. }
  5081. printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
  5082. return 0;
  5083. }
  5084. static void __exit ata_exit(void)
  5085. {
  5086. kfree(ata_force_tbl);
  5087. destroy_workqueue(ata_wq);
  5088. destroy_workqueue(ata_aux_wq);
  5089. }
  5090. subsys_initcall(ata_init);
  5091. module_exit(ata_exit);
  5092. static unsigned long ratelimit_time;
  5093. static DEFINE_SPINLOCK(ata_ratelimit_lock);
  5094. int ata_ratelimit(void)
  5095. {
  5096. int rc;
  5097. unsigned long flags;
  5098. spin_lock_irqsave(&ata_ratelimit_lock, flags);
  5099. if (time_after(jiffies, ratelimit_time)) {
  5100. rc = 1;
  5101. ratelimit_time = jiffies + (HZ/5);
  5102. } else
  5103. rc = 0;
  5104. spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
  5105. return rc;
  5106. }
  5107. /**
  5108. * ata_wait_register - wait until register value changes
  5109. * @reg: IO-mapped register
  5110. * @mask: Mask to apply to read register value
  5111. * @val: Wait condition
  5112. * @interval_msec: polling interval in milliseconds
  5113. * @timeout_msec: timeout in milliseconds
  5114. *
  5115. * Waiting for some bits of register to change is a common
  5116. * operation for ATA controllers. This function reads 32bit LE
  5117. * IO-mapped register @reg and tests for the following condition.
  5118. *
  5119. * (*@reg & mask) != val
  5120. *
  5121. * If the condition is met, it returns; otherwise, the process is
  5122. * repeated after @interval_msec until timeout.
  5123. *
  5124. * LOCKING:
  5125. * Kernel thread context (may sleep)
  5126. *
  5127. * RETURNS:
  5128. * The final register value.
  5129. */
  5130. u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
  5131. unsigned long interval_msec,
  5132. unsigned long timeout_msec)
  5133. {
  5134. unsigned long timeout;
  5135. u32 tmp;
  5136. tmp = ioread32(reg);
  5137. /* Calculate timeout _after_ the first read to make sure
  5138. * preceding writes reach the controller before starting to
  5139. * eat away the timeout.
  5140. */
  5141. timeout = jiffies + (timeout_msec * HZ) / 1000;
  5142. while ((tmp & mask) == val && time_before(jiffies, timeout)) {
  5143. msleep(interval_msec);
  5144. tmp = ioread32(reg);
  5145. }
  5146. return tmp;
  5147. }
  5148. /*
  5149. * Dummy port_ops
  5150. */
  5151. static void ata_dummy_noret(struct ata_port *ap) { }
  5152. static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
  5153. static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
  5154. static u8 ata_dummy_check_status(struct ata_port *ap)
  5155. {
  5156. return ATA_DRDY;
  5157. }
  5158. static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
  5159. {
  5160. return AC_ERR_SYSTEM;
  5161. }
  5162. struct ata_port_operations ata_dummy_port_ops = {
  5163. .check_status = ata_dummy_check_status,
  5164. .check_altstatus = ata_dummy_check_status,
  5165. .dev_select = ata_noop_dev_select,
  5166. .qc_prep = ata_noop_qc_prep,
  5167. .qc_issue = ata_dummy_qc_issue,
  5168. .freeze = ata_dummy_noret,
  5169. .thaw = ata_dummy_noret,
  5170. .error_handler = ata_dummy_noret,
  5171. .post_internal_cmd = ata_dummy_qc_noret,
  5172. .irq_clear = ata_dummy_noret,
  5173. .port_start = ata_dummy_ret0,
  5174. .port_stop = ata_dummy_noret,
  5175. };
  5176. const struct ata_port_info ata_dummy_port_info = {
  5177. .port_ops = &ata_dummy_port_ops,
  5178. };
  5179. /*
  5180. * libata is essentially a library of internal helper functions for
  5181. * low-level ATA host controller drivers. As such, the API/ABI is
  5182. * likely to change as new drivers are added and updated.
  5183. * Do not depend on ABI/API stability.
  5184. */
  5185. EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
  5186. EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
  5187. EXPORT_SYMBOL_GPL(sata_deb_timing_long);
  5188. EXPORT_SYMBOL_GPL(ata_base_port_ops);
  5189. EXPORT_SYMBOL_GPL(sata_port_ops);
  5190. EXPORT_SYMBOL_GPL(sata_pmp_port_ops);
  5191. EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
  5192. EXPORT_SYMBOL_GPL(ata_dummy_port_info);
  5193. EXPORT_SYMBOL_GPL(ata_std_bios_param);
  5194. EXPORT_SYMBOL_GPL(ata_host_init);
  5195. EXPORT_SYMBOL_GPL(ata_host_alloc);
  5196. EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
  5197. EXPORT_SYMBOL_GPL(ata_host_start);
  5198. EXPORT_SYMBOL_GPL(ata_host_register);
  5199. EXPORT_SYMBOL_GPL(ata_host_activate);
  5200. EXPORT_SYMBOL_GPL(ata_host_detach);
  5201. EXPORT_SYMBOL_GPL(ata_sg_init);
  5202. EXPORT_SYMBOL_GPL(ata_qc_complete);
  5203. EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
  5204. EXPORT_SYMBOL_GPL(ata_noop_dev_select);
  5205. EXPORT_SYMBOL_GPL(sata_print_link_status);
  5206. EXPORT_SYMBOL_GPL(atapi_cmd_type);
  5207. EXPORT_SYMBOL_GPL(ata_tf_to_fis);
  5208. EXPORT_SYMBOL_GPL(ata_tf_from_fis);
  5209. EXPORT_SYMBOL_GPL(ata_pack_xfermask);
  5210. EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
  5211. EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
  5212. EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
  5213. EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
  5214. EXPORT_SYMBOL_GPL(ata_mode_string);
  5215. EXPORT_SYMBOL_GPL(ata_id_xfermask);
  5216. EXPORT_SYMBOL_GPL(ata_port_start);
  5217. EXPORT_SYMBOL_GPL(ata_do_set_mode);
  5218. EXPORT_SYMBOL_GPL(ata_std_qc_defer);
  5219. EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
  5220. EXPORT_SYMBOL_GPL(ata_noop_irq_clear);
  5221. EXPORT_SYMBOL_GPL(ata_port_probe);
  5222. EXPORT_SYMBOL_GPL(ata_dev_disable);
  5223. EXPORT_SYMBOL_GPL(sata_set_spd);
  5224. EXPORT_SYMBOL_GPL(sata_link_debounce);
  5225. EXPORT_SYMBOL_GPL(sata_link_resume);
  5226. EXPORT_SYMBOL_GPL(sata_link_hardreset);
  5227. EXPORT_SYMBOL_GPL(ata_dev_classify);
  5228. EXPORT_SYMBOL_GPL(ata_dev_pair);
  5229. EXPORT_SYMBOL_GPL(ata_port_disable);
  5230. EXPORT_SYMBOL_GPL(ata_ratelimit);
  5231. EXPORT_SYMBOL_GPL(ata_wait_register);
  5232. EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
  5233. EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
  5234. EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
  5235. EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
  5236. EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
  5237. EXPORT_SYMBOL_GPL(sata_scr_valid);
  5238. EXPORT_SYMBOL_GPL(sata_scr_read);
  5239. EXPORT_SYMBOL_GPL(sata_scr_write);
  5240. EXPORT_SYMBOL_GPL(sata_scr_write_flush);
  5241. EXPORT_SYMBOL_GPL(ata_link_online);
  5242. EXPORT_SYMBOL_GPL(ata_link_offline);
  5243. #ifdef CONFIG_PM
  5244. EXPORT_SYMBOL_GPL(ata_host_suspend);
  5245. EXPORT_SYMBOL_GPL(ata_host_resume);
  5246. #endif /* CONFIG_PM */
  5247. EXPORT_SYMBOL_GPL(ata_id_string);
  5248. EXPORT_SYMBOL_GPL(ata_id_c_string);
  5249. EXPORT_SYMBOL_GPL(ata_scsi_simulate);
  5250. EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
  5251. EXPORT_SYMBOL_GPL(ata_timing_find_mode);
  5252. EXPORT_SYMBOL_GPL(ata_timing_compute);
  5253. EXPORT_SYMBOL_GPL(ata_timing_merge);
  5254. EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
  5255. #ifdef CONFIG_PCI
  5256. EXPORT_SYMBOL_GPL(pci_test_config_bits);
  5257. EXPORT_SYMBOL_GPL(ata_pci_remove_one);
  5258. #ifdef CONFIG_PM
  5259. EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
  5260. EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
  5261. EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
  5262. EXPORT_SYMBOL_GPL(ata_pci_device_resume);
  5263. #endif /* CONFIG_PM */
  5264. #endif /* CONFIG_PCI */
  5265. EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch);
  5266. EXPORT_SYMBOL_GPL(sata_pmp_std_prereset);
  5267. EXPORT_SYMBOL_GPL(sata_pmp_std_hardreset);
  5268. EXPORT_SYMBOL_GPL(sata_pmp_std_postreset);
  5269. EXPORT_SYMBOL_GPL(sata_pmp_error_handler);
  5270. EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
  5271. EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
  5272. EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
  5273. EXPORT_SYMBOL_GPL(ata_port_desc);
  5274. #ifdef CONFIG_PCI
  5275. EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
  5276. #endif /* CONFIG_PCI */
  5277. EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
  5278. EXPORT_SYMBOL_GPL(ata_link_abort);
  5279. EXPORT_SYMBOL_GPL(ata_port_abort);
  5280. EXPORT_SYMBOL_GPL(ata_port_freeze);
  5281. EXPORT_SYMBOL_GPL(sata_async_notification);
  5282. EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
  5283. EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
  5284. EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
  5285. EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
  5286. EXPORT_SYMBOL_GPL(ata_do_eh);
  5287. EXPORT_SYMBOL_GPL(ata_std_error_handler);
  5288. EXPORT_SYMBOL_GPL(ata_cable_40wire);
  5289. EXPORT_SYMBOL_GPL(ata_cable_80wire);
  5290. EXPORT_SYMBOL_GPL(ata_cable_unknown);
  5291. EXPORT_SYMBOL_GPL(ata_cable_ignore);
  5292. EXPORT_SYMBOL_GPL(ata_cable_sata);