libata-core.c 164 KB

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