libata-core.c 200 KB

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