libata-core.c 171 KB

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