libata-core.c 176 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146
  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. /* After the identify sequence we can now set up the devices. We do
  1875. this in the normal order so that the user doesn't get confused */
  1876. ata_link_for_each_dev(dev, &ap->link) {
  1877. if (!ata_dev_enabled(dev))
  1878. continue;
  1879. ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
  1880. rc = ata_dev_configure(dev);
  1881. ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
  1882. if (rc)
  1883. goto fail;
  1884. }
  1885. /* configure transfer mode */
  1886. rc = ata_set_mode(&ap->link, &dev);
  1887. if (rc)
  1888. goto fail;
  1889. ata_link_for_each_dev(dev, &ap->link)
  1890. if (ata_dev_enabled(dev))
  1891. return 0;
  1892. /* no device present, disable port */
  1893. ata_port_disable(ap);
  1894. ap->ops->port_disable(ap);
  1895. return -ENODEV;
  1896. fail:
  1897. tries[dev->devno]--;
  1898. switch (rc) {
  1899. case -EINVAL:
  1900. /* eeek, something went very wrong, give up */
  1901. tries[dev->devno] = 0;
  1902. break;
  1903. case -ENODEV:
  1904. /* give it just one more chance */
  1905. tries[dev->devno] = min(tries[dev->devno], 1);
  1906. case -EIO:
  1907. if (tries[dev->devno] == 1) {
  1908. /* This is the last chance, better to slow
  1909. * down than lose it.
  1910. */
  1911. sata_down_spd_limit(&ap->link);
  1912. ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
  1913. }
  1914. }
  1915. if (!tries[dev->devno])
  1916. ata_dev_disable(dev);
  1917. goto retry;
  1918. }
  1919. /**
  1920. * ata_port_probe - Mark port as enabled
  1921. * @ap: Port for which we indicate enablement
  1922. *
  1923. * Modify @ap data structure such that the system
  1924. * thinks that the entire port is enabled.
  1925. *
  1926. * LOCKING: host lock, or some other form of
  1927. * serialization.
  1928. */
  1929. void ata_port_probe(struct ata_port *ap)
  1930. {
  1931. ap->flags &= ~ATA_FLAG_DISABLED;
  1932. }
  1933. /**
  1934. * sata_print_link_status - Print SATA link status
  1935. * @link: SATA link to printk link status about
  1936. *
  1937. * This function prints link speed and status of a SATA link.
  1938. *
  1939. * LOCKING:
  1940. * None.
  1941. */
  1942. void sata_print_link_status(struct ata_link *link)
  1943. {
  1944. u32 sstatus, scontrol, tmp;
  1945. if (sata_scr_read(link, SCR_STATUS, &sstatus))
  1946. return;
  1947. sata_scr_read(link, SCR_CONTROL, &scontrol);
  1948. if (ata_link_online(link)) {
  1949. tmp = (sstatus >> 4) & 0xf;
  1950. ata_link_printk(link, KERN_INFO,
  1951. "SATA link up %s (SStatus %X SControl %X)\n",
  1952. sata_spd_string(tmp), sstatus, scontrol);
  1953. } else {
  1954. ata_link_printk(link, KERN_INFO,
  1955. "SATA link down (SStatus %X SControl %X)\n",
  1956. sstatus, scontrol);
  1957. }
  1958. }
  1959. /**
  1960. * __sata_phy_reset - Wake/reset a low-level SATA PHY
  1961. * @ap: SATA port associated with target SATA PHY.
  1962. *
  1963. * This function issues commands to standard SATA Sxxx
  1964. * PHY registers, to wake up the phy (and device), and
  1965. * clear any reset condition.
  1966. *
  1967. * LOCKING:
  1968. * PCI/etc. bus probe sem.
  1969. *
  1970. */
  1971. void __sata_phy_reset(struct ata_port *ap)
  1972. {
  1973. struct ata_link *link = &ap->link;
  1974. unsigned long timeout = jiffies + (HZ * 5);
  1975. u32 sstatus;
  1976. if (ap->flags & ATA_FLAG_SATA_RESET) {
  1977. /* issue phy wake/reset */
  1978. sata_scr_write_flush(link, SCR_CONTROL, 0x301);
  1979. /* Couldn't find anything in SATA I/II specs, but
  1980. * AHCI-1.1 10.4.2 says at least 1 ms. */
  1981. mdelay(1);
  1982. }
  1983. /* phy wake/clear reset */
  1984. sata_scr_write_flush(link, SCR_CONTROL, 0x300);
  1985. /* wait for phy to become ready, if necessary */
  1986. do {
  1987. msleep(200);
  1988. sata_scr_read(link, SCR_STATUS, &sstatus);
  1989. if ((sstatus & 0xf) != 1)
  1990. break;
  1991. } while (time_before(jiffies, timeout));
  1992. /* print link status */
  1993. sata_print_link_status(link);
  1994. /* TODO: phy layer with polling, timeouts, etc. */
  1995. if (!ata_link_offline(link))
  1996. ata_port_probe(ap);
  1997. else
  1998. ata_port_disable(ap);
  1999. if (ap->flags & ATA_FLAG_DISABLED)
  2000. return;
  2001. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  2002. ata_port_disable(ap);
  2003. return;
  2004. }
  2005. ap->cbl = ATA_CBL_SATA;
  2006. }
  2007. /**
  2008. * sata_phy_reset - Reset SATA bus.
  2009. * @ap: SATA port associated with target SATA PHY.
  2010. *
  2011. * This function resets the SATA bus, and then probes
  2012. * the bus for devices.
  2013. *
  2014. * LOCKING:
  2015. * PCI/etc. bus probe sem.
  2016. *
  2017. */
  2018. void sata_phy_reset(struct ata_port *ap)
  2019. {
  2020. __sata_phy_reset(ap);
  2021. if (ap->flags & ATA_FLAG_DISABLED)
  2022. return;
  2023. ata_bus_reset(ap);
  2024. }
  2025. /**
  2026. * ata_dev_pair - return other device on cable
  2027. * @adev: device
  2028. *
  2029. * Obtain the other device on the same cable, or if none is
  2030. * present NULL is returned
  2031. */
  2032. struct ata_device *ata_dev_pair(struct ata_device *adev)
  2033. {
  2034. struct ata_link *link = adev->link;
  2035. struct ata_device *pair = &link->device[1 - adev->devno];
  2036. if (!ata_dev_enabled(pair))
  2037. return NULL;
  2038. return pair;
  2039. }
  2040. /**
  2041. * ata_port_disable - Disable port.
  2042. * @ap: Port to be disabled.
  2043. *
  2044. * Modify @ap data structure such that the system
  2045. * thinks that the entire port is disabled, and should
  2046. * never attempt to probe or communicate with devices
  2047. * on this port.
  2048. *
  2049. * LOCKING: host lock, or some other form of
  2050. * serialization.
  2051. */
  2052. void ata_port_disable(struct ata_port *ap)
  2053. {
  2054. ap->link.device[0].class = ATA_DEV_NONE;
  2055. ap->link.device[1].class = ATA_DEV_NONE;
  2056. ap->flags |= ATA_FLAG_DISABLED;
  2057. }
  2058. /**
  2059. * sata_down_spd_limit - adjust SATA spd limit downward
  2060. * @link: Link to adjust SATA spd limit for
  2061. *
  2062. * Adjust SATA spd limit of @link downward. Note that this
  2063. * function only adjusts the limit. The change must be applied
  2064. * using sata_set_spd().
  2065. *
  2066. * LOCKING:
  2067. * Inherited from caller.
  2068. *
  2069. * RETURNS:
  2070. * 0 on success, negative errno on failure
  2071. */
  2072. int sata_down_spd_limit(struct ata_link *link)
  2073. {
  2074. u32 sstatus, spd, mask;
  2075. int rc, highbit;
  2076. if (!sata_scr_valid(link))
  2077. return -EOPNOTSUPP;
  2078. /* If SCR can be read, use it to determine the current SPD.
  2079. * If not, use cached value in link->sata_spd.
  2080. */
  2081. rc = sata_scr_read(link, SCR_STATUS, &sstatus);
  2082. if (rc == 0)
  2083. spd = (sstatus >> 4) & 0xf;
  2084. else
  2085. spd = link->sata_spd;
  2086. mask = link->sata_spd_limit;
  2087. if (mask <= 1)
  2088. return -EINVAL;
  2089. /* unconditionally mask off the highest bit */
  2090. highbit = fls(mask) - 1;
  2091. mask &= ~(1 << highbit);
  2092. /* Mask off all speeds higher than or equal to the current
  2093. * one. Force 1.5Gbps if current SPD is not available.
  2094. */
  2095. if (spd > 1)
  2096. mask &= (1 << (spd - 1)) - 1;
  2097. else
  2098. mask &= 1;
  2099. /* were we already at the bottom? */
  2100. if (!mask)
  2101. return -EINVAL;
  2102. link->sata_spd_limit = mask;
  2103. ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
  2104. sata_spd_string(fls(mask)));
  2105. return 0;
  2106. }
  2107. static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
  2108. {
  2109. u32 spd, limit;
  2110. if (link->sata_spd_limit == UINT_MAX)
  2111. limit = 0;
  2112. else
  2113. limit = fls(link->sata_spd_limit);
  2114. spd = (*scontrol >> 4) & 0xf;
  2115. *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
  2116. return spd != limit;
  2117. }
  2118. /**
  2119. * sata_set_spd_needed - is SATA spd configuration needed
  2120. * @link: Link in question
  2121. *
  2122. * Test whether the spd limit in SControl matches
  2123. * @link->sata_spd_limit. This function is used to determine
  2124. * whether hardreset is necessary to apply SATA spd
  2125. * configuration.
  2126. *
  2127. * LOCKING:
  2128. * Inherited from caller.
  2129. *
  2130. * RETURNS:
  2131. * 1 if SATA spd configuration is needed, 0 otherwise.
  2132. */
  2133. int sata_set_spd_needed(struct ata_link *link)
  2134. {
  2135. u32 scontrol;
  2136. if (sata_scr_read(link, SCR_CONTROL, &scontrol))
  2137. return 0;
  2138. return __sata_set_spd_needed(link, &scontrol);
  2139. }
  2140. /**
  2141. * sata_set_spd - set SATA spd according to spd limit
  2142. * @link: Link to set SATA spd for
  2143. *
  2144. * Set SATA spd of @link according to sata_spd_limit.
  2145. *
  2146. * LOCKING:
  2147. * Inherited from caller.
  2148. *
  2149. * RETURNS:
  2150. * 0 if spd doesn't need to be changed, 1 if spd has been
  2151. * changed. Negative errno if SCR registers are inaccessible.
  2152. */
  2153. int sata_set_spd(struct ata_link *link)
  2154. {
  2155. u32 scontrol;
  2156. int rc;
  2157. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  2158. return rc;
  2159. if (!__sata_set_spd_needed(link, &scontrol))
  2160. return 0;
  2161. if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
  2162. return rc;
  2163. return 1;
  2164. }
  2165. /*
  2166. * This mode timing computation functionality is ported over from
  2167. * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
  2168. */
  2169. /*
  2170. * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
  2171. * These were taken from ATA/ATAPI-6 standard, rev 0a, except
  2172. * for UDMA6, which is currently supported only by Maxtor drives.
  2173. *
  2174. * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
  2175. */
  2176. static const struct ata_timing ata_timing[] = {
  2177. { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
  2178. { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
  2179. { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
  2180. { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
  2181. { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
  2182. { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
  2183. { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
  2184. { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
  2185. { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
  2186. /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
  2187. { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
  2188. { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
  2189. { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
  2190. { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
  2191. { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
  2192. { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
  2193. { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
  2194. { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
  2195. { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
  2196. { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
  2197. { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
  2198. { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
  2199. { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
  2200. /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
  2201. { 0xFF }
  2202. };
  2203. #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
  2204. #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
  2205. static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
  2206. {
  2207. q->setup = EZ(t->setup * 1000, T);
  2208. q->act8b = EZ(t->act8b * 1000, T);
  2209. q->rec8b = EZ(t->rec8b * 1000, T);
  2210. q->cyc8b = EZ(t->cyc8b * 1000, T);
  2211. q->active = EZ(t->active * 1000, T);
  2212. q->recover = EZ(t->recover * 1000, T);
  2213. q->cycle = EZ(t->cycle * 1000, T);
  2214. q->udma = EZ(t->udma * 1000, UT);
  2215. }
  2216. void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
  2217. struct ata_timing *m, unsigned int what)
  2218. {
  2219. if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
  2220. if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
  2221. if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
  2222. if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
  2223. if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
  2224. if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
  2225. if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
  2226. if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
  2227. }
  2228. static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
  2229. {
  2230. const struct ata_timing *t;
  2231. for (t = ata_timing; t->mode != speed; t++)
  2232. if (t->mode == 0xFF)
  2233. return NULL;
  2234. return t;
  2235. }
  2236. int ata_timing_compute(struct ata_device *adev, unsigned short speed,
  2237. struct ata_timing *t, int T, int UT)
  2238. {
  2239. const struct ata_timing *s;
  2240. struct ata_timing p;
  2241. /*
  2242. * Find the mode.
  2243. */
  2244. if (!(s = ata_timing_find_mode(speed)))
  2245. return -EINVAL;
  2246. memcpy(t, s, sizeof(*s));
  2247. /*
  2248. * If the drive is an EIDE drive, it can tell us it needs extended
  2249. * PIO/MW_DMA cycle timing.
  2250. */
  2251. if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
  2252. memset(&p, 0, sizeof(p));
  2253. if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
  2254. if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
  2255. else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
  2256. } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
  2257. p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
  2258. }
  2259. ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
  2260. }
  2261. /*
  2262. * Convert the timing to bus clock counts.
  2263. */
  2264. ata_timing_quantize(t, t, T, UT);
  2265. /*
  2266. * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
  2267. * S.M.A.R.T * and some other commands. We have to ensure that the
  2268. * DMA cycle timing is slower/equal than the fastest PIO timing.
  2269. */
  2270. if (speed > XFER_PIO_6) {
  2271. ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
  2272. ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
  2273. }
  2274. /*
  2275. * Lengthen active & recovery time so that cycle time is correct.
  2276. */
  2277. if (t->act8b + t->rec8b < t->cyc8b) {
  2278. t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
  2279. t->rec8b = t->cyc8b - t->act8b;
  2280. }
  2281. if (t->active + t->recover < t->cycle) {
  2282. t->active += (t->cycle - (t->active + t->recover)) / 2;
  2283. t->recover = t->cycle - t->active;
  2284. }
  2285. /* In a few cases quantisation may produce enough errors to
  2286. leave t->cycle too low for the sum of active and recovery
  2287. if so we must correct this */
  2288. if (t->active + t->recover > t->cycle)
  2289. t->cycle = t->active + t->recover;
  2290. return 0;
  2291. }
  2292. /**
  2293. * ata_down_xfermask_limit - adjust dev xfer masks downward
  2294. * @dev: Device to adjust xfer masks
  2295. * @sel: ATA_DNXFER_* selector
  2296. *
  2297. * Adjust xfer masks of @dev downward. Note that this function
  2298. * does not apply the change. Invoking ata_set_mode() afterwards
  2299. * will apply the limit.
  2300. *
  2301. * LOCKING:
  2302. * Inherited from caller.
  2303. *
  2304. * RETURNS:
  2305. * 0 on success, negative errno on failure
  2306. */
  2307. int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
  2308. {
  2309. char buf[32];
  2310. unsigned int orig_mask, xfer_mask;
  2311. unsigned int pio_mask, mwdma_mask, udma_mask;
  2312. int quiet, highbit;
  2313. quiet = !!(sel & ATA_DNXFER_QUIET);
  2314. sel &= ~ATA_DNXFER_QUIET;
  2315. xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
  2316. dev->mwdma_mask,
  2317. dev->udma_mask);
  2318. ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
  2319. switch (sel) {
  2320. case ATA_DNXFER_PIO:
  2321. highbit = fls(pio_mask) - 1;
  2322. pio_mask &= ~(1 << highbit);
  2323. break;
  2324. case ATA_DNXFER_DMA:
  2325. if (udma_mask) {
  2326. highbit = fls(udma_mask) - 1;
  2327. udma_mask &= ~(1 << highbit);
  2328. if (!udma_mask)
  2329. return -ENOENT;
  2330. } else if (mwdma_mask) {
  2331. highbit = fls(mwdma_mask) - 1;
  2332. mwdma_mask &= ~(1 << highbit);
  2333. if (!mwdma_mask)
  2334. return -ENOENT;
  2335. }
  2336. break;
  2337. case ATA_DNXFER_40C:
  2338. udma_mask &= ATA_UDMA_MASK_40C;
  2339. break;
  2340. case ATA_DNXFER_FORCE_PIO0:
  2341. pio_mask &= 1;
  2342. case ATA_DNXFER_FORCE_PIO:
  2343. mwdma_mask = 0;
  2344. udma_mask = 0;
  2345. break;
  2346. default:
  2347. BUG();
  2348. }
  2349. xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
  2350. if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
  2351. return -ENOENT;
  2352. if (!quiet) {
  2353. if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
  2354. snprintf(buf, sizeof(buf), "%s:%s",
  2355. ata_mode_string(xfer_mask),
  2356. ata_mode_string(xfer_mask & ATA_MASK_PIO));
  2357. else
  2358. snprintf(buf, sizeof(buf), "%s",
  2359. ata_mode_string(xfer_mask));
  2360. ata_dev_printk(dev, KERN_WARNING,
  2361. "limiting speed to %s\n", buf);
  2362. }
  2363. ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
  2364. &dev->udma_mask);
  2365. return 0;
  2366. }
  2367. static int ata_dev_set_mode(struct ata_device *dev)
  2368. {
  2369. struct ata_eh_context *ehc = &dev->link->eh_context;
  2370. unsigned int err_mask;
  2371. int rc;
  2372. dev->flags &= ~ATA_DFLAG_PIO;
  2373. if (dev->xfer_shift == ATA_SHIFT_PIO)
  2374. dev->flags |= ATA_DFLAG_PIO;
  2375. err_mask = ata_dev_set_xfermode(dev);
  2376. /* Old CFA may refuse this command, which is just fine */
  2377. if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
  2378. err_mask &= ~AC_ERR_DEV;
  2379. /* Some very old devices and some bad newer ones fail any kind of
  2380. SET_XFERMODE request but support PIO0-2 timings and no IORDY */
  2381. if (dev->xfer_shift == ATA_SHIFT_PIO && !ata_id_has_iordy(dev->id) &&
  2382. dev->pio_mode <= XFER_PIO_2)
  2383. err_mask &= ~AC_ERR_DEV;
  2384. if (err_mask) {
  2385. ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
  2386. "(err_mask=0x%x)\n", err_mask);
  2387. return -EIO;
  2388. }
  2389. ehc->i.flags |= ATA_EHI_POST_SETMODE;
  2390. rc = ata_dev_revalidate(dev, 0);
  2391. ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
  2392. if (rc)
  2393. return rc;
  2394. DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
  2395. dev->xfer_shift, (int)dev->xfer_mode);
  2396. ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
  2397. ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
  2398. return 0;
  2399. }
  2400. /**
  2401. * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
  2402. * @link: link on which timings will be programmed
  2403. * @r_failed_dev: out paramter for failed device
  2404. *
  2405. * Standard implementation of the function used to tune and set
  2406. * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
  2407. * ata_dev_set_mode() fails, pointer to the failing device is
  2408. * returned in @r_failed_dev.
  2409. *
  2410. * LOCKING:
  2411. * PCI/etc. bus probe sem.
  2412. *
  2413. * RETURNS:
  2414. * 0 on success, negative errno otherwise
  2415. */
  2416. int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
  2417. {
  2418. struct ata_port *ap = link->ap;
  2419. struct ata_device *dev;
  2420. int rc = 0, used_dma = 0, found = 0;
  2421. /* step 1: calculate xfer_mask */
  2422. ata_link_for_each_dev(dev, link) {
  2423. unsigned int pio_mask, dma_mask;
  2424. if (!ata_dev_enabled(dev))
  2425. continue;
  2426. ata_dev_xfermask(dev);
  2427. pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
  2428. dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
  2429. dev->pio_mode = ata_xfer_mask2mode(pio_mask);
  2430. dev->dma_mode = ata_xfer_mask2mode(dma_mask);
  2431. found = 1;
  2432. if (dev->dma_mode)
  2433. used_dma = 1;
  2434. }
  2435. if (!found)
  2436. goto out;
  2437. /* step 2: always set host PIO timings */
  2438. ata_link_for_each_dev(dev, link) {
  2439. if (!ata_dev_enabled(dev))
  2440. continue;
  2441. if (!dev->pio_mode) {
  2442. ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
  2443. rc = -EINVAL;
  2444. goto out;
  2445. }
  2446. dev->xfer_mode = dev->pio_mode;
  2447. dev->xfer_shift = ATA_SHIFT_PIO;
  2448. if (ap->ops->set_piomode)
  2449. ap->ops->set_piomode(ap, dev);
  2450. }
  2451. /* step 3: set host DMA timings */
  2452. ata_link_for_each_dev(dev, link) {
  2453. if (!ata_dev_enabled(dev) || !dev->dma_mode)
  2454. continue;
  2455. dev->xfer_mode = dev->dma_mode;
  2456. dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
  2457. if (ap->ops->set_dmamode)
  2458. ap->ops->set_dmamode(ap, dev);
  2459. }
  2460. /* step 4: update devices' xfer mode */
  2461. ata_link_for_each_dev(dev, link) {
  2462. /* don't update suspended devices' xfer mode */
  2463. if (!ata_dev_enabled(dev))
  2464. continue;
  2465. rc = ata_dev_set_mode(dev);
  2466. if (rc)
  2467. goto out;
  2468. }
  2469. /* Record simplex status. If we selected DMA then the other
  2470. * host channels are not permitted to do so.
  2471. */
  2472. if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
  2473. ap->host->simplex_claimed = ap;
  2474. out:
  2475. if (rc)
  2476. *r_failed_dev = dev;
  2477. return rc;
  2478. }
  2479. /**
  2480. * ata_set_mode - Program timings and issue SET FEATURES - XFER
  2481. * @link: link on which timings will be programmed
  2482. * @r_failed_dev: out paramter for failed device
  2483. *
  2484. * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
  2485. * ata_set_mode() fails, pointer to the failing device is
  2486. * returned in @r_failed_dev.
  2487. *
  2488. * LOCKING:
  2489. * PCI/etc. bus probe sem.
  2490. *
  2491. * RETURNS:
  2492. * 0 on success, negative errno otherwise
  2493. */
  2494. int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
  2495. {
  2496. struct ata_port *ap = link->ap;
  2497. /* has private set_mode? */
  2498. if (ap->ops->set_mode)
  2499. return ap->ops->set_mode(link, r_failed_dev);
  2500. return ata_do_set_mode(link, r_failed_dev);
  2501. }
  2502. /**
  2503. * ata_tf_to_host - issue ATA taskfile to host controller
  2504. * @ap: port to which command is being issued
  2505. * @tf: ATA taskfile register set
  2506. *
  2507. * Issues ATA taskfile register set to ATA host controller,
  2508. * with proper synchronization with interrupt handler and
  2509. * other threads.
  2510. *
  2511. * LOCKING:
  2512. * spin_lock_irqsave(host lock)
  2513. */
  2514. static inline void ata_tf_to_host(struct ata_port *ap,
  2515. const struct ata_taskfile *tf)
  2516. {
  2517. ap->ops->tf_load(ap, tf);
  2518. ap->ops->exec_command(ap, tf);
  2519. }
  2520. /**
  2521. * ata_busy_sleep - sleep until BSY clears, or timeout
  2522. * @ap: port containing status register to be polled
  2523. * @tmout_pat: impatience timeout
  2524. * @tmout: overall timeout
  2525. *
  2526. * Sleep until ATA Status register bit BSY clears,
  2527. * or a timeout occurs.
  2528. *
  2529. * LOCKING:
  2530. * Kernel thread context (may sleep).
  2531. *
  2532. * RETURNS:
  2533. * 0 on success, -errno otherwise.
  2534. */
  2535. int ata_busy_sleep(struct ata_port *ap,
  2536. unsigned long tmout_pat, unsigned long tmout)
  2537. {
  2538. unsigned long timer_start, timeout;
  2539. u8 status;
  2540. status = ata_busy_wait(ap, ATA_BUSY, 300);
  2541. timer_start = jiffies;
  2542. timeout = timer_start + tmout_pat;
  2543. while (status != 0xff && (status & ATA_BUSY) &&
  2544. time_before(jiffies, timeout)) {
  2545. msleep(50);
  2546. status = ata_busy_wait(ap, ATA_BUSY, 3);
  2547. }
  2548. if (status != 0xff && (status & ATA_BUSY))
  2549. ata_port_printk(ap, KERN_WARNING,
  2550. "port is slow to respond, please be patient "
  2551. "(Status 0x%x)\n", status);
  2552. timeout = timer_start + tmout;
  2553. while (status != 0xff && (status & ATA_BUSY) &&
  2554. time_before(jiffies, timeout)) {
  2555. msleep(50);
  2556. status = ata_chk_status(ap);
  2557. }
  2558. if (status == 0xff)
  2559. return -ENODEV;
  2560. if (status & ATA_BUSY) {
  2561. ata_port_printk(ap, KERN_ERR, "port failed to respond "
  2562. "(%lu secs, Status 0x%x)\n",
  2563. tmout / HZ, status);
  2564. return -EBUSY;
  2565. }
  2566. return 0;
  2567. }
  2568. /**
  2569. * ata_wait_ready - sleep until BSY clears, or timeout
  2570. * @ap: port containing status register to be polled
  2571. * @deadline: deadline jiffies for the operation
  2572. *
  2573. * Sleep until ATA Status register bit BSY clears, or timeout
  2574. * occurs.
  2575. *
  2576. * LOCKING:
  2577. * Kernel thread context (may sleep).
  2578. *
  2579. * RETURNS:
  2580. * 0 on success, -errno otherwise.
  2581. */
  2582. int ata_wait_ready(struct ata_port *ap, unsigned long deadline)
  2583. {
  2584. unsigned long start = jiffies;
  2585. int warned = 0;
  2586. while (1) {
  2587. u8 status = ata_chk_status(ap);
  2588. unsigned long now = jiffies;
  2589. if (!(status & ATA_BUSY))
  2590. return 0;
  2591. if (!ata_link_online(&ap->link) && status == 0xff)
  2592. return -ENODEV;
  2593. if (time_after(now, deadline))
  2594. return -EBUSY;
  2595. if (!warned && time_after(now, start + 5 * HZ) &&
  2596. (deadline - now > 3 * HZ)) {
  2597. ata_port_printk(ap, KERN_WARNING,
  2598. "port is slow to respond, please be patient "
  2599. "(Status 0x%x)\n", status);
  2600. warned = 1;
  2601. }
  2602. msleep(50);
  2603. }
  2604. }
  2605. static int ata_bus_post_reset(struct ata_port *ap, unsigned int devmask,
  2606. unsigned long deadline)
  2607. {
  2608. struct ata_ioports *ioaddr = &ap->ioaddr;
  2609. unsigned int dev0 = devmask & (1 << 0);
  2610. unsigned int dev1 = devmask & (1 << 1);
  2611. int rc, ret = 0;
  2612. /* if device 0 was found in ata_devchk, wait for its
  2613. * BSY bit to clear
  2614. */
  2615. if (dev0) {
  2616. rc = ata_wait_ready(ap, deadline);
  2617. if (rc) {
  2618. if (rc != -ENODEV)
  2619. return rc;
  2620. ret = rc;
  2621. }
  2622. }
  2623. /* if device 1 was found in ata_devchk, wait for register
  2624. * access briefly, then wait for BSY to clear.
  2625. */
  2626. if (dev1) {
  2627. int i;
  2628. ap->ops->dev_select(ap, 1);
  2629. /* Wait for register access. Some ATAPI devices fail
  2630. * to set nsect/lbal after reset, so don't waste too
  2631. * much time on it. We're gonna wait for !BSY anyway.
  2632. */
  2633. for (i = 0; i < 2; i++) {
  2634. u8 nsect, lbal;
  2635. nsect = ioread8(ioaddr->nsect_addr);
  2636. lbal = ioread8(ioaddr->lbal_addr);
  2637. if ((nsect == 1) && (lbal == 1))
  2638. break;
  2639. msleep(50); /* give drive a breather */
  2640. }
  2641. rc = ata_wait_ready(ap, deadline);
  2642. if (rc) {
  2643. if (rc != -ENODEV)
  2644. return rc;
  2645. ret = rc;
  2646. }
  2647. }
  2648. /* is all this really necessary? */
  2649. ap->ops->dev_select(ap, 0);
  2650. if (dev1)
  2651. ap->ops->dev_select(ap, 1);
  2652. if (dev0)
  2653. ap->ops->dev_select(ap, 0);
  2654. return ret;
  2655. }
  2656. static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
  2657. unsigned long deadline)
  2658. {
  2659. struct ata_ioports *ioaddr = &ap->ioaddr;
  2660. DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
  2661. /* software reset. causes dev0 to be selected */
  2662. iowrite8(ap->ctl, ioaddr->ctl_addr);
  2663. udelay(20); /* FIXME: flush */
  2664. iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
  2665. udelay(20); /* FIXME: flush */
  2666. iowrite8(ap->ctl, ioaddr->ctl_addr);
  2667. /* spec mandates ">= 2ms" before checking status.
  2668. * We wait 150ms, because that was the magic delay used for
  2669. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  2670. * between when the ATA command register is written, and then
  2671. * status is checked. Because waiting for "a while" before
  2672. * checking status is fine, post SRST, we perform this magic
  2673. * delay here as well.
  2674. *
  2675. * Old drivers/ide uses the 2mS rule and then waits for ready
  2676. */
  2677. msleep(150);
  2678. /* Before we perform post reset processing we want to see if
  2679. * the bus shows 0xFF because the odd clown forgets the D7
  2680. * pulldown resistor.
  2681. */
  2682. if (ata_check_status(ap) == 0xFF)
  2683. return -ENODEV;
  2684. return ata_bus_post_reset(ap, devmask, deadline);
  2685. }
  2686. /**
  2687. * ata_bus_reset - reset host port and associated ATA channel
  2688. * @ap: port to reset
  2689. *
  2690. * This is typically the first time we actually start issuing
  2691. * commands to the ATA channel. We wait for BSY to clear, then
  2692. * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
  2693. * result. Determine what devices, if any, are on the channel
  2694. * by looking at the device 0/1 error register. Look at the signature
  2695. * stored in each device's taskfile registers, to determine if
  2696. * the device is ATA or ATAPI.
  2697. *
  2698. * LOCKING:
  2699. * PCI/etc. bus probe sem.
  2700. * Obtains host lock.
  2701. *
  2702. * SIDE EFFECTS:
  2703. * Sets ATA_FLAG_DISABLED if bus reset fails.
  2704. */
  2705. void ata_bus_reset(struct ata_port *ap)
  2706. {
  2707. struct ata_device *device = ap->link.device;
  2708. struct ata_ioports *ioaddr = &ap->ioaddr;
  2709. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  2710. u8 err;
  2711. unsigned int dev0, dev1 = 0, devmask = 0;
  2712. int rc;
  2713. DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
  2714. /* determine if device 0/1 are present */
  2715. if (ap->flags & ATA_FLAG_SATA_RESET)
  2716. dev0 = 1;
  2717. else {
  2718. dev0 = ata_devchk(ap, 0);
  2719. if (slave_possible)
  2720. dev1 = ata_devchk(ap, 1);
  2721. }
  2722. if (dev0)
  2723. devmask |= (1 << 0);
  2724. if (dev1)
  2725. devmask |= (1 << 1);
  2726. /* select device 0 again */
  2727. ap->ops->dev_select(ap, 0);
  2728. /* issue bus reset */
  2729. if (ap->flags & ATA_FLAG_SRST) {
  2730. rc = ata_bus_softreset(ap, devmask, jiffies + 40 * HZ);
  2731. if (rc && rc != -ENODEV)
  2732. goto err_out;
  2733. }
  2734. /*
  2735. * determine by signature whether we have ATA or ATAPI devices
  2736. */
  2737. device[0].class = ata_dev_try_classify(ap, 0, &err);
  2738. if ((slave_possible) && (err != 0x81))
  2739. device[1].class = ata_dev_try_classify(ap, 1, &err);
  2740. /* is double-select really necessary? */
  2741. if (device[1].class != ATA_DEV_NONE)
  2742. ap->ops->dev_select(ap, 1);
  2743. if (device[0].class != ATA_DEV_NONE)
  2744. ap->ops->dev_select(ap, 0);
  2745. /* if no devices were detected, disable this port */
  2746. if ((device[0].class == ATA_DEV_NONE) &&
  2747. (device[1].class == ATA_DEV_NONE))
  2748. goto err_out;
  2749. if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
  2750. /* set up device control for ATA_FLAG_SATA_RESET */
  2751. iowrite8(ap->ctl, ioaddr->ctl_addr);
  2752. }
  2753. DPRINTK("EXIT\n");
  2754. return;
  2755. err_out:
  2756. ata_port_printk(ap, KERN_ERR, "disabling port\n");
  2757. ap->ops->port_disable(ap);
  2758. DPRINTK("EXIT\n");
  2759. }
  2760. /**
  2761. * sata_link_debounce - debounce SATA phy status
  2762. * @link: ATA link to debounce SATA phy status for
  2763. * @params: timing parameters { interval, duratinon, timeout } in msec
  2764. * @deadline: deadline jiffies for the operation
  2765. *
  2766. * Make sure SStatus of @link reaches stable state, determined by
  2767. * holding the same value where DET is not 1 for @duration polled
  2768. * every @interval, before @timeout. Timeout constraints the
  2769. * beginning of the stable state. Because DET gets stuck at 1 on
  2770. * some controllers after hot unplugging, this functions waits
  2771. * until timeout then returns 0 if DET is stable at 1.
  2772. *
  2773. * @timeout is further limited by @deadline. The sooner of the
  2774. * two is used.
  2775. *
  2776. * LOCKING:
  2777. * Kernel thread context (may sleep)
  2778. *
  2779. * RETURNS:
  2780. * 0 on success, -errno on failure.
  2781. */
  2782. int sata_link_debounce(struct ata_link *link, const unsigned long *params,
  2783. unsigned long deadline)
  2784. {
  2785. unsigned long interval_msec = params[0];
  2786. unsigned long duration = msecs_to_jiffies(params[1]);
  2787. unsigned long last_jiffies, t;
  2788. u32 last, cur;
  2789. int rc;
  2790. t = jiffies + msecs_to_jiffies(params[2]);
  2791. if (time_before(t, deadline))
  2792. deadline = t;
  2793. if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
  2794. return rc;
  2795. cur &= 0xf;
  2796. last = cur;
  2797. last_jiffies = jiffies;
  2798. while (1) {
  2799. msleep(interval_msec);
  2800. if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
  2801. return rc;
  2802. cur &= 0xf;
  2803. /* DET stable? */
  2804. if (cur == last) {
  2805. if (cur == 1 && time_before(jiffies, deadline))
  2806. continue;
  2807. if (time_after(jiffies, last_jiffies + duration))
  2808. return 0;
  2809. continue;
  2810. }
  2811. /* unstable, start over */
  2812. last = cur;
  2813. last_jiffies = jiffies;
  2814. /* Check deadline. If debouncing failed, return
  2815. * -EPIPE to tell upper layer to lower link speed.
  2816. */
  2817. if (time_after(jiffies, deadline))
  2818. return -EPIPE;
  2819. }
  2820. }
  2821. /**
  2822. * sata_link_resume - resume SATA link
  2823. * @link: ATA link to resume SATA
  2824. * @params: timing parameters { interval, duratinon, timeout } in msec
  2825. * @deadline: deadline jiffies for the operation
  2826. *
  2827. * Resume SATA phy @link and debounce it.
  2828. *
  2829. * LOCKING:
  2830. * Kernel thread context (may sleep)
  2831. *
  2832. * RETURNS:
  2833. * 0 on success, -errno on failure.
  2834. */
  2835. int sata_link_resume(struct ata_link *link, const unsigned long *params,
  2836. unsigned long deadline)
  2837. {
  2838. u32 scontrol;
  2839. int rc;
  2840. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  2841. return rc;
  2842. scontrol = (scontrol & 0x0f0) | 0x300;
  2843. if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
  2844. return rc;
  2845. /* Some PHYs react badly if SStatus is pounded immediately
  2846. * after resuming. Delay 200ms before debouncing.
  2847. */
  2848. msleep(200);
  2849. return sata_link_debounce(link, params, deadline);
  2850. }
  2851. /**
  2852. * ata_std_prereset - prepare for reset
  2853. * @link: ATA link to be reset
  2854. * @deadline: deadline jiffies for the operation
  2855. *
  2856. * @link is about to be reset. Initialize it. Failure from
  2857. * prereset makes libata abort whole reset sequence and give up
  2858. * that port, so prereset should be best-effort. It does its
  2859. * best to prepare for reset sequence but if things go wrong, it
  2860. * should just whine, not fail.
  2861. *
  2862. * LOCKING:
  2863. * Kernel thread context (may sleep)
  2864. *
  2865. * RETURNS:
  2866. * 0 on success, -errno otherwise.
  2867. */
  2868. int ata_std_prereset(struct ata_link *link, unsigned long deadline)
  2869. {
  2870. struct ata_port *ap = link->ap;
  2871. struct ata_eh_context *ehc = &link->eh_context;
  2872. const unsigned long *timing = sata_ehc_deb_timing(ehc);
  2873. int rc;
  2874. /* handle link resume */
  2875. if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
  2876. (link->flags & ATA_LFLAG_HRST_TO_RESUME))
  2877. ehc->i.action |= ATA_EH_HARDRESET;
  2878. /* if we're about to do hardreset, nothing more to do */
  2879. if (ehc->i.action & ATA_EH_HARDRESET)
  2880. return 0;
  2881. /* if SATA, resume link */
  2882. if (ap->flags & ATA_FLAG_SATA) {
  2883. rc = sata_link_resume(link, timing, deadline);
  2884. /* whine about phy resume failure but proceed */
  2885. if (rc && rc != -EOPNOTSUPP)
  2886. ata_link_printk(link, KERN_WARNING, "failed to resume "
  2887. "link for reset (errno=%d)\n", rc);
  2888. }
  2889. /* Wait for !BSY if the controller can wait for the first D2H
  2890. * Reg FIS and we don't know that no device is attached.
  2891. */
  2892. if (!(link->flags & ATA_LFLAG_SKIP_D2H_BSY) && !ata_link_offline(link)) {
  2893. rc = ata_wait_ready(ap, deadline);
  2894. if (rc && rc != -ENODEV) {
  2895. ata_link_printk(link, KERN_WARNING, "device not ready "
  2896. "(errno=%d), forcing hardreset\n", rc);
  2897. ehc->i.action |= ATA_EH_HARDRESET;
  2898. }
  2899. }
  2900. return 0;
  2901. }
  2902. /**
  2903. * ata_std_softreset - reset host port via ATA SRST
  2904. * @link: ATA link to reset
  2905. * @classes: resulting classes of attached devices
  2906. * @deadline: deadline jiffies for the operation
  2907. *
  2908. * Reset host port using ATA SRST.
  2909. *
  2910. * LOCKING:
  2911. * Kernel thread context (may sleep)
  2912. *
  2913. * RETURNS:
  2914. * 0 on success, -errno otherwise.
  2915. */
  2916. int ata_std_softreset(struct ata_link *link, unsigned int *classes,
  2917. unsigned long deadline)
  2918. {
  2919. struct ata_port *ap = link->ap;
  2920. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  2921. unsigned int devmask = 0;
  2922. int rc;
  2923. u8 err;
  2924. DPRINTK("ENTER\n");
  2925. if (ata_link_offline(link)) {
  2926. classes[0] = ATA_DEV_NONE;
  2927. goto out;
  2928. }
  2929. /* determine if device 0/1 are present */
  2930. if (ata_devchk(ap, 0))
  2931. devmask |= (1 << 0);
  2932. if (slave_possible && ata_devchk(ap, 1))
  2933. devmask |= (1 << 1);
  2934. /* select device 0 again */
  2935. ap->ops->dev_select(ap, 0);
  2936. /* issue bus reset */
  2937. DPRINTK("about to softreset, devmask=%x\n", devmask);
  2938. rc = ata_bus_softreset(ap, devmask, deadline);
  2939. /* if link is occupied, -ENODEV too is an error */
  2940. if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
  2941. ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
  2942. return rc;
  2943. }
  2944. /* determine by signature whether we have ATA or ATAPI devices */
  2945. classes[0] = ata_dev_try_classify(ap, 0, &err);
  2946. if (slave_possible && err != 0x81)
  2947. classes[1] = ata_dev_try_classify(ap, 1, &err);
  2948. out:
  2949. DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
  2950. return 0;
  2951. }
  2952. /**
  2953. * sata_link_hardreset - reset link via SATA phy reset
  2954. * @link: link to reset
  2955. * @timing: timing parameters { interval, duratinon, timeout } in msec
  2956. * @deadline: deadline jiffies for the operation
  2957. *
  2958. * SATA phy-reset @link using DET bits of SControl register.
  2959. *
  2960. * LOCKING:
  2961. * Kernel thread context (may sleep)
  2962. *
  2963. * RETURNS:
  2964. * 0 on success, -errno otherwise.
  2965. */
  2966. int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
  2967. unsigned long deadline)
  2968. {
  2969. u32 scontrol;
  2970. int rc;
  2971. DPRINTK("ENTER\n");
  2972. if (sata_set_spd_needed(link)) {
  2973. /* SATA spec says nothing about how to reconfigure
  2974. * spd. To be on the safe side, turn off phy during
  2975. * reconfiguration. This works for at least ICH7 AHCI
  2976. * and Sil3124.
  2977. */
  2978. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  2979. goto out;
  2980. scontrol = (scontrol & 0x0f0) | 0x304;
  2981. if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
  2982. goto out;
  2983. sata_set_spd(link);
  2984. }
  2985. /* issue phy wake/reset */
  2986. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  2987. goto out;
  2988. scontrol = (scontrol & 0x0f0) | 0x301;
  2989. if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
  2990. goto out;
  2991. /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
  2992. * 10.4.2 says at least 1 ms.
  2993. */
  2994. msleep(1);
  2995. /* bring link back */
  2996. rc = sata_link_resume(link, timing, deadline);
  2997. out:
  2998. DPRINTK("EXIT, rc=%d\n", rc);
  2999. return rc;
  3000. }
  3001. /**
  3002. * sata_std_hardreset - reset host port via SATA phy reset
  3003. * @link: link to reset
  3004. * @class: resulting class of attached device
  3005. * @deadline: deadline jiffies for the operation
  3006. *
  3007. * SATA phy-reset host port using DET bits of SControl register,
  3008. * wait for !BSY and classify the attached device.
  3009. *
  3010. * LOCKING:
  3011. * Kernel thread context (may sleep)
  3012. *
  3013. * RETURNS:
  3014. * 0 on success, -errno otherwise.
  3015. */
  3016. int sata_std_hardreset(struct ata_link *link, unsigned int *class,
  3017. unsigned long deadline)
  3018. {
  3019. struct ata_port *ap = link->ap;
  3020. const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
  3021. int rc;
  3022. DPRINTK("ENTER\n");
  3023. /* do hardreset */
  3024. rc = sata_link_hardreset(link, timing, deadline);
  3025. if (rc) {
  3026. ata_link_printk(link, KERN_ERR,
  3027. "COMRESET failed (errno=%d)\n", rc);
  3028. return rc;
  3029. }
  3030. /* TODO: phy layer with polling, timeouts, etc. */
  3031. if (ata_link_offline(link)) {
  3032. *class = ATA_DEV_NONE;
  3033. DPRINTK("EXIT, link offline\n");
  3034. return 0;
  3035. }
  3036. /* wait a while before checking status, see SRST for more info */
  3037. msleep(150);
  3038. rc = ata_wait_ready(ap, deadline);
  3039. /* link occupied, -ENODEV too is an error */
  3040. if (rc) {
  3041. ata_link_printk(link, KERN_ERR,
  3042. "COMRESET failed (errno=%d)\n", rc);
  3043. return rc;
  3044. }
  3045. ap->ops->dev_select(ap, 0); /* probably unnecessary */
  3046. *class = ata_dev_try_classify(ap, 0, NULL);
  3047. DPRINTK("EXIT, class=%u\n", *class);
  3048. return 0;
  3049. }
  3050. /**
  3051. * ata_std_postreset - standard postreset callback
  3052. * @link: the target ata_link
  3053. * @classes: classes of attached devices
  3054. *
  3055. * This function is invoked after a successful reset. Note that
  3056. * the device might have been reset more than once using
  3057. * different reset methods before postreset is invoked.
  3058. *
  3059. * LOCKING:
  3060. * Kernel thread context (may sleep)
  3061. */
  3062. void ata_std_postreset(struct ata_link *link, unsigned int *classes)
  3063. {
  3064. struct ata_port *ap = link->ap;
  3065. u32 serror;
  3066. DPRINTK("ENTER\n");
  3067. /* print link status */
  3068. sata_print_link_status(link);
  3069. /* clear SError */
  3070. if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
  3071. sata_scr_write(link, SCR_ERROR, serror);
  3072. /* is double-select really necessary? */
  3073. if (classes[0] != ATA_DEV_NONE)
  3074. ap->ops->dev_select(ap, 1);
  3075. if (classes[1] != ATA_DEV_NONE)
  3076. ap->ops->dev_select(ap, 0);
  3077. /* bail out if no device is present */
  3078. if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
  3079. DPRINTK("EXIT, no device\n");
  3080. return;
  3081. }
  3082. /* set up device control */
  3083. if (ap->ioaddr.ctl_addr)
  3084. iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
  3085. DPRINTK("EXIT\n");
  3086. }
  3087. /**
  3088. * ata_dev_same_device - Determine whether new ID matches configured device
  3089. * @dev: device to compare against
  3090. * @new_class: class of the new device
  3091. * @new_id: IDENTIFY page of the new device
  3092. *
  3093. * Compare @new_class and @new_id against @dev and determine
  3094. * whether @dev is the device indicated by @new_class and
  3095. * @new_id.
  3096. *
  3097. * LOCKING:
  3098. * None.
  3099. *
  3100. * RETURNS:
  3101. * 1 if @dev matches @new_class and @new_id, 0 otherwise.
  3102. */
  3103. static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
  3104. const u16 *new_id)
  3105. {
  3106. const u16 *old_id = dev->id;
  3107. unsigned char model[2][ATA_ID_PROD_LEN + 1];
  3108. unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
  3109. if (dev->class != new_class) {
  3110. ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
  3111. dev->class, new_class);
  3112. return 0;
  3113. }
  3114. ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
  3115. ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
  3116. ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
  3117. ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
  3118. if (strcmp(model[0], model[1])) {
  3119. ata_dev_printk(dev, KERN_INFO, "model number mismatch "
  3120. "'%s' != '%s'\n", model[0], model[1]);
  3121. return 0;
  3122. }
  3123. if (strcmp(serial[0], serial[1])) {
  3124. ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
  3125. "'%s' != '%s'\n", serial[0], serial[1]);
  3126. return 0;
  3127. }
  3128. return 1;
  3129. }
  3130. /**
  3131. * ata_dev_reread_id - Re-read IDENTIFY data
  3132. * @dev: target ATA device
  3133. * @readid_flags: read ID flags
  3134. *
  3135. * Re-read IDENTIFY page and make sure @dev is still attached to
  3136. * the port.
  3137. *
  3138. * LOCKING:
  3139. * Kernel thread context (may sleep)
  3140. *
  3141. * RETURNS:
  3142. * 0 on success, negative errno otherwise
  3143. */
  3144. int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
  3145. {
  3146. unsigned int class = dev->class;
  3147. u16 *id = (void *)dev->link->ap->sector_buf;
  3148. int rc;
  3149. /* read ID data */
  3150. rc = ata_dev_read_id(dev, &class, readid_flags, id);
  3151. if (rc)
  3152. return rc;
  3153. /* is the device still there? */
  3154. if (!ata_dev_same_device(dev, class, id))
  3155. return -ENODEV;
  3156. memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
  3157. return 0;
  3158. }
  3159. /**
  3160. * ata_dev_revalidate - Revalidate ATA device
  3161. * @dev: device to revalidate
  3162. * @readid_flags: read ID flags
  3163. *
  3164. * Re-read IDENTIFY page, make sure @dev is still attached to the
  3165. * port and reconfigure it according to the new IDENTIFY page.
  3166. *
  3167. * LOCKING:
  3168. * Kernel thread context (may sleep)
  3169. *
  3170. * RETURNS:
  3171. * 0 on success, negative errno otherwise
  3172. */
  3173. int ata_dev_revalidate(struct ata_device *dev, unsigned int readid_flags)
  3174. {
  3175. u64 n_sectors = dev->n_sectors;
  3176. int rc;
  3177. if (!ata_dev_enabled(dev))
  3178. return -ENODEV;
  3179. /* re-read ID */
  3180. rc = ata_dev_reread_id(dev, readid_flags);
  3181. if (rc)
  3182. goto fail;
  3183. /* configure device according to the new ID */
  3184. rc = ata_dev_configure(dev);
  3185. if (rc)
  3186. goto fail;
  3187. /* verify n_sectors hasn't changed */
  3188. if (dev->class == ATA_DEV_ATA && n_sectors &&
  3189. dev->n_sectors != n_sectors) {
  3190. ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
  3191. "%llu != %llu\n",
  3192. (unsigned long long)n_sectors,
  3193. (unsigned long long)dev->n_sectors);
  3194. /* restore original n_sectors */
  3195. dev->n_sectors = n_sectors;
  3196. rc = -ENODEV;
  3197. goto fail;
  3198. }
  3199. return 0;
  3200. fail:
  3201. ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
  3202. return rc;
  3203. }
  3204. struct ata_blacklist_entry {
  3205. const char *model_num;
  3206. const char *model_rev;
  3207. unsigned long horkage;
  3208. };
  3209. static const struct ata_blacklist_entry ata_device_blacklist [] = {
  3210. /* Devices with DMA related problems under Linux */
  3211. { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
  3212. { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
  3213. { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
  3214. { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
  3215. { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
  3216. { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
  3217. { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
  3218. { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
  3219. { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
  3220. { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
  3221. { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
  3222. { "CRD-84", NULL, ATA_HORKAGE_NODMA },
  3223. { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
  3224. { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
  3225. { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
  3226. { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
  3227. { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
  3228. { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
  3229. { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
  3230. { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
  3231. { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
  3232. { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
  3233. { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
  3234. { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
  3235. { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
  3236. { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
  3237. { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
  3238. { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
  3239. { "SAMSUNG CD-ROM SN-124","N001", ATA_HORKAGE_NODMA },
  3240. { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
  3241. { "IOMEGA ZIP 250 ATAPI", NULL, ATA_HORKAGE_NODMA }, /* temporary fix */
  3242. { "IOMEGA ZIP 250 ATAPI Floppy",
  3243. NULL, ATA_HORKAGE_NODMA },
  3244. /* Weird ATAPI devices */
  3245. { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
  3246. /* Devices we expect to fail diagnostics */
  3247. /* Devices where NCQ should be avoided */
  3248. /* NCQ is slow */
  3249. { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
  3250. /* http://thread.gmane.org/gmane.linux.ide/14907 */
  3251. { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
  3252. /* NCQ is broken */
  3253. { "Maxtor 6L250S0", "BANC1G10", ATA_HORKAGE_NONCQ },
  3254. { "Maxtor 6B200M0", "BANC1BM0", ATA_HORKAGE_NONCQ },
  3255. { "Maxtor 6B200M0", "BANC1B10", ATA_HORKAGE_NONCQ },
  3256. { "Maxtor 7B250S0", "BANC1B70", ATA_HORKAGE_NONCQ, },
  3257. { "Maxtor 7B300S0", "BANC1B70", ATA_HORKAGE_NONCQ },
  3258. { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
  3259. { "HITACHI HDS7250SASUN500G 0621KTAWSD", "K2AOAJ0AHITACHI",
  3260. ATA_HORKAGE_NONCQ },
  3261. /* NCQ hard hangs device under heavier load, needs hard power cycle */
  3262. { "Maxtor 6B250S0", "BANC1B70", ATA_HORKAGE_NONCQ },
  3263. /* Blacklist entries taken from Silicon Image 3124/3132
  3264. Windows driver .inf file - also several Linux problem reports */
  3265. { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
  3266. { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
  3267. { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
  3268. /* Drives which do spurious command completion */
  3269. { "HTS541680J9SA00", "SB2IC7EP", ATA_HORKAGE_NONCQ, },
  3270. { "HTS541612J9SA00", "SBDIC7JP", ATA_HORKAGE_NONCQ, },
  3271. { "Hitachi HTS541616J9SA00", "SB4OC70P", ATA_HORKAGE_NONCQ, },
  3272. { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
  3273. { "FUJITSU MHV2080BH", "00840028", ATA_HORKAGE_NONCQ, },
  3274. { "ST9160821AS", "3.CLF", ATA_HORKAGE_NONCQ, },
  3275. { "ST3160812AS", "3.AD", ATA_HORKAGE_NONCQ, },
  3276. { "SAMSUNG HD401LJ", "ZZ100-15", ATA_HORKAGE_NONCQ, },
  3277. /* devices which puke on READ_NATIVE_MAX */
  3278. { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
  3279. { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
  3280. { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
  3281. { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
  3282. /* End Marker */
  3283. { }
  3284. };
  3285. static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
  3286. {
  3287. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  3288. unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
  3289. const struct ata_blacklist_entry *ad = ata_device_blacklist;
  3290. ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
  3291. ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
  3292. while (ad->model_num) {
  3293. if (!strcmp(ad->model_num, model_num)) {
  3294. if (ad->model_rev == NULL)
  3295. return ad->horkage;
  3296. if (!strcmp(ad->model_rev, model_rev))
  3297. return ad->horkage;
  3298. }
  3299. ad++;
  3300. }
  3301. return 0;
  3302. }
  3303. static int ata_dma_blacklisted(const struct ata_device *dev)
  3304. {
  3305. /* We don't support polling DMA.
  3306. * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
  3307. * if the LLDD handles only interrupts in the HSM_ST_LAST state.
  3308. */
  3309. if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
  3310. (dev->flags & ATA_DFLAG_CDB_INTR))
  3311. return 1;
  3312. return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
  3313. }
  3314. /**
  3315. * ata_dev_xfermask - Compute supported xfermask of the given device
  3316. * @dev: Device to compute xfermask for
  3317. *
  3318. * Compute supported xfermask of @dev and store it in
  3319. * dev->*_mask. This function is responsible for applying all
  3320. * known limits including host controller limits, device
  3321. * blacklist, etc...
  3322. *
  3323. * LOCKING:
  3324. * None.
  3325. */
  3326. static void ata_dev_xfermask(struct ata_device *dev)
  3327. {
  3328. struct ata_link *link = dev->link;
  3329. struct ata_port *ap = link->ap;
  3330. struct ata_host *host = ap->host;
  3331. unsigned long xfer_mask;
  3332. /* controller modes available */
  3333. xfer_mask = ata_pack_xfermask(ap->pio_mask,
  3334. ap->mwdma_mask, ap->udma_mask);
  3335. /* drive modes available */
  3336. xfer_mask &= ata_pack_xfermask(dev->pio_mask,
  3337. dev->mwdma_mask, dev->udma_mask);
  3338. xfer_mask &= ata_id_xfermask(dev->id);
  3339. /*
  3340. * CFA Advanced TrueIDE timings are not allowed on a shared
  3341. * cable
  3342. */
  3343. if (ata_dev_pair(dev)) {
  3344. /* No PIO5 or PIO6 */
  3345. xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
  3346. /* No MWDMA3 or MWDMA 4 */
  3347. xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
  3348. }
  3349. if (ata_dma_blacklisted(dev)) {
  3350. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  3351. ata_dev_printk(dev, KERN_WARNING,
  3352. "device is on DMA blacklist, disabling DMA\n");
  3353. }
  3354. if ((host->flags & ATA_HOST_SIMPLEX) &&
  3355. host->simplex_claimed && host->simplex_claimed != ap) {
  3356. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  3357. ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
  3358. "other device, disabling DMA\n");
  3359. }
  3360. if (ap->flags & ATA_FLAG_NO_IORDY)
  3361. xfer_mask &= ata_pio_mask_no_iordy(dev);
  3362. if (ap->ops->mode_filter)
  3363. xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
  3364. /* Apply cable rule here. Don't apply it early because when
  3365. * we handle hot plug the cable type can itself change.
  3366. * Check this last so that we know if the transfer rate was
  3367. * solely limited by the cable.
  3368. * Unknown or 80 wire cables reported host side are checked
  3369. * drive side as well. Cases where we know a 40wire cable
  3370. * is used safely for 80 are not checked here.
  3371. */
  3372. if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
  3373. /* UDMA/44 or higher would be available */
  3374. if((ap->cbl == ATA_CBL_PATA40) ||
  3375. (ata_drive_40wire(dev->id) &&
  3376. (ap->cbl == ATA_CBL_PATA_UNK ||
  3377. ap->cbl == ATA_CBL_PATA80))) {
  3378. ata_dev_printk(dev, KERN_WARNING,
  3379. "limited to UDMA/33 due to 40-wire cable\n");
  3380. xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
  3381. }
  3382. ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
  3383. &dev->mwdma_mask, &dev->udma_mask);
  3384. }
  3385. /**
  3386. * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
  3387. * @dev: Device to which command will be sent
  3388. *
  3389. * Issue SET FEATURES - XFER MODE command to device @dev
  3390. * on port @ap.
  3391. *
  3392. * LOCKING:
  3393. * PCI/etc. bus probe sem.
  3394. *
  3395. * RETURNS:
  3396. * 0 on success, AC_ERR_* mask otherwise.
  3397. */
  3398. static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
  3399. {
  3400. struct ata_taskfile tf;
  3401. unsigned int err_mask;
  3402. /* set up set-features taskfile */
  3403. DPRINTK("set features - xfer mode\n");
  3404. /* Some controllers and ATAPI devices show flaky interrupt
  3405. * behavior after setting xfer mode. Use polling instead.
  3406. */
  3407. ata_tf_init(dev, &tf);
  3408. tf.command = ATA_CMD_SET_FEATURES;
  3409. tf.feature = SETFEATURES_XFER;
  3410. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
  3411. tf.protocol = ATA_PROT_NODATA;
  3412. tf.nsect = dev->xfer_mode;
  3413. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
  3414. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  3415. return err_mask;
  3416. }
  3417. /**
  3418. * ata_dev_set_AN - Issue SET FEATURES - SATA FEATURES
  3419. * @dev: Device to which command will be sent
  3420. * @enable: Whether to enable or disable the feature
  3421. *
  3422. * Issue SET FEATURES - SATA FEATURES command to device @dev
  3423. * on port @ap with sector count set to indicate Asynchronous
  3424. * Notification feature
  3425. *
  3426. * LOCKING:
  3427. * PCI/etc. bus probe sem.
  3428. *
  3429. * RETURNS:
  3430. * 0 on success, AC_ERR_* mask otherwise.
  3431. */
  3432. static unsigned int ata_dev_set_AN(struct ata_device *dev, u8 enable)
  3433. {
  3434. struct ata_taskfile tf;
  3435. unsigned int err_mask;
  3436. /* set up set-features taskfile */
  3437. DPRINTK("set features - SATA features\n");
  3438. ata_tf_init(dev, &tf);
  3439. tf.command = ATA_CMD_SET_FEATURES;
  3440. tf.feature = enable;
  3441. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  3442. tf.protocol = ATA_PROT_NODATA;
  3443. tf.nsect = SATA_AN;
  3444. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
  3445. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  3446. return err_mask;
  3447. }
  3448. /**
  3449. * ata_dev_init_params - Issue INIT DEV PARAMS command
  3450. * @dev: Device to which command will be sent
  3451. * @heads: Number of heads (taskfile parameter)
  3452. * @sectors: Number of sectors (taskfile parameter)
  3453. *
  3454. * LOCKING:
  3455. * Kernel thread context (may sleep)
  3456. *
  3457. * RETURNS:
  3458. * 0 on success, AC_ERR_* mask otherwise.
  3459. */
  3460. static unsigned int ata_dev_init_params(struct ata_device *dev,
  3461. u16 heads, u16 sectors)
  3462. {
  3463. struct ata_taskfile tf;
  3464. unsigned int err_mask;
  3465. /* Number of sectors per track 1-255. Number of heads 1-16 */
  3466. if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
  3467. return AC_ERR_INVALID;
  3468. /* set up init dev params taskfile */
  3469. DPRINTK("init dev params \n");
  3470. ata_tf_init(dev, &tf);
  3471. tf.command = ATA_CMD_INIT_DEV_PARAMS;
  3472. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  3473. tf.protocol = ATA_PROT_NODATA;
  3474. tf.nsect = sectors;
  3475. tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
  3476. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
  3477. /* A clean abort indicates an original or just out of spec drive
  3478. and we should continue as we issue the setup based on the
  3479. drive reported working geometry */
  3480. if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
  3481. err_mask = 0;
  3482. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  3483. return err_mask;
  3484. }
  3485. /**
  3486. * ata_sg_clean - Unmap DMA memory associated with command
  3487. * @qc: Command containing DMA memory to be released
  3488. *
  3489. * Unmap all mapped DMA memory associated with this command.
  3490. *
  3491. * LOCKING:
  3492. * spin_lock_irqsave(host lock)
  3493. */
  3494. void ata_sg_clean(struct ata_queued_cmd *qc)
  3495. {
  3496. struct ata_port *ap = qc->ap;
  3497. struct scatterlist *sg = qc->__sg;
  3498. int dir = qc->dma_dir;
  3499. void *pad_buf = NULL;
  3500. WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
  3501. WARN_ON(sg == NULL);
  3502. if (qc->flags & ATA_QCFLAG_SINGLE)
  3503. WARN_ON(qc->n_elem > 1);
  3504. VPRINTK("unmapping %u sg elements\n", qc->n_elem);
  3505. /* if we padded the buffer out to 32-bit bound, and data
  3506. * xfer direction is from-device, we must copy from the
  3507. * pad buffer back into the supplied buffer
  3508. */
  3509. if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
  3510. pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  3511. if (qc->flags & ATA_QCFLAG_SG) {
  3512. if (qc->n_elem)
  3513. dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
  3514. /* restore last sg */
  3515. sg[qc->orig_n_elem - 1].length += qc->pad_len;
  3516. if (pad_buf) {
  3517. struct scatterlist *psg = &qc->pad_sgent;
  3518. void *addr = kmap_atomic(psg->page, KM_IRQ0);
  3519. memcpy(addr + psg->offset, pad_buf, qc->pad_len);
  3520. kunmap_atomic(addr, KM_IRQ0);
  3521. }
  3522. } else {
  3523. if (qc->n_elem)
  3524. dma_unmap_single(ap->dev,
  3525. sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
  3526. dir);
  3527. /* restore sg */
  3528. sg->length += qc->pad_len;
  3529. if (pad_buf)
  3530. memcpy(qc->buf_virt + sg->length - qc->pad_len,
  3531. pad_buf, qc->pad_len);
  3532. }
  3533. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  3534. qc->__sg = NULL;
  3535. }
  3536. /**
  3537. * ata_fill_sg - Fill PCI IDE PRD table
  3538. * @qc: Metadata associated with taskfile to be transferred
  3539. *
  3540. * Fill PCI IDE PRD (scatter-gather) table with segments
  3541. * associated with the current disk command.
  3542. *
  3543. * LOCKING:
  3544. * spin_lock_irqsave(host lock)
  3545. *
  3546. */
  3547. static void ata_fill_sg(struct ata_queued_cmd *qc)
  3548. {
  3549. struct ata_port *ap = qc->ap;
  3550. struct scatterlist *sg;
  3551. unsigned int idx;
  3552. WARN_ON(qc->__sg == NULL);
  3553. WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
  3554. idx = 0;
  3555. ata_for_each_sg(sg, qc) {
  3556. u32 addr, offset;
  3557. u32 sg_len, len;
  3558. /* determine if physical DMA addr spans 64K boundary.
  3559. * Note h/w doesn't support 64-bit, so we unconditionally
  3560. * truncate dma_addr_t to u32.
  3561. */
  3562. addr = (u32) sg_dma_address(sg);
  3563. sg_len = sg_dma_len(sg);
  3564. while (sg_len) {
  3565. offset = addr & 0xffff;
  3566. len = sg_len;
  3567. if ((offset + sg_len) > 0x10000)
  3568. len = 0x10000 - offset;
  3569. ap->prd[idx].addr = cpu_to_le32(addr);
  3570. ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
  3571. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
  3572. idx++;
  3573. sg_len -= len;
  3574. addr += len;
  3575. }
  3576. }
  3577. if (idx)
  3578. ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  3579. }
  3580. /**
  3581. * ata_fill_sg_dumb - Fill PCI IDE PRD table
  3582. * @qc: Metadata associated with taskfile to be transferred
  3583. *
  3584. * Fill PCI IDE PRD (scatter-gather) table with segments
  3585. * associated with the current disk command. Perform the fill
  3586. * so that we avoid writing any length 64K records for
  3587. * controllers that don't follow the spec.
  3588. *
  3589. * LOCKING:
  3590. * spin_lock_irqsave(host lock)
  3591. *
  3592. */
  3593. static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
  3594. {
  3595. struct ata_port *ap = qc->ap;
  3596. struct scatterlist *sg;
  3597. unsigned int idx;
  3598. WARN_ON(qc->__sg == NULL);
  3599. WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
  3600. idx = 0;
  3601. ata_for_each_sg(sg, qc) {
  3602. u32 addr, offset;
  3603. u32 sg_len, len, blen;
  3604. /* determine if physical DMA addr spans 64K boundary.
  3605. * Note h/w doesn't support 64-bit, so we unconditionally
  3606. * truncate dma_addr_t to u32.
  3607. */
  3608. addr = (u32) sg_dma_address(sg);
  3609. sg_len = sg_dma_len(sg);
  3610. while (sg_len) {
  3611. offset = addr & 0xffff;
  3612. len = sg_len;
  3613. if ((offset + sg_len) > 0x10000)
  3614. len = 0x10000 - offset;
  3615. blen = len & 0xffff;
  3616. ap->prd[idx].addr = cpu_to_le32(addr);
  3617. if (blen == 0) {
  3618. /* Some PATA chipsets like the CS5530 can't
  3619. cope with 0x0000 meaning 64K as the spec says */
  3620. ap->prd[idx].flags_len = cpu_to_le32(0x8000);
  3621. blen = 0x8000;
  3622. ap->prd[++idx].addr = cpu_to_le32(addr + 0x8000);
  3623. }
  3624. ap->prd[idx].flags_len = cpu_to_le32(blen);
  3625. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
  3626. idx++;
  3627. sg_len -= len;
  3628. addr += len;
  3629. }
  3630. }
  3631. if (idx)
  3632. ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  3633. }
  3634. /**
  3635. * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
  3636. * @qc: Metadata associated with taskfile to check
  3637. *
  3638. * Allow low-level driver to filter ATA PACKET commands, returning
  3639. * a status indicating whether or not it is OK to use DMA for the
  3640. * supplied PACKET command.
  3641. *
  3642. * LOCKING:
  3643. * spin_lock_irqsave(host lock)
  3644. *
  3645. * RETURNS: 0 when ATAPI DMA can be used
  3646. * nonzero otherwise
  3647. */
  3648. int ata_check_atapi_dma(struct ata_queued_cmd *qc)
  3649. {
  3650. struct ata_port *ap = qc->ap;
  3651. /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
  3652. * few ATAPI devices choke on such DMA requests.
  3653. */
  3654. if (unlikely(qc->nbytes & 15))
  3655. return 1;
  3656. if (ap->ops->check_atapi_dma)
  3657. return ap->ops->check_atapi_dma(qc);
  3658. return 0;
  3659. }
  3660. /**
  3661. * ata_qc_prep - Prepare taskfile for submission
  3662. * @qc: Metadata associated with taskfile to be prepared
  3663. *
  3664. * Prepare ATA taskfile for submission.
  3665. *
  3666. * LOCKING:
  3667. * spin_lock_irqsave(host lock)
  3668. */
  3669. void ata_qc_prep(struct ata_queued_cmd *qc)
  3670. {
  3671. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  3672. return;
  3673. ata_fill_sg(qc);
  3674. }
  3675. /**
  3676. * ata_dumb_qc_prep - Prepare taskfile for submission
  3677. * @qc: Metadata associated with taskfile to be prepared
  3678. *
  3679. * Prepare ATA taskfile for submission.
  3680. *
  3681. * LOCKING:
  3682. * spin_lock_irqsave(host lock)
  3683. */
  3684. void ata_dumb_qc_prep(struct ata_queued_cmd *qc)
  3685. {
  3686. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  3687. return;
  3688. ata_fill_sg_dumb(qc);
  3689. }
  3690. void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
  3691. /**
  3692. * ata_sg_init_one - Associate command with memory buffer
  3693. * @qc: Command to be associated
  3694. * @buf: Memory buffer
  3695. * @buflen: Length of memory buffer, in bytes.
  3696. *
  3697. * Initialize the data-related elements of queued_cmd @qc
  3698. * to point to a single memory buffer, @buf of byte length @buflen.
  3699. *
  3700. * LOCKING:
  3701. * spin_lock_irqsave(host lock)
  3702. */
  3703. void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
  3704. {
  3705. qc->flags |= ATA_QCFLAG_SINGLE;
  3706. qc->__sg = &qc->sgent;
  3707. qc->n_elem = 1;
  3708. qc->orig_n_elem = 1;
  3709. qc->buf_virt = buf;
  3710. qc->nbytes = buflen;
  3711. sg_init_one(&qc->sgent, buf, buflen);
  3712. }
  3713. /**
  3714. * ata_sg_init - Associate command with scatter-gather table.
  3715. * @qc: Command to be associated
  3716. * @sg: Scatter-gather table.
  3717. * @n_elem: Number of elements in s/g table.
  3718. *
  3719. * Initialize the data-related elements of queued_cmd @qc
  3720. * to point to a scatter-gather table @sg, containing @n_elem
  3721. * elements.
  3722. *
  3723. * LOCKING:
  3724. * spin_lock_irqsave(host lock)
  3725. */
  3726. void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
  3727. unsigned int n_elem)
  3728. {
  3729. qc->flags |= ATA_QCFLAG_SG;
  3730. qc->__sg = sg;
  3731. qc->n_elem = n_elem;
  3732. qc->orig_n_elem = n_elem;
  3733. }
  3734. /**
  3735. * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
  3736. * @qc: Command with memory buffer to be mapped.
  3737. *
  3738. * DMA-map the memory buffer associated with queued_cmd @qc.
  3739. *
  3740. * LOCKING:
  3741. * spin_lock_irqsave(host lock)
  3742. *
  3743. * RETURNS:
  3744. * Zero on success, negative on error.
  3745. */
  3746. static int ata_sg_setup_one(struct ata_queued_cmd *qc)
  3747. {
  3748. struct ata_port *ap = qc->ap;
  3749. int dir = qc->dma_dir;
  3750. struct scatterlist *sg = qc->__sg;
  3751. dma_addr_t dma_address;
  3752. int trim_sg = 0;
  3753. /* we must lengthen transfers to end on a 32-bit boundary */
  3754. qc->pad_len = sg->length & 3;
  3755. if (qc->pad_len) {
  3756. void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  3757. struct scatterlist *psg = &qc->pad_sgent;
  3758. WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
  3759. memset(pad_buf, 0, ATA_DMA_PAD_SZ);
  3760. if (qc->tf.flags & ATA_TFLAG_WRITE)
  3761. memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
  3762. qc->pad_len);
  3763. sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
  3764. sg_dma_len(psg) = ATA_DMA_PAD_SZ;
  3765. /* trim sg */
  3766. sg->length -= qc->pad_len;
  3767. if (sg->length == 0)
  3768. trim_sg = 1;
  3769. DPRINTK("padding done, sg->length=%u pad_len=%u\n",
  3770. sg->length, qc->pad_len);
  3771. }
  3772. if (trim_sg) {
  3773. qc->n_elem--;
  3774. goto skip_map;
  3775. }
  3776. dma_address = dma_map_single(ap->dev, qc->buf_virt,
  3777. sg->length, dir);
  3778. if (dma_mapping_error(dma_address)) {
  3779. /* restore sg */
  3780. sg->length += qc->pad_len;
  3781. return -1;
  3782. }
  3783. sg_dma_address(sg) = dma_address;
  3784. sg_dma_len(sg) = sg->length;
  3785. skip_map:
  3786. DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
  3787. qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  3788. return 0;
  3789. }
  3790. /**
  3791. * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
  3792. * @qc: Command with scatter-gather table to be mapped.
  3793. *
  3794. * DMA-map the scatter-gather table associated with queued_cmd @qc.
  3795. *
  3796. * LOCKING:
  3797. * spin_lock_irqsave(host lock)
  3798. *
  3799. * RETURNS:
  3800. * Zero on success, negative on error.
  3801. *
  3802. */
  3803. static int ata_sg_setup(struct ata_queued_cmd *qc)
  3804. {
  3805. struct ata_port *ap = qc->ap;
  3806. struct scatterlist *sg = qc->__sg;
  3807. struct scatterlist *lsg = &sg[qc->n_elem - 1];
  3808. int n_elem, pre_n_elem, dir, trim_sg = 0;
  3809. VPRINTK("ENTER, ata%u\n", ap->print_id);
  3810. WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
  3811. /* we must lengthen transfers to end on a 32-bit boundary */
  3812. qc->pad_len = lsg->length & 3;
  3813. if (qc->pad_len) {
  3814. void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  3815. struct scatterlist *psg = &qc->pad_sgent;
  3816. unsigned int offset;
  3817. WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
  3818. memset(pad_buf, 0, ATA_DMA_PAD_SZ);
  3819. /*
  3820. * psg->page/offset are used to copy to-be-written
  3821. * data in this function or read data in ata_sg_clean.
  3822. */
  3823. offset = lsg->offset + lsg->length - qc->pad_len;
  3824. psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
  3825. psg->offset = offset_in_page(offset);
  3826. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  3827. void *addr = kmap_atomic(psg->page, KM_IRQ0);
  3828. memcpy(pad_buf, addr + psg->offset, qc->pad_len);
  3829. kunmap_atomic(addr, KM_IRQ0);
  3830. }
  3831. sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
  3832. sg_dma_len(psg) = ATA_DMA_PAD_SZ;
  3833. /* trim last sg */
  3834. lsg->length -= qc->pad_len;
  3835. if (lsg->length == 0)
  3836. trim_sg = 1;
  3837. DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
  3838. qc->n_elem - 1, lsg->length, qc->pad_len);
  3839. }
  3840. pre_n_elem = qc->n_elem;
  3841. if (trim_sg && pre_n_elem)
  3842. pre_n_elem--;
  3843. if (!pre_n_elem) {
  3844. n_elem = 0;
  3845. goto skip_map;
  3846. }
  3847. dir = qc->dma_dir;
  3848. n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
  3849. if (n_elem < 1) {
  3850. /* restore last sg */
  3851. lsg->length += qc->pad_len;
  3852. return -1;
  3853. }
  3854. DPRINTK("%d sg elements mapped\n", n_elem);
  3855. skip_map:
  3856. qc->n_elem = n_elem;
  3857. return 0;
  3858. }
  3859. /**
  3860. * swap_buf_le16 - swap halves of 16-bit words in place
  3861. * @buf: Buffer to swap
  3862. * @buf_words: Number of 16-bit words in buffer.
  3863. *
  3864. * Swap halves of 16-bit words if needed to convert from
  3865. * little-endian byte order to native cpu byte order, or
  3866. * vice-versa.
  3867. *
  3868. * LOCKING:
  3869. * Inherited from caller.
  3870. */
  3871. void swap_buf_le16(u16 *buf, unsigned int buf_words)
  3872. {
  3873. #ifdef __BIG_ENDIAN
  3874. unsigned int i;
  3875. for (i = 0; i < buf_words; i++)
  3876. buf[i] = le16_to_cpu(buf[i]);
  3877. #endif /* __BIG_ENDIAN */
  3878. }
  3879. /**
  3880. * ata_data_xfer - Transfer data by PIO
  3881. * @adev: device to target
  3882. * @buf: data buffer
  3883. * @buflen: buffer length
  3884. * @write_data: read/write
  3885. *
  3886. * Transfer data from/to the device data register by PIO.
  3887. *
  3888. * LOCKING:
  3889. * Inherited from caller.
  3890. */
  3891. void ata_data_xfer(struct ata_device *adev, unsigned char *buf,
  3892. unsigned int buflen, int write_data)
  3893. {
  3894. struct ata_port *ap = adev->link->ap;
  3895. unsigned int words = buflen >> 1;
  3896. /* Transfer multiple of 2 bytes */
  3897. if (write_data)
  3898. iowrite16_rep(ap->ioaddr.data_addr, buf, words);
  3899. else
  3900. ioread16_rep(ap->ioaddr.data_addr, buf, words);
  3901. /* Transfer trailing 1 byte, if any. */
  3902. if (unlikely(buflen & 0x01)) {
  3903. u16 align_buf[1] = { 0 };
  3904. unsigned char *trailing_buf = buf + buflen - 1;
  3905. if (write_data) {
  3906. memcpy(align_buf, trailing_buf, 1);
  3907. iowrite16(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
  3908. } else {
  3909. align_buf[0] = cpu_to_le16(ioread16(ap->ioaddr.data_addr));
  3910. memcpy(trailing_buf, align_buf, 1);
  3911. }
  3912. }
  3913. }
  3914. /**
  3915. * ata_data_xfer_noirq - Transfer data by PIO
  3916. * @adev: device to target
  3917. * @buf: data buffer
  3918. * @buflen: buffer length
  3919. * @write_data: read/write
  3920. *
  3921. * Transfer data from/to the device data register by PIO. Do the
  3922. * transfer with interrupts disabled.
  3923. *
  3924. * LOCKING:
  3925. * Inherited from caller.
  3926. */
  3927. void ata_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
  3928. unsigned int buflen, int write_data)
  3929. {
  3930. unsigned long flags;
  3931. local_irq_save(flags);
  3932. ata_data_xfer(adev, buf, buflen, write_data);
  3933. local_irq_restore(flags);
  3934. }
  3935. /**
  3936. * ata_pio_sector - Transfer a sector of data.
  3937. * @qc: Command on going
  3938. *
  3939. * Transfer qc->sect_size bytes of data from/to the ATA device.
  3940. *
  3941. * LOCKING:
  3942. * Inherited from caller.
  3943. */
  3944. static void ata_pio_sector(struct ata_queued_cmd *qc)
  3945. {
  3946. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  3947. struct scatterlist *sg = qc->__sg;
  3948. struct ata_port *ap = qc->ap;
  3949. struct page *page;
  3950. unsigned int offset;
  3951. unsigned char *buf;
  3952. if (qc->curbytes == qc->nbytes - qc->sect_size)
  3953. ap->hsm_task_state = HSM_ST_LAST;
  3954. page = sg[qc->cursg].page;
  3955. offset = sg[qc->cursg].offset + qc->cursg_ofs;
  3956. /* get the current page and offset */
  3957. page = nth_page(page, (offset >> PAGE_SHIFT));
  3958. offset %= PAGE_SIZE;
  3959. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  3960. if (PageHighMem(page)) {
  3961. unsigned long flags;
  3962. /* FIXME: use a bounce buffer */
  3963. local_irq_save(flags);
  3964. buf = kmap_atomic(page, KM_IRQ0);
  3965. /* do the actual data transfer */
  3966. ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
  3967. kunmap_atomic(buf, KM_IRQ0);
  3968. local_irq_restore(flags);
  3969. } else {
  3970. buf = page_address(page);
  3971. ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
  3972. }
  3973. qc->curbytes += qc->sect_size;
  3974. qc->cursg_ofs += qc->sect_size;
  3975. if (qc->cursg_ofs == (&sg[qc->cursg])->length) {
  3976. qc->cursg++;
  3977. qc->cursg_ofs = 0;
  3978. }
  3979. }
  3980. /**
  3981. * ata_pio_sectors - Transfer one or many sectors.
  3982. * @qc: Command on going
  3983. *
  3984. * Transfer one or many sectors of data from/to the
  3985. * ATA device for the DRQ request.
  3986. *
  3987. * LOCKING:
  3988. * Inherited from caller.
  3989. */
  3990. static void ata_pio_sectors(struct ata_queued_cmd *qc)
  3991. {
  3992. if (is_multi_taskfile(&qc->tf)) {
  3993. /* READ/WRITE MULTIPLE */
  3994. unsigned int nsect;
  3995. WARN_ON(qc->dev->multi_count == 0);
  3996. nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
  3997. qc->dev->multi_count);
  3998. while (nsect--)
  3999. ata_pio_sector(qc);
  4000. } else
  4001. ata_pio_sector(qc);
  4002. ata_altstatus(qc->ap); /* flush */
  4003. }
  4004. /**
  4005. * atapi_send_cdb - Write CDB bytes to hardware
  4006. * @ap: Port to which ATAPI device is attached.
  4007. * @qc: Taskfile currently active
  4008. *
  4009. * When device has indicated its readiness to accept
  4010. * a CDB, this function is called. Send the CDB.
  4011. *
  4012. * LOCKING:
  4013. * caller.
  4014. */
  4015. static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
  4016. {
  4017. /* send SCSI cdb */
  4018. DPRINTK("send cdb\n");
  4019. WARN_ON(qc->dev->cdb_len < 12);
  4020. ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
  4021. ata_altstatus(ap); /* flush */
  4022. switch (qc->tf.protocol) {
  4023. case ATA_PROT_ATAPI:
  4024. ap->hsm_task_state = HSM_ST;
  4025. break;
  4026. case ATA_PROT_ATAPI_NODATA:
  4027. ap->hsm_task_state = HSM_ST_LAST;
  4028. break;
  4029. case ATA_PROT_ATAPI_DMA:
  4030. ap->hsm_task_state = HSM_ST_LAST;
  4031. /* initiate bmdma */
  4032. ap->ops->bmdma_start(qc);
  4033. break;
  4034. }
  4035. }
  4036. /**
  4037. * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
  4038. * @qc: Command on going
  4039. * @bytes: number of bytes
  4040. *
  4041. * Transfer Transfer data from/to the ATAPI device.
  4042. *
  4043. * LOCKING:
  4044. * Inherited from caller.
  4045. *
  4046. */
  4047. static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
  4048. {
  4049. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  4050. struct scatterlist *sg = qc->__sg;
  4051. struct ata_port *ap = qc->ap;
  4052. struct page *page;
  4053. unsigned char *buf;
  4054. unsigned int offset, count;
  4055. if (qc->curbytes + bytes >= qc->nbytes)
  4056. ap->hsm_task_state = HSM_ST_LAST;
  4057. next_sg:
  4058. if (unlikely(qc->cursg >= qc->n_elem)) {
  4059. /*
  4060. * The end of qc->sg is reached and the device expects
  4061. * more data to transfer. In order not to overrun qc->sg
  4062. * and fulfill length specified in the byte count register,
  4063. * - for read case, discard trailing data from the device
  4064. * - for write case, padding zero data to the device
  4065. */
  4066. u16 pad_buf[1] = { 0 };
  4067. unsigned int words = bytes >> 1;
  4068. unsigned int i;
  4069. if (words) /* warning if bytes > 1 */
  4070. ata_dev_printk(qc->dev, KERN_WARNING,
  4071. "%u bytes trailing data\n", bytes);
  4072. for (i = 0; i < words; i++)
  4073. ap->ops->data_xfer(qc->dev, (unsigned char*)pad_buf, 2, do_write);
  4074. ap->hsm_task_state = HSM_ST_LAST;
  4075. return;
  4076. }
  4077. sg = &qc->__sg[qc->cursg];
  4078. page = sg->page;
  4079. offset = sg->offset + qc->cursg_ofs;
  4080. /* get the current page and offset */
  4081. page = nth_page(page, (offset >> PAGE_SHIFT));
  4082. offset %= PAGE_SIZE;
  4083. /* don't overrun current sg */
  4084. count = min(sg->length - qc->cursg_ofs, bytes);
  4085. /* don't cross page boundaries */
  4086. count = min(count, (unsigned int)PAGE_SIZE - offset);
  4087. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  4088. if (PageHighMem(page)) {
  4089. unsigned long flags;
  4090. /* FIXME: use bounce buffer */
  4091. local_irq_save(flags);
  4092. buf = kmap_atomic(page, KM_IRQ0);
  4093. /* do the actual data transfer */
  4094. ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
  4095. kunmap_atomic(buf, KM_IRQ0);
  4096. local_irq_restore(flags);
  4097. } else {
  4098. buf = page_address(page);
  4099. ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
  4100. }
  4101. bytes -= count;
  4102. qc->curbytes += count;
  4103. qc->cursg_ofs += count;
  4104. if (qc->cursg_ofs == sg->length) {
  4105. qc->cursg++;
  4106. qc->cursg_ofs = 0;
  4107. }
  4108. if (bytes)
  4109. goto next_sg;
  4110. }
  4111. /**
  4112. * atapi_pio_bytes - Transfer data from/to the ATAPI device.
  4113. * @qc: Command on going
  4114. *
  4115. * Transfer Transfer data from/to the ATAPI device.
  4116. *
  4117. * LOCKING:
  4118. * Inherited from caller.
  4119. */
  4120. static void atapi_pio_bytes(struct ata_queued_cmd *qc)
  4121. {
  4122. struct ata_port *ap = qc->ap;
  4123. struct ata_device *dev = qc->dev;
  4124. unsigned int ireason, bc_lo, bc_hi, bytes;
  4125. int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
  4126. /* Abuse qc->result_tf for temp storage of intermediate TF
  4127. * here to save some kernel stack usage.
  4128. * For normal completion, qc->result_tf is not relevant. For
  4129. * error, qc->result_tf is later overwritten by ata_qc_complete().
  4130. * So, the correctness of qc->result_tf is not affected.
  4131. */
  4132. ap->ops->tf_read(ap, &qc->result_tf);
  4133. ireason = qc->result_tf.nsect;
  4134. bc_lo = qc->result_tf.lbam;
  4135. bc_hi = qc->result_tf.lbah;
  4136. bytes = (bc_hi << 8) | bc_lo;
  4137. /* shall be cleared to zero, indicating xfer of data */
  4138. if (ireason & (1 << 0))
  4139. goto err_out;
  4140. /* make sure transfer direction matches expected */
  4141. i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
  4142. if (do_write != i_write)
  4143. goto err_out;
  4144. VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
  4145. __atapi_pio_bytes(qc, bytes);
  4146. ata_altstatus(ap); /* flush */
  4147. return;
  4148. err_out:
  4149. ata_dev_printk(dev, KERN_INFO, "ATAPI check failed\n");
  4150. qc->err_mask |= AC_ERR_HSM;
  4151. ap->hsm_task_state = HSM_ST_ERR;
  4152. }
  4153. /**
  4154. * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
  4155. * @ap: the target ata_port
  4156. * @qc: qc on going
  4157. *
  4158. * RETURNS:
  4159. * 1 if ok in workqueue, 0 otherwise.
  4160. */
  4161. static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
  4162. {
  4163. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4164. return 1;
  4165. if (ap->hsm_task_state == HSM_ST_FIRST) {
  4166. if (qc->tf.protocol == ATA_PROT_PIO &&
  4167. (qc->tf.flags & ATA_TFLAG_WRITE))
  4168. return 1;
  4169. if (is_atapi_taskfile(&qc->tf) &&
  4170. !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  4171. return 1;
  4172. }
  4173. return 0;
  4174. }
  4175. /**
  4176. * ata_hsm_qc_complete - finish a qc running on standard HSM
  4177. * @qc: Command to complete
  4178. * @in_wq: 1 if called from workqueue, 0 otherwise
  4179. *
  4180. * Finish @qc which is running on standard HSM.
  4181. *
  4182. * LOCKING:
  4183. * If @in_wq is zero, spin_lock_irqsave(host lock).
  4184. * Otherwise, none on entry and grabs host lock.
  4185. */
  4186. static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
  4187. {
  4188. struct ata_port *ap = qc->ap;
  4189. unsigned long flags;
  4190. if (ap->ops->error_handler) {
  4191. if (in_wq) {
  4192. spin_lock_irqsave(ap->lock, flags);
  4193. /* EH might have kicked in while host lock is
  4194. * released.
  4195. */
  4196. qc = ata_qc_from_tag(ap, qc->tag);
  4197. if (qc) {
  4198. if (likely(!(qc->err_mask & AC_ERR_HSM))) {
  4199. ap->ops->irq_on(ap);
  4200. ata_qc_complete(qc);
  4201. } else
  4202. ata_port_freeze(ap);
  4203. }
  4204. spin_unlock_irqrestore(ap->lock, flags);
  4205. } else {
  4206. if (likely(!(qc->err_mask & AC_ERR_HSM)))
  4207. ata_qc_complete(qc);
  4208. else
  4209. ata_port_freeze(ap);
  4210. }
  4211. } else {
  4212. if (in_wq) {
  4213. spin_lock_irqsave(ap->lock, flags);
  4214. ap->ops->irq_on(ap);
  4215. ata_qc_complete(qc);
  4216. spin_unlock_irqrestore(ap->lock, flags);
  4217. } else
  4218. ata_qc_complete(qc);
  4219. }
  4220. }
  4221. /**
  4222. * ata_hsm_move - move the HSM to the next state.
  4223. * @ap: the target ata_port
  4224. * @qc: qc on going
  4225. * @status: current device status
  4226. * @in_wq: 1 if called from workqueue, 0 otherwise
  4227. *
  4228. * RETURNS:
  4229. * 1 when poll next status needed, 0 otherwise.
  4230. */
  4231. int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
  4232. u8 status, int in_wq)
  4233. {
  4234. unsigned long flags = 0;
  4235. int poll_next;
  4236. WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
  4237. /* Make sure ata_qc_issue_prot() does not throw things
  4238. * like DMA polling into the workqueue. Notice that
  4239. * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
  4240. */
  4241. WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
  4242. fsm_start:
  4243. DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
  4244. ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
  4245. switch (ap->hsm_task_state) {
  4246. case HSM_ST_FIRST:
  4247. /* Send first data block or PACKET CDB */
  4248. /* If polling, we will stay in the work queue after
  4249. * sending the data. Otherwise, interrupt handler
  4250. * takes over after sending the data.
  4251. */
  4252. poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
  4253. /* check device status */
  4254. if (unlikely((status & ATA_DRQ) == 0)) {
  4255. /* handle BSY=0, DRQ=0 as error */
  4256. if (likely(status & (ATA_ERR | ATA_DF)))
  4257. /* device stops HSM for abort/error */
  4258. qc->err_mask |= AC_ERR_DEV;
  4259. else
  4260. /* HSM violation. Let EH handle this */
  4261. qc->err_mask |= AC_ERR_HSM;
  4262. ap->hsm_task_state = HSM_ST_ERR;
  4263. goto fsm_start;
  4264. }
  4265. /* Device should not ask for data transfer (DRQ=1)
  4266. * when it finds something wrong.
  4267. * We ignore DRQ here and stop the HSM by
  4268. * changing hsm_task_state to HSM_ST_ERR and
  4269. * let the EH abort the command or reset the device.
  4270. */
  4271. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  4272. ata_port_printk(ap, KERN_WARNING, "DRQ=1 with device "
  4273. "error, dev_stat 0x%X\n", status);
  4274. qc->err_mask |= AC_ERR_HSM;
  4275. ap->hsm_task_state = HSM_ST_ERR;
  4276. goto fsm_start;
  4277. }
  4278. /* Send the CDB (atapi) or the first data block (ata pio out).
  4279. * During the state transition, interrupt handler shouldn't
  4280. * be invoked before the data transfer is complete and
  4281. * hsm_task_state is changed. Hence, the following locking.
  4282. */
  4283. if (in_wq)
  4284. spin_lock_irqsave(ap->lock, flags);
  4285. if (qc->tf.protocol == ATA_PROT_PIO) {
  4286. /* PIO data out protocol.
  4287. * send first data block.
  4288. */
  4289. /* ata_pio_sectors() might change the state
  4290. * to HSM_ST_LAST. so, the state is changed here
  4291. * before ata_pio_sectors().
  4292. */
  4293. ap->hsm_task_state = HSM_ST;
  4294. ata_pio_sectors(qc);
  4295. } else
  4296. /* send CDB */
  4297. atapi_send_cdb(ap, qc);
  4298. if (in_wq)
  4299. spin_unlock_irqrestore(ap->lock, flags);
  4300. /* if polling, ata_pio_task() handles the rest.
  4301. * otherwise, interrupt handler takes over from here.
  4302. */
  4303. break;
  4304. case HSM_ST:
  4305. /* complete command or read/write the data register */
  4306. if (qc->tf.protocol == ATA_PROT_ATAPI) {
  4307. /* ATAPI PIO protocol */
  4308. if ((status & ATA_DRQ) == 0) {
  4309. /* No more data to transfer or device error.
  4310. * Device error will be tagged in HSM_ST_LAST.
  4311. */
  4312. ap->hsm_task_state = HSM_ST_LAST;
  4313. goto fsm_start;
  4314. }
  4315. /* Device should not ask for data transfer (DRQ=1)
  4316. * when it finds something wrong.
  4317. * We ignore DRQ here and stop the HSM by
  4318. * changing hsm_task_state to HSM_ST_ERR and
  4319. * let the EH abort the command or reset the device.
  4320. */
  4321. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  4322. ata_port_printk(ap, KERN_WARNING, "DRQ=1 with "
  4323. "device error, dev_stat 0x%X\n",
  4324. status);
  4325. qc->err_mask |= AC_ERR_HSM;
  4326. ap->hsm_task_state = HSM_ST_ERR;
  4327. goto fsm_start;
  4328. }
  4329. atapi_pio_bytes(qc);
  4330. if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
  4331. /* bad ireason reported by device */
  4332. goto fsm_start;
  4333. } else {
  4334. /* ATA PIO protocol */
  4335. if (unlikely((status & ATA_DRQ) == 0)) {
  4336. /* handle BSY=0, DRQ=0 as error */
  4337. if (likely(status & (ATA_ERR | ATA_DF)))
  4338. /* device stops HSM for abort/error */
  4339. qc->err_mask |= AC_ERR_DEV;
  4340. else
  4341. /* HSM violation. Let EH handle this.
  4342. * Phantom devices also trigger this
  4343. * condition. Mark hint.
  4344. */
  4345. qc->err_mask |= AC_ERR_HSM |
  4346. AC_ERR_NODEV_HINT;
  4347. ap->hsm_task_state = HSM_ST_ERR;
  4348. goto fsm_start;
  4349. }
  4350. /* For PIO reads, some devices may ask for
  4351. * data transfer (DRQ=1) alone with ERR=1.
  4352. * We respect DRQ here and transfer one
  4353. * block of junk data before changing the
  4354. * hsm_task_state to HSM_ST_ERR.
  4355. *
  4356. * For PIO writes, ERR=1 DRQ=1 doesn't make
  4357. * sense since the data block has been
  4358. * transferred to the device.
  4359. */
  4360. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  4361. /* data might be corrputed */
  4362. qc->err_mask |= AC_ERR_DEV;
  4363. if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
  4364. ata_pio_sectors(qc);
  4365. status = ata_wait_idle(ap);
  4366. }
  4367. if (status & (ATA_BUSY | ATA_DRQ))
  4368. qc->err_mask |= AC_ERR_HSM;
  4369. /* ata_pio_sectors() might change the
  4370. * state to HSM_ST_LAST. so, the state
  4371. * is changed after ata_pio_sectors().
  4372. */
  4373. ap->hsm_task_state = HSM_ST_ERR;
  4374. goto fsm_start;
  4375. }
  4376. ata_pio_sectors(qc);
  4377. if (ap->hsm_task_state == HSM_ST_LAST &&
  4378. (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
  4379. /* all data read */
  4380. status = ata_wait_idle(ap);
  4381. goto fsm_start;
  4382. }
  4383. }
  4384. poll_next = 1;
  4385. break;
  4386. case HSM_ST_LAST:
  4387. if (unlikely(!ata_ok(status))) {
  4388. qc->err_mask |= __ac_err_mask(status);
  4389. ap->hsm_task_state = HSM_ST_ERR;
  4390. goto fsm_start;
  4391. }
  4392. /* no more data to transfer */
  4393. DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
  4394. ap->print_id, qc->dev->devno, status);
  4395. WARN_ON(qc->err_mask);
  4396. ap->hsm_task_state = HSM_ST_IDLE;
  4397. /* complete taskfile transaction */
  4398. ata_hsm_qc_complete(qc, in_wq);
  4399. poll_next = 0;
  4400. break;
  4401. case HSM_ST_ERR:
  4402. /* make sure qc->err_mask is available to
  4403. * know what's wrong and recover
  4404. */
  4405. WARN_ON(qc->err_mask == 0);
  4406. ap->hsm_task_state = HSM_ST_IDLE;
  4407. /* complete taskfile transaction */
  4408. ata_hsm_qc_complete(qc, in_wq);
  4409. poll_next = 0;
  4410. break;
  4411. default:
  4412. poll_next = 0;
  4413. BUG();
  4414. }
  4415. return poll_next;
  4416. }
  4417. static void ata_pio_task(struct work_struct *work)
  4418. {
  4419. struct ata_port *ap =
  4420. container_of(work, struct ata_port, port_task.work);
  4421. struct ata_queued_cmd *qc = ap->port_task_data;
  4422. u8 status;
  4423. int poll_next;
  4424. fsm_start:
  4425. WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
  4426. /*
  4427. * This is purely heuristic. This is a fast path.
  4428. * Sometimes when we enter, BSY will be cleared in
  4429. * a chk-status or two. If not, the drive is probably seeking
  4430. * or something. Snooze for a couple msecs, then
  4431. * chk-status again. If still busy, queue delayed work.
  4432. */
  4433. status = ata_busy_wait(ap, ATA_BUSY, 5);
  4434. if (status & ATA_BUSY) {
  4435. msleep(2);
  4436. status = ata_busy_wait(ap, ATA_BUSY, 10);
  4437. if (status & ATA_BUSY) {
  4438. ata_port_queue_task(ap, ata_pio_task, qc, ATA_SHORT_PAUSE);
  4439. return;
  4440. }
  4441. }
  4442. /* move the HSM */
  4443. poll_next = ata_hsm_move(ap, qc, status, 1);
  4444. /* another command or interrupt handler
  4445. * may be running at this point.
  4446. */
  4447. if (poll_next)
  4448. goto fsm_start;
  4449. }
  4450. /**
  4451. * ata_qc_new - Request an available ATA command, for queueing
  4452. * @ap: Port associated with device @dev
  4453. * @dev: Device from whom we request an available command structure
  4454. *
  4455. * LOCKING:
  4456. * None.
  4457. */
  4458. static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
  4459. {
  4460. struct ata_queued_cmd *qc = NULL;
  4461. unsigned int i;
  4462. /* no command while frozen */
  4463. if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
  4464. return NULL;
  4465. /* the last tag is reserved for internal command. */
  4466. for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
  4467. if (!test_and_set_bit(i, &ap->qc_allocated)) {
  4468. qc = __ata_qc_from_tag(ap, i);
  4469. break;
  4470. }
  4471. if (qc)
  4472. qc->tag = i;
  4473. return qc;
  4474. }
  4475. /**
  4476. * ata_qc_new_init - Request an available ATA command, and initialize it
  4477. * @dev: Device from whom we request an available command structure
  4478. *
  4479. * LOCKING:
  4480. * None.
  4481. */
  4482. struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
  4483. {
  4484. struct ata_port *ap = dev->link->ap;
  4485. struct ata_queued_cmd *qc;
  4486. qc = ata_qc_new(ap);
  4487. if (qc) {
  4488. qc->scsicmd = NULL;
  4489. qc->ap = ap;
  4490. qc->dev = dev;
  4491. ata_qc_reinit(qc);
  4492. }
  4493. return qc;
  4494. }
  4495. /**
  4496. * ata_qc_free - free unused ata_queued_cmd
  4497. * @qc: Command to complete
  4498. *
  4499. * Designed to free unused ata_queued_cmd object
  4500. * in case something prevents using it.
  4501. *
  4502. * LOCKING:
  4503. * spin_lock_irqsave(host lock)
  4504. */
  4505. void ata_qc_free(struct ata_queued_cmd *qc)
  4506. {
  4507. struct ata_port *ap = qc->ap;
  4508. unsigned int tag;
  4509. WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  4510. qc->flags = 0;
  4511. tag = qc->tag;
  4512. if (likely(ata_tag_valid(tag))) {
  4513. qc->tag = ATA_TAG_POISON;
  4514. clear_bit(tag, &ap->qc_allocated);
  4515. }
  4516. }
  4517. void __ata_qc_complete(struct ata_queued_cmd *qc)
  4518. {
  4519. struct ata_port *ap = qc->ap;
  4520. struct ata_link *link = qc->dev->link;
  4521. WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  4522. WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
  4523. if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
  4524. ata_sg_clean(qc);
  4525. /* command should be marked inactive atomically with qc completion */
  4526. if (qc->tf.protocol == ATA_PROT_NCQ)
  4527. link->sactive &= ~(1 << qc->tag);
  4528. else
  4529. link->active_tag = ATA_TAG_POISON;
  4530. /* atapi: mark qc as inactive to prevent the interrupt handler
  4531. * from completing the command twice later, before the error handler
  4532. * is called. (when rc != 0 and atapi request sense is needed)
  4533. */
  4534. qc->flags &= ~ATA_QCFLAG_ACTIVE;
  4535. ap->qc_active &= ~(1 << qc->tag);
  4536. /* call completion callback */
  4537. qc->complete_fn(qc);
  4538. }
  4539. static void fill_result_tf(struct ata_queued_cmd *qc)
  4540. {
  4541. struct ata_port *ap = qc->ap;
  4542. qc->result_tf.flags = qc->tf.flags;
  4543. ap->ops->tf_read(ap, &qc->result_tf);
  4544. }
  4545. /**
  4546. * ata_qc_complete - Complete an active ATA command
  4547. * @qc: Command to complete
  4548. * @err_mask: ATA Status register contents
  4549. *
  4550. * Indicate to the mid and upper layers that an ATA
  4551. * command has completed, with either an ok or not-ok status.
  4552. *
  4553. * LOCKING:
  4554. * spin_lock_irqsave(host lock)
  4555. */
  4556. void ata_qc_complete(struct ata_queued_cmd *qc)
  4557. {
  4558. struct ata_port *ap = qc->ap;
  4559. /* XXX: New EH and old EH use different mechanisms to
  4560. * synchronize EH with regular execution path.
  4561. *
  4562. * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
  4563. * Normal execution path is responsible for not accessing a
  4564. * failed qc. libata core enforces the rule by returning NULL
  4565. * from ata_qc_from_tag() for failed qcs.
  4566. *
  4567. * Old EH depends on ata_qc_complete() nullifying completion
  4568. * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
  4569. * not synchronize with interrupt handler. Only PIO task is
  4570. * taken care of.
  4571. */
  4572. if (ap->ops->error_handler) {
  4573. WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
  4574. if (unlikely(qc->err_mask))
  4575. qc->flags |= ATA_QCFLAG_FAILED;
  4576. if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
  4577. if (!ata_tag_internal(qc->tag)) {
  4578. /* always fill result TF for failed qc */
  4579. fill_result_tf(qc);
  4580. ata_qc_schedule_eh(qc);
  4581. return;
  4582. }
  4583. }
  4584. /* read result TF if requested */
  4585. if (qc->flags & ATA_QCFLAG_RESULT_TF)
  4586. fill_result_tf(qc);
  4587. __ata_qc_complete(qc);
  4588. } else {
  4589. if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
  4590. return;
  4591. /* read result TF if failed or requested */
  4592. if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
  4593. fill_result_tf(qc);
  4594. __ata_qc_complete(qc);
  4595. }
  4596. }
  4597. /**
  4598. * ata_qc_complete_multiple - Complete multiple qcs successfully
  4599. * @ap: port in question
  4600. * @qc_active: new qc_active mask
  4601. * @finish_qc: LLDD callback invoked before completing a qc
  4602. *
  4603. * Complete in-flight commands. This functions is meant to be
  4604. * called from low-level driver's interrupt routine to complete
  4605. * requests normally. ap->qc_active and @qc_active is compared
  4606. * and commands are completed accordingly.
  4607. *
  4608. * LOCKING:
  4609. * spin_lock_irqsave(host lock)
  4610. *
  4611. * RETURNS:
  4612. * Number of completed commands on success, -errno otherwise.
  4613. */
  4614. int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
  4615. void (*finish_qc)(struct ata_queued_cmd *))
  4616. {
  4617. int nr_done = 0;
  4618. u32 done_mask;
  4619. int i;
  4620. done_mask = ap->qc_active ^ qc_active;
  4621. if (unlikely(done_mask & qc_active)) {
  4622. ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
  4623. "(%08x->%08x)\n", ap->qc_active, qc_active);
  4624. return -EINVAL;
  4625. }
  4626. for (i = 0; i < ATA_MAX_QUEUE; i++) {
  4627. struct ata_queued_cmd *qc;
  4628. if (!(done_mask & (1 << i)))
  4629. continue;
  4630. if ((qc = ata_qc_from_tag(ap, i))) {
  4631. if (finish_qc)
  4632. finish_qc(qc);
  4633. ata_qc_complete(qc);
  4634. nr_done++;
  4635. }
  4636. }
  4637. return nr_done;
  4638. }
  4639. static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
  4640. {
  4641. struct ata_port *ap = qc->ap;
  4642. switch (qc->tf.protocol) {
  4643. case ATA_PROT_NCQ:
  4644. case ATA_PROT_DMA:
  4645. case ATA_PROT_ATAPI_DMA:
  4646. return 1;
  4647. case ATA_PROT_ATAPI:
  4648. case ATA_PROT_PIO:
  4649. if (ap->flags & ATA_FLAG_PIO_DMA)
  4650. return 1;
  4651. /* fall through */
  4652. default:
  4653. return 0;
  4654. }
  4655. /* never reached */
  4656. }
  4657. /**
  4658. * ata_qc_issue - issue taskfile to device
  4659. * @qc: command to issue to device
  4660. *
  4661. * Prepare an ATA command to submission to device.
  4662. * This includes mapping the data into a DMA-able
  4663. * area, filling in the S/G table, and finally
  4664. * writing the taskfile to hardware, starting the command.
  4665. *
  4666. * LOCKING:
  4667. * spin_lock_irqsave(host lock)
  4668. */
  4669. void ata_qc_issue(struct ata_queued_cmd *qc)
  4670. {
  4671. struct ata_port *ap = qc->ap;
  4672. struct ata_link *link = qc->dev->link;
  4673. /* Make sure only one non-NCQ command is outstanding. The
  4674. * check is skipped for old EH because it reuses active qc to
  4675. * request ATAPI sense.
  4676. */
  4677. WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
  4678. if (qc->tf.protocol == ATA_PROT_NCQ) {
  4679. WARN_ON(link->sactive & (1 << qc->tag));
  4680. link->sactive |= 1 << qc->tag;
  4681. } else {
  4682. WARN_ON(link->sactive);
  4683. link->active_tag = qc->tag;
  4684. }
  4685. qc->flags |= ATA_QCFLAG_ACTIVE;
  4686. ap->qc_active |= 1 << qc->tag;
  4687. if (ata_should_dma_map(qc)) {
  4688. if (qc->flags & ATA_QCFLAG_SG) {
  4689. if (ata_sg_setup(qc))
  4690. goto sg_err;
  4691. } else if (qc->flags & ATA_QCFLAG_SINGLE) {
  4692. if (ata_sg_setup_one(qc))
  4693. goto sg_err;
  4694. }
  4695. } else {
  4696. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  4697. }
  4698. ap->ops->qc_prep(qc);
  4699. qc->err_mask |= ap->ops->qc_issue(qc);
  4700. if (unlikely(qc->err_mask))
  4701. goto err;
  4702. return;
  4703. sg_err:
  4704. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  4705. qc->err_mask |= AC_ERR_SYSTEM;
  4706. err:
  4707. ata_qc_complete(qc);
  4708. }
  4709. /**
  4710. * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
  4711. * @qc: command to issue to device
  4712. *
  4713. * Using various libata functions and hooks, this function
  4714. * starts an ATA command. ATA commands are grouped into
  4715. * classes called "protocols", and issuing each type of protocol
  4716. * is slightly different.
  4717. *
  4718. * May be used as the qc_issue() entry in ata_port_operations.
  4719. *
  4720. * LOCKING:
  4721. * spin_lock_irqsave(host lock)
  4722. *
  4723. * RETURNS:
  4724. * Zero on success, AC_ERR_* mask on failure
  4725. */
  4726. unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
  4727. {
  4728. struct ata_port *ap = qc->ap;
  4729. /* Use polling pio if the LLD doesn't handle
  4730. * interrupt driven pio and atapi CDB interrupt.
  4731. */
  4732. if (ap->flags & ATA_FLAG_PIO_POLLING) {
  4733. switch (qc->tf.protocol) {
  4734. case ATA_PROT_PIO:
  4735. case ATA_PROT_NODATA:
  4736. case ATA_PROT_ATAPI:
  4737. case ATA_PROT_ATAPI_NODATA:
  4738. qc->tf.flags |= ATA_TFLAG_POLLING;
  4739. break;
  4740. case ATA_PROT_ATAPI_DMA:
  4741. if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
  4742. /* see ata_dma_blacklisted() */
  4743. BUG();
  4744. break;
  4745. default:
  4746. break;
  4747. }
  4748. }
  4749. /* select the device */
  4750. ata_dev_select(ap, qc->dev->devno, 1, 0);
  4751. /* start the command */
  4752. switch (qc->tf.protocol) {
  4753. case ATA_PROT_NODATA:
  4754. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4755. ata_qc_set_polling(qc);
  4756. ata_tf_to_host(ap, &qc->tf);
  4757. ap->hsm_task_state = HSM_ST_LAST;
  4758. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4759. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4760. break;
  4761. case ATA_PROT_DMA:
  4762. WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
  4763. ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
  4764. ap->ops->bmdma_setup(qc); /* set up bmdma */
  4765. ap->ops->bmdma_start(qc); /* initiate bmdma */
  4766. ap->hsm_task_state = HSM_ST_LAST;
  4767. break;
  4768. case ATA_PROT_PIO:
  4769. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4770. ata_qc_set_polling(qc);
  4771. ata_tf_to_host(ap, &qc->tf);
  4772. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  4773. /* PIO data out protocol */
  4774. ap->hsm_task_state = HSM_ST_FIRST;
  4775. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4776. /* always send first data block using
  4777. * the ata_pio_task() codepath.
  4778. */
  4779. } else {
  4780. /* PIO data in protocol */
  4781. ap->hsm_task_state = HSM_ST;
  4782. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4783. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4784. /* if polling, ata_pio_task() handles the rest.
  4785. * otherwise, interrupt handler takes over from here.
  4786. */
  4787. }
  4788. break;
  4789. case ATA_PROT_ATAPI:
  4790. case ATA_PROT_ATAPI_NODATA:
  4791. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4792. ata_qc_set_polling(qc);
  4793. ata_tf_to_host(ap, &qc->tf);
  4794. ap->hsm_task_state = HSM_ST_FIRST;
  4795. /* send cdb by polling if no cdb interrupt */
  4796. if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
  4797. (qc->tf.flags & ATA_TFLAG_POLLING))
  4798. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4799. break;
  4800. case ATA_PROT_ATAPI_DMA:
  4801. WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
  4802. ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
  4803. ap->ops->bmdma_setup(qc); /* set up bmdma */
  4804. ap->hsm_task_state = HSM_ST_FIRST;
  4805. /* send cdb by polling if no cdb interrupt */
  4806. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  4807. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4808. break;
  4809. default:
  4810. WARN_ON(1);
  4811. return AC_ERR_SYSTEM;
  4812. }
  4813. return 0;
  4814. }
  4815. /**
  4816. * ata_host_intr - Handle host interrupt for given (port, task)
  4817. * @ap: Port on which interrupt arrived (possibly...)
  4818. * @qc: Taskfile currently active in engine
  4819. *
  4820. * Handle host interrupt for given queued command. Currently,
  4821. * only DMA interrupts are handled. All other commands are
  4822. * handled via polling with interrupts disabled (nIEN bit).
  4823. *
  4824. * LOCKING:
  4825. * spin_lock_irqsave(host lock)
  4826. *
  4827. * RETURNS:
  4828. * One if interrupt was handled, zero if not (shared irq).
  4829. */
  4830. inline unsigned int ata_host_intr (struct ata_port *ap,
  4831. struct ata_queued_cmd *qc)
  4832. {
  4833. struct ata_eh_info *ehi = &ap->link.eh_info;
  4834. u8 status, host_stat = 0;
  4835. VPRINTK("ata%u: protocol %d task_state %d\n",
  4836. ap->print_id, qc->tf.protocol, ap->hsm_task_state);
  4837. /* Check whether we are expecting interrupt in this state */
  4838. switch (ap->hsm_task_state) {
  4839. case HSM_ST_FIRST:
  4840. /* Some pre-ATAPI-4 devices assert INTRQ
  4841. * at this state when ready to receive CDB.
  4842. */
  4843. /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
  4844. * The flag was turned on only for atapi devices.
  4845. * No need to check is_atapi_taskfile(&qc->tf) again.
  4846. */
  4847. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  4848. goto idle_irq;
  4849. break;
  4850. case HSM_ST_LAST:
  4851. if (qc->tf.protocol == ATA_PROT_DMA ||
  4852. qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
  4853. /* check status of DMA engine */
  4854. host_stat = ap->ops->bmdma_status(ap);
  4855. VPRINTK("ata%u: host_stat 0x%X\n",
  4856. ap->print_id, host_stat);
  4857. /* if it's not our irq... */
  4858. if (!(host_stat & ATA_DMA_INTR))
  4859. goto idle_irq;
  4860. /* before we do anything else, clear DMA-Start bit */
  4861. ap->ops->bmdma_stop(qc);
  4862. if (unlikely(host_stat & ATA_DMA_ERR)) {
  4863. /* error when transfering data to/from memory */
  4864. qc->err_mask |= AC_ERR_HOST_BUS;
  4865. ap->hsm_task_state = HSM_ST_ERR;
  4866. }
  4867. }
  4868. break;
  4869. case HSM_ST:
  4870. break;
  4871. default:
  4872. goto idle_irq;
  4873. }
  4874. /* check altstatus */
  4875. status = ata_altstatus(ap);
  4876. if (status & ATA_BUSY)
  4877. goto idle_irq;
  4878. /* check main status, clearing INTRQ */
  4879. status = ata_chk_status(ap);
  4880. if (unlikely(status & ATA_BUSY))
  4881. goto idle_irq;
  4882. /* ack bmdma irq events */
  4883. ap->ops->irq_clear(ap);
  4884. ata_hsm_move(ap, qc, status, 0);
  4885. if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
  4886. qc->tf.protocol == ATA_PROT_ATAPI_DMA))
  4887. ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
  4888. return 1; /* irq handled */
  4889. idle_irq:
  4890. ap->stats.idle_irq++;
  4891. #ifdef ATA_IRQ_TRAP
  4892. if ((ap->stats.idle_irq % 1000) == 0) {
  4893. ata_chk_status(ap);
  4894. ap->ops->irq_clear(ap);
  4895. ata_port_printk(ap, KERN_WARNING, "irq trap\n");
  4896. return 1;
  4897. }
  4898. #endif
  4899. return 0; /* irq not handled */
  4900. }
  4901. /**
  4902. * ata_interrupt - Default ATA host interrupt handler
  4903. * @irq: irq line (unused)
  4904. * @dev_instance: pointer to our ata_host information structure
  4905. *
  4906. * Default interrupt handler for PCI IDE devices. Calls
  4907. * ata_host_intr() for each port that is not disabled.
  4908. *
  4909. * LOCKING:
  4910. * Obtains host lock during operation.
  4911. *
  4912. * RETURNS:
  4913. * IRQ_NONE or IRQ_HANDLED.
  4914. */
  4915. irqreturn_t ata_interrupt (int irq, void *dev_instance)
  4916. {
  4917. struct ata_host *host = dev_instance;
  4918. unsigned int i;
  4919. unsigned int handled = 0;
  4920. unsigned long flags;
  4921. /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
  4922. spin_lock_irqsave(&host->lock, flags);
  4923. for (i = 0; i < host->n_ports; i++) {
  4924. struct ata_port *ap;
  4925. ap = host->ports[i];
  4926. if (ap &&
  4927. !(ap->flags & ATA_FLAG_DISABLED)) {
  4928. struct ata_queued_cmd *qc;
  4929. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  4930. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
  4931. (qc->flags & ATA_QCFLAG_ACTIVE))
  4932. handled |= ata_host_intr(ap, qc);
  4933. }
  4934. }
  4935. spin_unlock_irqrestore(&host->lock, flags);
  4936. return IRQ_RETVAL(handled);
  4937. }
  4938. /**
  4939. * sata_scr_valid - test whether SCRs are accessible
  4940. * @link: ATA link to test SCR accessibility for
  4941. *
  4942. * Test whether SCRs are accessible for @link.
  4943. *
  4944. * LOCKING:
  4945. * None.
  4946. *
  4947. * RETURNS:
  4948. * 1 if SCRs are accessible, 0 otherwise.
  4949. */
  4950. int sata_scr_valid(struct ata_link *link)
  4951. {
  4952. struct ata_port *ap = link->ap;
  4953. return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
  4954. }
  4955. /**
  4956. * sata_scr_read - read SCR register of the specified port
  4957. * @link: ATA link to read SCR for
  4958. * @reg: SCR to read
  4959. * @val: Place to store read value
  4960. *
  4961. * Read SCR register @reg of @link into *@val. This function is
  4962. * guaranteed to succeed if the cable type of the port is SATA
  4963. * and the port implements ->scr_read.
  4964. *
  4965. * LOCKING:
  4966. * None.
  4967. *
  4968. * RETURNS:
  4969. * 0 on success, negative errno on failure.
  4970. */
  4971. int sata_scr_read(struct ata_link *link, int reg, u32 *val)
  4972. {
  4973. struct ata_port *ap = link->ap;
  4974. if (sata_scr_valid(link))
  4975. return ap->ops->scr_read(ap, reg, val);
  4976. return -EOPNOTSUPP;
  4977. }
  4978. /**
  4979. * sata_scr_write - write SCR register of the specified port
  4980. * @link: ATA link to write SCR for
  4981. * @reg: SCR to write
  4982. * @val: value to write
  4983. *
  4984. * Write @val to SCR register @reg of @link. This function is
  4985. * guaranteed to succeed if the cable type of the port is SATA
  4986. * and the port implements ->scr_read.
  4987. *
  4988. * LOCKING:
  4989. * None.
  4990. *
  4991. * RETURNS:
  4992. * 0 on success, negative errno on failure.
  4993. */
  4994. int sata_scr_write(struct ata_link *link, int reg, u32 val)
  4995. {
  4996. struct ata_port *ap = link->ap;
  4997. if (sata_scr_valid(link))
  4998. return ap->ops->scr_write(ap, reg, val);
  4999. return -EOPNOTSUPP;
  5000. }
  5001. /**
  5002. * sata_scr_write_flush - write SCR register of the specified port and flush
  5003. * @link: ATA link to write SCR for
  5004. * @reg: SCR to write
  5005. * @val: value to write
  5006. *
  5007. * This function is identical to sata_scr_write() except that this
  5008. * function performs flush after writing to the register.
  5009. *
  5010. * LOCKING:
  5011. * None.
  5012. *
  5013. * RETURNS:
  5014. * 0 on success, negative errno on failure.
  5015. */
  5016. int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
  5017. {
  5018. struct ata_port *ap = link->ap;
  5019. int rc;
  5020. if (sata_scr_valid(link)) {
  5021. rc = ap->ops->scr_write(ap, reg, val);
  5022. if (rc == 0)
  5023. rc = ap->ops->scr_read(ap, reg, &val);
  5024. return rc;
  5025. }
  5026. return -EOPNOTSUPP;
  5027. }
  5028. /**
  5029. * ata_link_online - test whether the given link is online
  5030. * @link: ATA link to test
  5031. *
  5032. * Test whether @link is online. Note that this function returns
  5033. * 0 if online status of @link cannot be obtained, so
  5034. * ata_link_online(link) != !ata_link_offline(link).
  5035. *
  5036. * LOCKING:
  5037. * None.
  5038. *
  5039. * RETURNS:
  5040. * 1 if the port online status is available and online.
  5041. */
  5042. int ata_link_online(struct ata_link *link)
  5043. {
  5044. u32 sstatus;
  5045. if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
  5046. (sstatus & 0xf) == 0x3)
  5047. return 1;
  5048. return 0;
  5049. }
  5050. /**
  5051. * ata_link_offline - test whether the given link is offline
  5052. * @link: ATA link to test
  5053. *
  5054. * Test whether @link is offline. Note that this function
  5055. * returns 0 if offline status of @link cannot be obtained, so
  5056. * ata_link_online(link) != !ata_link_offline(link).
  5057. *
  5058. * LOCKING:
  5059. * None.
  5060. *
  5061. * RETURNS:
  5062. * 1 if the port offline status is available and offline.
  5063. */
  5064. int ata_link_offline(struct ata_link *link)
  5065. {
  5066. u32 sstatus;
  5067. if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
  5068. (sstatus & 0xf) != 0x3)
  5069. return 1;
  5070. return 0;
  5071. }
  5072. int ata_flush_cache(struct ata_device *dev)
  5073. {
  5074. unsigned int err_mask;
  5075. u8 cmd;
  5076. if (!ata_try_flush_cache(dev))
  5077. return 0;
  5078. if (dev->flags & ATA_DFLAG_FLUSH_EXT)
  5079. cmd = ATA_CMD_FLUSH_EXT;
  5080. else
  5081. cmd = ATA_CMD_FLUSH;
  5082. /* This is wrong. On a failed flush we get back the LBA of the lost
  5083. sector and we should (assuming it wasn't aborted as unknown) issue
  5084. a further flush command to continue the writeback until it
  5085. does not error */
  5086. err_mask = ata_do_simple_cmd(dev, cmd);
  5087. if (err_mask) {
  5088. ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
  5089. return -EIO;
  5090. }
  5091. return 0;
  5092. }
  5093. #ifdef CONFIG_PM
  5094. static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
  5095. unsigned int action, unsigned int ehi_flags,
  5096. int wait)
  5097. {
  5098. unsigned long flags;
  5099. int i, rc;
  5100. for (i = 0; i < host->n_ports; i++) {
  5101. struct ata_port *ap = host->ports[i];
  5102. struct ata_link *link;
  5103. /* Previous resume operation might still be in
  5104. * progress. Wait for PM_PENDING to clear.
  5105. */
  5106. if (ap->pflags & ATA_PFLAG_PM_PENDING) {
  5107. ata_port_wait_eh(ap);
  5108. WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
  5109. }
  5110. /* request PM ops to EH */
  5111. spin_lock_irqsave(ap->lock, flags);
  5112. ap->pm_mesg = mesg;
  5113. if (wait) {
  5114. rc = 0;
  5115. ap->pm_result = &rc;
  5116. }
  5117. ap->pflags |= ATA_PFLAG_PM_PENDING;
  5118. __ata_port_for_each_link(link, ap) {
  5119. link->eh_info.action |= action;
  5120. link->eh_info.flags |= ehi_flags;
  5121. }
  5122. ata_port_schedule_eh(ap);
  5123. spin_unlock_irqrestore(ap->lock, flags);
  5124. /* wait and check result */
  5125. if (wait) {
  5126. ata_port_wait_eh(ap);
  5127. WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
  5128. if (rc)
  5129. return rc;
  5130. }
  5131. }
  5132. return 0;
  5133. }
  5134. /**
  5135. * ata_host_suspend - suspend host
  5136. * @host: host to suspend
  5137. * @mesg: PM message
  5138. *
  5139. * Suspend @host. Actual operation is performed by EH. This
  5140. * function requests EH to perform PM operations and waits for EH
  5141. * to finish.
  5142. *
  5143. * LOCKING:
  5144. * Kernel thread context (may sleep).
  5145. *
  5146. * RETURNS:
  5147. * 0 on success, -errno on failure.
  5148. */
  5149. int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
  5150. {
  5151. int rc;
  5152. rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
  5153. if (rc == 0)
  5154. host->dev->power.power_state = mesg;
  5155. return rc;
  5156. }
  5157. /**
  5158. * ata_host_resume - resume host
  5159. * @host: host to resume
  5160. *
  5161. * Resume @host. Actual operation is performed by EH. This
  5162. * function requests EH to perform PM operations and returns.
  5163. * Note that all resume operations are performed parallely.
  5164. *
  5165. * LOCKING:
  5166. * Kernel thread context (may sleep).
  5167. */
  5168. void ata_host_resume(struct ata_host *host)
  5169. {
  5170. ata_host_request_pm(host, PMSG_ON, ATA_EH_SOFTRESET,
  5171. ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
  5172. host->dev->power.power_state = PMSG_ON;
  5173. }
  5174. #endif
  5175. /**
  5176. * ata_port_start - Set port up for dma.
  5177. * @ap: Port to initialize
  5178. *
  5179. * Called just after data structures for each port are
  5180. * initialized. Allocates space for PRD table.
  5181. *
  5182. * May be used as the port_start() entry in ata_port_operations.
  5183. *
  5184. * LOCKING:
  5185. * Inherited from caller.
  5186. */
  5187. int ata_port_start(struct ata_port *ap)
  5188. {
  5189. struct device *dev = ap->dev;
  5190. int rc;
  5191. ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
  5192. GFP_KERNEL);
  5193. if (!ap->prd)
  5194. return -ENOMEM;
  5195. rc = ata_pad_alloc(ap, dev);
  5196. if (rc)
  5197. return rc;
  5198. DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd,
  5199. (unsigned long long)ap->prd_dma);
  5200. return 0;
  5201. }
  5202. /**
  5203. * ata_dev_init - Initialize an ata_device structure
  5204. * @dev: Device structure to initialize
  5205. *
  5206. * Initialize @dev in preparation for probing.
  5207. *
  5208. * LOCKING:
  5209. * Inherited from caller.
  5210. */
  5211. void ata_dev_init(struct ata_device *dev)
  5212. {
  5213. struct ata_link *link = dev->link;
  5214. struct ata_port *ap = link->ap;
  5215. unsigned long flags;
  5216. /* SATA spd limit is bound to the first device */
  5217. link->sata_spd_limit = link->hw_sata_spd_limit;
  5218. link->sata_spd = 0;
  5219. /* High bits of dev->flags are used to record warm plug
  5220. * requests which occur asynchronously. Synchronize using
  5221. * host lock.
  5222. */
  5223. spin_lock_irqsave(ap->lock, flags);
  5224. dev->flags &= ~ATA_DFLAG_INIT_MASK;
  5225. dev->horkage = 0;
  5226. spin_unlock_irqrestore(ap->lock, flags);
  5227. memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
  5228. sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
  5229. dev->pio_mask = UINT_MAX;
  5230. dev->mwdma_mask = UINT_MAX;
  5231. dev->udma_mask = UINT_MAX;
  5232. }
  5233. /**
  5234. * ata_link_init - Initialize an ata_link structure
  5235. * @ap: ATA port link is attached to
  5236. * @link: Link structure to initialize
  5237. * @pmp: Port multiplier port number
  5238. *
  5239. * Initialize @link.
  5240. *
  5241. * LOCKING:
  5242. * Kernel thread context (may sleep)
  5243. */
  5244. static void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
  5245. {
  5246. int i;
  5247. /* clear everything except for devices */
  5248. memset(link, 0, offsetof(struct ata_link, device[0]));
  5249. link->ap = ap;
  5250. link->pmp = pmp;
  5251. link->active_tag = ATA_TAG_POISON;
  5252. link->hw_sata_spd_limit = UINT_MAX;
  5253. /* can't use iterator, ap isn't initialized yet */
  5254. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  5255. struct ata_device *dev = &link->device[i];
  5256. dev->link = link;
  5257. dev->devno = dev - link->device;
  5258. ata_dev_init(dev);
  5259. }
  5260. }
  5261. /**
  5262. * sata_link_init_spd - Initialize link->sata_spd_limit
  5263. * @link: Link to configure sata_spd_limit for
  5264. *
  5265. * Initialize @link->[hw_]sata_spd_limit to the currently
  5266. * configured value.
  5267. *
  5268. * LOCKING:
  5269. * Kernel thread context (may sleep).
  5270. *
  5271. * RETURNS:
  5272. * 0 on success, -errno on failure.
  5273. */
  5274. static int sata_link_init_spd(struct ata_link *link)
  5275. {
  5276. u32 scontrol, spd;
  5277. int rc;
  5278. rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
  5279. if (rc)
  5280. return rc;
  5281. spd = (scontrol >> 4) & 0xf;
  5282. if (spd)
  5283. link->hw_sata_spd_limit &= (1 << spd) - 1;
  5284. link->sata_spd_limit = link->hw_sata_spd_limit;
  5285. return 0;
  5286. }
  5287. /**
  5288. * ata_port_alloc - allocate and initialize basic ATA port resources
  5289. * @host: ATA host this allocated port belongs to
  5290. *
  5291. * Allocate and initialize basic ATA port resources.
  5292. *
  5293. * RETURNS:
  5294. * Allocate ATA port on success, NULL on failure.
  5295. *
  5296. * LOCKING:
  5297. * Inherited from calling layer (may sleep).
  5298. */
  5299. struct ata_port *ata_port_alloc(struct ata_host *host)
  5300. {
  5301. struct ata_port *ap;
  5302. DPRINTK("ENTER\n");
  5303. ap = kzalloc(sizeof(*ap), GFP_KERNEL);
  5304. if (!ap)
  5305. return NULL;
  5306. ap->pflags |= ATA_PFLAG_INITIALIZING;
  5307. ap->lock = &host->lock;
  5308. ap->flags = ATA_FLAG_DISABLED;
  5309. ap->print_id = -1;
  5310. ap->ctl = ATA_DEVCTL_OBS;
  5311. ap->host = host;
  5312. ap->dev = host->dev;
  5313. ap->last_ctl = 0xFF;
  5314. #if defined(ATA_VERBOSE_DEBUG)
  5315. /* turn on all debugging levels */
  5316. ap->msg_enable = 0x00FF;
  5317. #elif defined(ATA_DEBUG)
  5318. ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
  5319. #else
  5320. ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
  5321. #endif
  5322. INIT_DELAYED_WORK(&ap->port_task, NULL);
  5323. INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
  5324. INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
  5325. INIT_LIST_HEAD(&ap->eh_done_q);
  5326. init_waitqueue_head(&ap->eh_wait_q);
  5327. init_timer_deferrable(&ap->fastdrain_timer);
  5328. ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
  5329. ap->fastdrain_timer.data = (unsigned long)ap;
  5330. ap->cbl = ATA_CBL_NONE;
  5331. ata_link_init(ap, &ap->link, 0);
  5332. #ifdef ATA_IRQ_TRAP
  5333. ap->stats.unhandled_irq = 1;
  5334. ap->stats.idle_irq = 1;
  5335. #endif
  5336. return ap;
  5337. }
  5338. static void ata_host_release(struct device *gendev, void *res)
  5339. {
  5340. struct ata_host *host = dev_get_drvdata(gendev);
  5341. int i;
  5342. for (i = 0; i < host->n_ports; i++) {
  5343. struct ata_port *ap = host->ports[i];
  5344. if (!ap)
  5345. continue;
  5346. if ((host->flags & ATA_HOST_STARTED) && ap->ops->port_stop)
  5347. ap->ops->port_stop(ap);
  5348. }
  5349. if ((host->flags & ATA_HOST_STARTED) && host->ops->host_stop)
  5350. host->ops->host_stop(host);
  5351. for (i = 0; i < host->n_ports; i++) {
  5352. struct ata_port *ap = host->ports[i];
  5353. if (!ap)
  5354. continue;
  5355. if (ap->scsi_host)
  5356. scsi_host_put(ap->scsi_host);
  5357. kfree(ap);
  5358. host->ports[i] = NULL;
  5359. }
  5360. dev_set_drvdata(gendev, NULL);
  5361. }
  5362. /**
  5363. * ata_host_alloc - allocate and init basic ATA host resources
  5364. * @dev: generic device this host is associated with
  5365. * @max_ports: maximum number of ATA ports associated with this host
  5366. *
  5367. * Allocate and initialize basic ATA host resources. LLD calls
  5368. * this function to allocate a host, initializes it fully and
  5369. * attaches it using ata_host_register().
  5370. *
  5371. * @max_ports ports are allocated and host->n_ports is
  5372. * initialized to @max_ports. The caller is allowed to decrease
  5373. * host->n_ports before calling ata_host_register(). The unused
  5374. * ports will be automatically freed on registration.
  5375. *
  5376. * RETURNS:
  5377. * Allocate ATA host on success, NULL on failure.
  5378. *
  5379. * LOCKING:
  5380. * Inherited from calling layer (may sleep).
  5381. */
  5382. struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
  5383. {
  5384. struct ata_host *host;
  5385. size_t sz;
  5386. int i;
  5387. DPRINTK("ENTER\n");
  5388. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  5389. return NULL;
  5390. /* alloc a container for our list of ATA ports (buses) */
  5391. sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
  5392. /* alloc a container for our list of ATA ports (buses) */
  5393. host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
  5394. if (!host)
  5395. goto err_out;
  5396. devres_add(dev, host);
  5397. dev_set_drvdata(dev, host);
  5398. spin_lock_init(&host->lock);
  5399. host->dev = dev;
  5400. host->n_ports = max_ports;
  5401. /* allocate ports bound to this host */
  5402. for (i = 0; i < max_ports; i++) {
  5403. struct ata_port *ap;
  5404. ap = ata_port_alloc(host);
  5405. if (!ap)
  5406. goto err_out;
  5407. ap->port_no = i;
  5408. host->ports[i] = ap;
  5409. }
  5410. devres_remove_group(dev, NULL);
  5411. return host;
  5412. err_out:
  5413. devres_release_group(dev, NULL);
  5414. return NULL;
  5415. }
  5416. /**
  5417. * ata_host_alloc_pinfo - alloc host and init with port_info array
  5418. * @dev: generic device this host is associated with
  5419. * @ppi: array of ATA port_info to initialize host with
  5420. * @n_ports: number of ATA ports attached to this host
  5421. *
  5422. * Allocate ATA host and initialize with info from @ppi. If NULL
  5423. * terminated, @ppi may contain fewer entries than @n_ports. The
  5424. * last entry will be used for the remaining ports.
  5425. *
  5426. * RETURNS:
  5427. * Allocate ATA host on success, NULL on failure.
  5428. *
  5429. * LOCKING:
  5430. * Inherited from calling layer (may sleep).
  5431. */
  5432. struct ata_host *ata_host_alloc_pinfo(struct device *dev,
  5433. const struct ata_port_info * const * ppi,
  5434. int n_ports)
  5435. {
  5436. const struct ata_port_info *pi;
  5437. struct ata_host *host;
  5438. int i, j;
  5439. host = ata_host_alloc(dev, n_ports);
  5440. if (!host)
  5441. return NULL;
  5442. for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
  5443. struct ata_port *ap = host->ports[i];
  5444. if (ppi[j])
  5445. pi = ppi[j++];
  5446. ap->pio_mask = pi->pio_mask;
  5447. ap->mwdma_mask = pi->mwdma_mask;
  5448. ap->udma_mask = pi->udma_mask;
  5449. ap->flags |= pi->flags;
  5450. ap->link.flags |= pi->link_flags;
  5451. ap->ops = pi->port_ops;
  5452. if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
  5453. host->ops = pi->port_ops;
  5454. if (!host->private_data && pi->private_data)
  5455. host->private_data = pi->private_data;
  5456. }
  5457. return host;
  5458. }
  5459. /**
  5460. * ata_host_start - start and freeze ports of an ATA host
  5461. * @host: ATA host to start ports for
  5462. *
  5463. * Start and then freeze ports of @host. Started status is
  5464. * recorded in host->flags, so this function can be called
  5465. * multiple times. Ports are guaranteed to get started only
  5466. * once. If host->ops isn't initialized yet, its set to the
  5467. * first non-dummy port ops.
  5468. *
  5469. * LOCKING:
  5470. * Inherited from calling layer (may sleep).
  5471. *
  5472. * RETURNS:
  5473. * 0 if all ports are started successfully, -errno otherwise.
  5474. */
  5475. int ata_host_start(struct ata_host *host)
  5476. {
  5477. int i, rc;
  5478. if (host->flags & ATA_HOST_STARTED)
  5479. return 0;
  5480. for (i = 0; i < host->n_ports; i++) {
  5481. struct ata_port *ap = host->ports[i];
  5482. if (!host->ops && !ata_port_is_dummy(ap))
  5483. host->ops = ap->ops;
  5484. if (ap->ops->port_start) {
  5485. rc = ap->ops->port_start(ap);
  5486. if (rc) {
  5487. ata_port_printk(ap, KERN_ERR, "failed to "
  5488. "start port (errno=%d)\n", rc);
  5489. goto err_out;
  5490. }
  5491. }
  5492. ata_eh_freeze_port(ap);
  5493. }
  5494. host->flags |= ATA_HOST_STARTED;
  5495. return 0;
  5496. err_out:
  5497. while (--i >= 0) {
  5498. struct ata_port *ap = host->ports[i];
  5499. if (ap->ops->port_stop)
  5500. ap->ops->port_stop(ap);
  5501. }
  5502. return rc;
  5503. }
  5504. /**
  5505. * ata_sas_host_init - Initialize a host struct
  5506. * @host: host to initialize
  5507. * @dev: device host is attached to
  5508. * @flags: host flags
  5509. * @ops: port_ops
  5510. *
  5511. * LOCKING:
  5512. * PCI/etc. bus probe sem.
  5513. *
  5514. */
  5515. /* KILLME - the only user left is ipr */
  5516. void ata_host_init(struct ata_host *host, struct device *dev,
  5517. unsigned long flags, const struct ata_port_operations *ops)
  5518. {
  5519. spin_lock_init(&host->lock);
  5520. host->dev = dev;
  5521. host->flags = flags;
  5522. host->ops = ops;
  5523. }
  5524. /**
  5525. * ata_host_register - register initialized ATA host
  5526. * @host: ATA host to register
  5527. * @sht: template for SCSI host
  5528. *
  5529. * Register initialized ATA host. @host is allocated using
  5530. * ata_host_alloc() and fully initialized by LLD. This function
  5531. * starts ports, registers @host with ATA and SCSI layers and
  5532. * probe registered devices.
  5533. *
  5534. * LOCKING:
  5535. * Inherited from calling layer (may sleep).
  5536. *
  5537. * RETURNS:
  5538. * 0 on success, -errno otherwise.
  5539. */
  5540. int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
  5541. {
  5542. int i, rc;
  5543. /* host must have been started */
  5544. if (!(host->flags & ATA_HOST_STARTED)) {
  5545. dev_printk(KERN_ERR, host->dev,
  5546. "BUG: trying to register unstarted host\n");
  5547. WARN_ON(1);
  5548. return -EINVAL;
  5549. }
  5550. /* Blow away unused ports. This happens when LLD can't
  5551. * determine the exact number of ports to allocate at
  5552. * allocation time.
  5553. */
  5554. for (i = host->n_ports; host->ports[i]; i++)
  5555. kfree(host->ports[i]);
  5556. /* give ports names and add SCSI hosts */
  5557. for (i = 0; i < host->n_ports; i++)
  5558. host->ports[i]->print_id = ata_print_id++;
  5559. rc = ata_scsi_add_hosts(host, sht);
  5560. if (rc)
  5561. return rc;
  5562. /* associate with ACPI nodes */
  5563. ata_acpi_associate(host);
  5564. /* set cable, sata_spd_limit and report */
  5565. for (i = 0; i < host->n_ports; i++) {
  5566. struct ata_port *ap = host->ports[i];
  5567. int irq_line;
  5568. unsigned long xfer_mask;
  5569. /* set SATA cable type if still unset */
  5570. if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
  5571. ap->cbl = ATA_CBL_SATA;
  5572. /* init sata_spd_limit to the current value */
  5573. sata_link_init_spd(&ap->link);
  5574. /* report the secondary IRQ for second channel legacy */
  5575. irq_line = host->irq;
  5576. if (i == 1 && host->irq2)
  5577. irq_line = host->irq2;
  5578. xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
  5579. ap->udma_mask);
  5580. /* print per-port info to dmesg */
  5581. if (!ata_port_is_dummy(ap))
  5582. ata_port_printk(ap, KERN_INFO, "%cATA max %s cmd 0x%p "
  5583. "ctl 0x%p bmdma 0x%p irq %d\n",
  5584. (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
  5585. ata_mode_string(xfer_mask),
  5586. ap->ioaddr.cmd_addr,
  5587. ap->ioaddr.ctl_addr,
  5588. ap->ioaddr.bmdma_addr,
  5589. irq_line);
  5590. else
  5591. ata_port_printk(ap, KERN_INFO, "DUMMY\n");
  5592. }
  5593. /* perform each probe synchronously */
  5594. DPRINTK("probe begin\n");
  5595. for (i = 0; i < host->n_ports; i++) {
  5596. struct ata_port *ap = host->ports[i];
  5597. int rc;
  5598. /* probe */
  5599. if (ap->ops->error_handler) {
  5600. struct ata_eh_info *ehi = &ap->link.eh_info;
  5601. unsigned long flags;
  5602. ata_port_probe(ap);
  5603. /* kick EH for boot probing */
  5604. spin_lock_irqsave(ap->lock, flags);
  5605. ehi->probe_mask =
  5606. (1 << ata_link_max_devices(&ap->link)) - 1;
  5607. ehi->action |= ATA_EH_SOFTRESET;
  5608. ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
  5609. ap->pflags &= ~ATA_PFLAG_INITIALIZING;
  5610. ap->pflags |= ATA_PFLAG_LOADING;
  5611. ata_port_schedule_eh(ap);
  5612. spin_unlock_irqrestore(ap->lock, flags);
  5613. /* wait for EH to finish */
  5614. ata_port_wait_eh(ap);
  5615. } else {
  5616. DPRINTK("ata%u: bus probe begin\n", ap->print_id);
  5617. rc = ata_bus_probe(ap);
  5618. DPRINTK("ata%u: bus probe end\n", ap->print_id);
  5619. if (rc) {
  5620. /* FIXME: do something useful here?
  5621. * Current libata behavior will
  5622. * tear down everything when
  5623. * the module is removed
  5624. * or the h/w is unplugged.
  5625. */
  5626. }
  5627. }
  5628. }
  5629. /* probes are done, now scan each port's disk(s) */
  5630. DPRINTK("host probe begin\n");
  5631. for (i = 0; i < host->n_ports; i++) {
  5632. struct ata_port *ap = host->ports[i];
  5633. ata_scsi_scan_host(ap, 1);
  5634. }
  5635. return 0;
  5636. }
  5637. /**
  5638. * ata_host_activate - start host, request IRQ and register it
  5639. * @host: target ATA host
  5640. * @irq: IRQ to request
  5641. * @irq_handler: irq_handler used when requesting IRQ
  5642. * @irq_flags: irq_flags used when requesting IRQ
  5643. * @sht: scsi_host_template to use when registering the host
  5644. *
  5645. * After allocating an ATA host and initializing it, most libata
  5646. * LLDs perform three steps to activate the host - start host,
  5647. * request IRQ and register it. This helper takes necessasry
  5648. * arguments and performs the three steps in one go.
  5649. *
  5650. * LOCKING:
  5651. * Inherited from calling layer (may sleep).
  5652. *
  5653. * RETURNS:
  5654. * 0 on success, -errno otherwise.
  5655. */
  5656. int ata_host_activate(struct ata_host *host, int irq,
  5657. irq_handler_t irq_handler, unsigned long irq_flags,
  5658. struct scsi_host_template *sht)
  5659. {
  5660. int rc;
  5661. rc = ata_host_start(host);
  5662. if (rc)
  5663. return rc;
  5664. rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
  5665. dev_driver_string(host->dev), host);
  5666. if (rc)
  5667. return rc;
  5668. /* Used to print device info at probe */
  5669. host->irq = irq;
  5670. rc = ata_host_register(host, sht);
  5671. /* if failed, just free the IRQ and leave ports alone */
  5672. if (rc)
  5673. devm_free_irq(host->dev, irq, host);
  5674. return rc;
  5675. }
  5676. /**
  5677. * ata_port_detach - Detach ATA port in prepration of device removal
  5678. * @ap: ATA port to be detached
  5679. *
  5680. * Detach all ATA devices and the associated SCSI devices of @ap;
  5681. * then, remove the associated SCSI host. @ap is guaranteed to
  5682. * be quiescent on return from this function.
  5683. *
  5684. * LOCKING:
  5685. * Kernel thread context (may sleep).
  5686. */
  5687. void ata_port_detach(struct ata_port *ap)
  5688. {
  5689. unsigned long flags;
  5690. struct ata_link *link;
  5691. struct ata_device *dev;
  5692. if (!ap->ops->error_handler)
  5693. goto skip_eh;
  5694. /* tell EH we're leaving & flush EH */
  5695. spin_lock_irqsave(ap->lock, flags);
  5696. ap->pflags |= ATA_PFLAG_UNLOADING;
  5697. spin_unlock_irqrestore(ap->lock, flags);
  5698. ata_port_wait_eh(ap);
  5699. /* EH is now guaranteed to see UNLOADING, so no new device
  5700. * will be attached. Disable all existing devices.
  5701. */
  5702. spin_lock_irqsave(ap->lock, flags);
  5703. ata_port_for_each_link(link, ap) {
  5704. ata_link_for_each_dev(dev, link)
  5705. ata_dev_disable(dev);
  5706. }
  5707. spin_unlock_irqrestore(ap->lock, flags);
  5708. /* Final freeze & EH. All in-flight commands are aborted. EH
  5709. * will be skipped and retrials will be terminated with bad
  5710. * target.
  5711. */
  5712. spin_lock_irqsave(ap->lock, flags);
  5713. ata_port_freeze(ap); /* won't be thawed */
  5714. spin_unlock_irqrestore(ap->lock, flags);
  5715. ata_port_wait_eh(ap);
  5716. cancel_rearming_delayed_work(&ap->hotplug_task);
  5717. skip_eh:
  5718. /* remove the associated SCSI host */
  5719. scsi_remove_host(ap->scsi_host);
  5720. }
  5721. /**
  5722. * ata_host_detach - Detach all ports of an ATA host
  5723. * @host: Host to detach
  5724. *
  5725. * Detach all ports of @host.
  5726. *
  5727. * LOCKING:
  5728. * Kernel thread context (may sleep).
  5729. */
  5730. void ata_host_detach(struct ata_host *host)
  5731. {
  5732. int i;
  5733. for (i = 0; i < host->n_ports; i++)
  5734. ata_port_detach(host->ports[i]);
  5735. }
  5736. /**
  5737. * ata_std_ports - initialize ioaddr with standard port offsets.
  5738. * @ioaddr: IO address structure to be initialized
  5739. *
  5740. * Utility function which initializes data_addr, error_addr,
  5741. * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
  5742. * device_addr, status_addr, and command_addr to standard offsets
  5743. * relative to cmd_addr.
  5744. *
  5745. * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
  5746. */
  5747. void ata_std_ports(struct ata_ioports *ioaddr)
  5748. {
  5749. ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
  5750. ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
  5751. ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
  5752. ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
  5753. ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
  5754. ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
  5755. ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
  5756. ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
  5757. ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
  5758. ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
  5759. }
  5760. #ifdef CONFIG_PCI
  5761. /**
  5762. * ata_pci_remove_one - PCI layer callback for device removal
  5763. * @pdev: PCI device that was removed
  5764. *
  5765. * PCI layer indicates to libata via this hook that hot-unplug or
  5766. * module unload event has occurred. Detach all ports. Resource
  5767. * release is handled via devres.
  5768. *
  5769. * LOCKING:
  5770. * Inherited from PCI layer (may sleep).
  5771. */
  5772. void ata_pci_remove_one(struct pci_dev *pdev)
  5773. {
  5774. struct device *dev = pci_dev_to_dev(pdev);
  5775. struct ata_host *host = dev_get_drvdata(dev);
  5776. ata_host_detach(host);
  5777. }
  5778. /* move to PCI subsystem */
  5779. int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
  5780. {
  5781. unsigned long tmp = 0;
  5782. switch (bits->width) {
  5783. case 1: {
  5784. u8 tmp8 = 0;
  5785. pci_read_config_byte(pdev, bits->reg, &tmp8);
  5786. tmp = tmp8;
  5787. break;
  5788. }
  5789. case 2: {
  5790. u16 tmp16 = 0;
  5791. pci_read_config_word(pdev, bits->reg, &tmp16);
  5792. tmp = tmp16;
  5793. break;
  5794. }
  5795. case 4: {
  5796. u32 tmp32 = 0;
  5797. pci_read_config_dword(pdev, bits->reg, &tmp32);
  5798. tmp = tmp32;
  5799. break;
  5800. }
  5801. default:
  5802. return -EINVAL;
  5803. }
  5804. tmp &= bits->mask;
  5805. return (tmp == bits->val) ? 1 : 0;
  5806. }
  5807. #ifdef CONFIG_PM
  5808. void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
  5809. {
  5810. pci_save_state(pdev);
  5811. pci_disable_device(pdev);
  5812. if (mesg.event == PM_EVENT_SUSPEND)
  5813. pci_set_power_state(pdev, PCI_D3hot);
  5814. }
  5815. int ata_pci_device_do_resume(struct pci_dev *pdev)
  5816. {
  5817. int rc;
  5818. pci_set_power_state(pdev, PCI_D0);
  5819. pci_restore_state(pdev);
  5820. rc = pcim_enable_device(pdev);
  5821. if (rc) {
  5822. dev_printk(KERN_ERR, &pdev->dev,
  5823. "failed to enable device after resume (%d)\n", rc);
  5824. return rc;
  5825. }
  5826. pci_set_master(pdev);
  5827. return 0;
  5828. }
  5829. int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
  5830. {
  5831. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  5832. int rc = 0;
  5833. rc = ata_host_suspend(host, mesg);
  5834. if (rc)
  5835. return rc;
  5836. ata_pci_device_do_suspend(pdev, mesg);
  5837. return 0;
  5838. }
  5839. int ata_pci_device_resume(struct pci_dev *pdev)
  5840. {
  5841. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  5842. int rc;
  5843. rc = ata_pci_device_do_resume(pdev);
  5844. if (rc == 0)
  5845. ata_host_resume(host);
  5846. return rc;
  5847. }
  5848. #endif /* CONFIG_PM */
  5849. #endif /* CONFIG_PCI */
  5850. static int __init ata_init(void)
  5851. {
  5852. ata_probe_timeout *= HZ;
  5853. ata_wq = create_workqueue("ata");
  5854. if (!ata_wq)
  5855. return -ENOMEM;
  5856. ata_aux_wq = create_singlethread_workqueue("ata_aux");
  5857. if (!ata_aux_wq) {
  5858. destroy_workqueue(ata_wq);
  5859. return -ENOMEM;
  5860. }
  5861. printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
  5862. return 0;
  5863. }
  5864. static void __exit ata_exit(void)
  5865. {
  5866. destroy_workqueue(ata_wq);
  5867. destroy_workqueue(ata_aux_wq);
  5868. }
  5869. subsys_initcall(ata_init);
  5870. module_exit(ata_exit);
  5871. static unsigned long ratelimit_time;
  5872. static DEFINE_SPINLOCK(ata_ratelimit_lock);
  5873. int ata_ratelimit(void)
  5874. {
  5875. int rc;
  5876. unsigned long flags;
  5877. spin_lock_irqsave(&ata_ratelimit_lock, flags);
  5878. if (time_after(jiffies, ratelimit_time)) {
  5879. rc = 1;
  5880. ratelimit_time = jiffies + (HZ/5);
  5881. } else
  5882. rc = 0;
  5883. spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
  5884. return rc;
  5885. }
  5886. /**
  5887. * ata_wait_register - wait until register value changes
  5888. * @reg: IO-mapped register
  5889. * @mask: Mask to apply to read register value
  5890. * @val: Wait condition
  5891. * @interval_msec: polling interval in milliseconds
  5892. * @timeout_msec: timeout in milliseconds
  5893. *
  5894. * Waiting for some bits of register to change is a common
  5895. * operation for ATA controllers. This function reads 32bit LE
  5896. * IO-mapped register @reg and tests for the following condition.
  5897. *
  5898. * (*@reg & mask) != val
  5899. *
  5900. * If the condition is met, it returns; otherwise, the process is
  5901. * repeated after @interval_msec until timeout.
  5902. *
  5903. * LOCKING:
  5904. * Kernel thread context (may sleep)
  5905. *
  5906. * RETURNS:
  5907. * The final register value.
  5908. */
  5909. u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
  5910. unsigned long interval_msec,
  5911. unsigned long timeout_msec)
  5912. {
  5913. unsigned long timeout;
  5914. u32 tmp;
  5915. tmp = ioread32(reg);
  5916. /* Calculate timeout _after_ the first read to make sure
  5917. * preceding writes reach the controller before starting to
  5918. * eat away the timeout.
  5919. */
  5920. timeout = jiffies + (timeout_msec * HZ) / 1000;
  5921. while ((tmp & mask) == val && time_before(jiffies, timeout)) {
  5922. msleep(interval_msec);
  5923. tmp = ioread32(reg);
  5924. }
  5925. return tmp;
  5926. }
  5927. /*
  5928. * Dummy port_ops
  5929. */
  5930. static void ata_dummy_noret(struct ata_port *ap) { }
  5931. static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
  5932. static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
  5933. static u8 ata_dummy_check_status(struct ata_port *ap)
  5934. {
  5935. return ATA_DRDY;
  5936. }
  5937. static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
  5938. {
  5939. return AC_ERR_SYSTEM;
  5940. }
  5941. const struct ata_port_operations ata_dummy_port_ops = {
  5942. .port_disable = ata_port_disable,
  5943. .check_status = ata_dummy_check_status,
  5944. .check_altstatus = ata_dummy_check_status,
  5945. .dev_select = ata_noop_dev_select,
  5946. .qc_prep = ata_noop_qc_prep,
  5947. .qc_issue = ata_dummy_qc_issue,
  5948. .freeze = ata_dummy_noret,
  5949. .thaw = ata_dummy_noret,
  5950. .error_handler = ata_dummy_noret,
  5951. .post_internal_cmd = ata_dummy_qc_noret,
  5952. .irq_clear = ata_dummy_noret,
  5953. .port_start = ata_dummy_ret0,
  5954. .port_stop = ata_dummy_noret,
  5955. };
  5956. const struct ata_port_info ata_dummy_port_info = {
  5957. .port_ops = &ata_dummy_port_ops,
  5958. };
  5959. /*
  5960. * libata is essentially a library of internal helper functions for
  5961. * low-level ATA host controller drivers. As such, the API/ABI is
  5962. * likely to change as new drivers are added and updated.
  5963. * Do not depend on ABI/API stability.
  5964. */
  5965. EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
  5966. EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
  5967. EXPORT_SYMBOL_GPL(sata_deb_timing_long);
  5968. EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
  5969. EXPORT_SYMBOL_GPL(ata_dummy_port_info);
  5970. EXPORT_SYMBOL_GPL(ata_std_bios_param);
  5971. EXPORT_SYMBOL_GPL(ata_std_ports);
  5972. EXPORT_SYMBOL_GPL(ata_host_init);
  5973. EXPORT_SYMBOL_GPL(ata_host_alloc);
  5974. EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
  5975. EXPORT_SYMBOL_GPL(ata_host_start);
  5976. EXPORT_SYMBOL_GPL(ata_host_register);
  5977. EXPORT_SYMBOL_GPL(ata_host_activate);
  5978. EXPORT_SYMBOL_GPL(ata_host_detach);
  5979. EXPORT_SYMBOL_GPL(ata_sg_init);
  5980. EXPORT_SYMBOL_GPL(ata_sg_init_one);
  5981. EXPORT_SYMBOL_GPL(ata_hsm_move);
  5982. EXPORT_SYMBOL_GPL(ata_qc_complete);
  5983. EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
  5984. EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
  5985. EXPORT_SYMBOL_GPL(ata_tf_load);
  5986. EXPORT_SYMBOL_GPL(ata_tf_read);
  5987. EXPORT_SYMBOL_GPL(ata_noop_dev_select);
  5988. EXPORT_SYMBOL_GPL(ata_std_dev_select);
  5989. EXPORT_SYMBOL_GPL(sata_print_link_status);
  5990. EXPORT_SYMBOL_GPL(ata_tf_to_fis);
  5991. EXPORT_SYMBOL_GPL(ata_tf_from_fis);
  5992. EXPORT_SYMBOL_GPL(ata_check_status);
  5993. EXPORT_SYMBOL_GPL(ata_altstatus);
  5994. EXPORT_SYMBOL_GPL(ata_exec_command);
  5995. EXPORT_SYMBOL_GPL(ata_port_start);
  5996. EXPORT_SYMBOL_GPL(ata_sff_port_start);
  5997. EXPORT_SYMBOL_GPL(ata_interrupt);
  5998. EXPORT_SYMBOL_GPL(ata_do_set_mode);
  5999. EXPORT_SYMBOL_GPL(ata_data_xfer);
  6000. EXPORT_SYMBOL_GPL(ata_data_xfer_noirq);
  6001. EXPORT_SYMBOL_GPL(ata_qc_prep);
  6002. EXPORT_SYMBOL_GPL(ata_dumb_qc_prep);
  6003. EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
  6004. EXPORT_SYMBOL_GPL(ata_bmdma_setup);
  6005. EXPORT_SYMBOL_GPL(ata_bmdma_start);
  6006. EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
  6007. EXPORT_SYMBOL_GPL(ata_bmdma_status);
  6008. EXPORT_SYMBOL_GPL(ata_bmdma_stop);
  6009. EXPORT_SYMBOL_GPL(ata_bmdma_freeze);
  6010. EXPORT_SYMBOL_GPL(ata_bmdma_thaw);
  6011. EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh);
  6012. EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
  6013. EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
  6014. EXPORT_SYMBOL_GPL(ata_port_probe);
  6015. EXPORT_SYMBOL_GPL(ata_dev_disable);
  6016. EXPORT_SYMBOL_GPL(sata_set_spd);
  6017. EXPORT_SYMBOL_GPL(sata_link_debounce);
  6018. EXPORT_SYMBOL_GPL(sata_link_resume);
  6019. EXPORT_SYMBOL_GPL(sata_phy_reset);
  6020. EXPORT_SYMBOL_GPL(__sata_phy_reset);
  6021. EXPORT_SYMBOL_GPL(ata_bus_reset);
  6022. EXPORT_SYMBOL_GPL(ata_std_prereset);
  6023. EXPORT_SYMBOL_GPL(ata_std_softreset);
  6024. EXPORT_SYMBOL_GPL(sata_link_hardreset);
  6025. EXPORT_SYMBOL_GPL(sata_std_hardreset);
  6026. EXPORT_SYMBOL_GPL(ata_std_postreset);
  6027. EXPORT_SYMBOL_GPL(ata_dev_classify);
  6028. EXPORT_SYMBOL_GPL(ata_dev_pair);
  6029. EXPORT_SYMBOL_GPL(ata_port_disable);
  6030. EXPORT_SYMBOL_GPL(ata_ratelimit);
  6031. EXPORT_SYMBOL_GPL(ata_wait_register);
  6032. EXPORT_SYMBOL_GPL(ata_busy_sleep);
  6033. EXPORT_SYMBOL_GPL(ata_wait_ready);
  6034. EXPORT_SYMBOL_GPL(ata_port_queue_task);
  6035. EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
  6036. EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
  6037. EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
  6038. EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
  6039. EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
  6040. EXPORT_SYMBOL_GPL(ata_host_intr);
  6041. EXPORT_SYMBOL_GPL(sata_scr_valid);
  6042. EXPORT_SYMBOL_GPL(sata_scr_read);
  6043. EXPORT_SYMBOL_GPL(sata_scr_write);
  6044. EXPORT_SYMBOL_GPL(sata_scr_write_flush);
  6045. EXPORT_SYMBOL_GPL(ata_link_online);
  6046. EXPORT_SYMBOL_GPL(ata_link_offline);
  6047. #ifdef CONFIG_PM
  6048. EXPORT_SYMBOL_GPL(ata_host_suspend);
  6049. EXPORT_SYMBOL_GPL(ata_host_resume);
  6050. #endif /* CONFIG_PM */
  6051. EXPORT_SYMBOL_GPL(ata_id_string);
  6052. EXPORT_SYMBOL_GPL(ata_id_c_string);
  6053. EXPORT_SYMBOL_GPL(ata_id_to_dma_mode);
  6054. EXPORT_SYMBOL_GPL(ata_scsi_simulate);
  6055. EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
  6056. EXPORT_SYMBOL_GPL(ata_timing_compute);
  6057. EXPORT_SYMBOL_GPL(ata_timing_merge);
  6058. #ifdef CONFIG_PCI
  6059. EXPORT_SYMBOL_GPL(pci_test_config_bits);
  6060. EXPORT_SYMBOL_GPL(ata_pci_init_sff_host);
  6061. EXPORT_SYMBOL_GPL(ata_pci_init_bmdma);
  6062. EXPORT_SYMBOL_GPL(ata_pci_prepare_sff_host);
  6063. EXPORT_SYMBOL_GPL(ata_pci_init_one);
  6064. EXPORT_SYMBOL_GPL(ata_pci_remove_one);
  6065. #ifdef CONFIG_PM
  6066. EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
  6067. EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
  6068. EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
  6069. EXPORT_SYMBOL_GPL(ata_pci_device_resume);
  6070. #endif /* CONFIG_PM */
  6071. EXPORT_SYMBOL_GPL(ata_pci_default_filter);
  6072. EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
  6073. #endif /* CONFIG_PCI */
  6074. EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
  6075. EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
  6076. EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
  6077. EXPORT_SYMBOL_GPL(ata_eng_timeout);
  6078. EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
  6079. EXPORT_SYMBOL_GPL(ata_link_abort);
  6080. EXPORT_SYMBOL_GPL(ata_port_abort);
  6081. EXPORT_SYMBOL_GPL(ata_port_freeze);
  6082. EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
  6083. EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
  6084. EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
  6085. EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
  6086. EXPORT_SYMBOL_GPL(ata_do_eh);
  6087. EXPORT_SYMBOL_GPL(ata_irq_on);
  6088. EXPORT_SYMBOL_GPL(ata_dev_try_classify);
  6089. EXPORT_SYMBOL_GPL(ata_cable_40wire);
  6090. EXPORT_SYMBOL_GPL(ata_cable_80wire);
  6091. EXPORT_SYMBOL_GPL(ata_cable_unknown);
  6092. EXPORT_SYMBOL_GPL(ata_cable_sata);