libata-core.c 170 KB

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