libata-core.c 173 KB

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