libata-core.c 189 KB

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