libata-core.c 186 KB

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