libata-core.c 184 KB

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