libata-core.c 175 KB

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