libata-core.c 191 KB

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