libata-core.c 197 KB

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