libata-core.c 176 KB

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