libata-core.c 185 KB

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