libata-core.c 177 KB

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