libata-core.c 197 KB

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