libata-core.c 158 KB

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