libata-core.c 174 KB

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