libata-core.c 171 KB

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