libata-core.c 165 KB

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