libata-core.c 157 KB

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