libata-core.c 177 KB

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