libata-core.c 181 KB

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