libata-core.c 170 KB

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