libata-core.c 200 KB

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