libata-core.c 170 KB

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