libata-core.c 200 KB

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