libata-core.c 170 KB

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