libata-core.c 169 KB

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