libata-core.c 172 KB

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