libata-core.c 198 KB

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