libata-core.c 183 KB

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