libata-core.c 152 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267
  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. */
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/pci.h>
  37. #include <linux/init.h>
  38. #include <linux/list.h>
  39. #include <linux/mm.h>
  40. #include <linux/highmem.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/blkdev.h>
  43. #include <linux/delay.h>
  44. #include <linux/timer.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/completion.h>
  47. #include <linux/suspend.h>
  48. #include <linux/workqueue.h>
  49. #include <linux/jiffies.h>
  50. #include <linux/scatterlist.h>
  51. #include <scsi/scsi.h>
  52. #include <scsi/scsi_cmnd.h>
  53. #include <scsi/scsi_host.h>
  54. #include <linux/libata.h>
  55. #include <asm/io.h>
  56. #include <asm/semaphore.h>
  57. #include <asm/byteorder.h>
  58. #include "libata.h"
  59. /* debounce timing parameters in msecs { interval, duration, timeout } */
  60. const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
  61. const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
  62. const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
  63. static unsigned int ata_dev_init_params(struct ata_device *dev,
  64. u16 heads, u16 sectors);
  65. static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
  66. static void ata_dev_xfermask(struct ata_device *dev);
  67. static unsigned int ata_unique_id = 1;
  68. static struct workqueue_struct *ata_wq;
  69. struct workqueue_struct *ata_aux_wq;
  70. int atapi_enabled = 1;
  71. module_param(atapi_enabled, int, 0444);
  72. MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
  73. int atapi_dmadir = 0;
  74. module_param(atapi_dmadir, int, 0444);
  75. MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
  76. int libata_fua = 0;
  77. module_param_named(fua, libata_fua, int, 0444);
  78. MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
  79. static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
  80. module_param(ata_probe_timeout, int, 0444);
  81. MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
  82. MODULE_AUTHOR("Jeff Garzik");
  83. MODULE_DESCRIPTION("Library module for ATA devices");
  84. MODULE_LICENSE("GPL");
  85. MODULE_VERSION(DRV_VERSION);
  86. /**
  87. * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
  88. * @tf: Taskfile to convert
  89. * @fis: Buffer into which data will output
  90. * @pmp: Port multiplier port
  91. *
  92. * Converts a standard ATA taskfile to a Serial ATA
  93. * FIS structure (Register - Host to Device).
  94. *
  95. * LOCKING:
  96. * Inherited from caller.
  97. */
  98. void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
  99. {
  100. fis[0] = 0x27; /* Register - Host to Device FIS */
  101. fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
  102. bit 7 indicates Command FIS */
  103. fis[2] = tf->command;
  104. fis[3] = tf->feature;
  105. fis[4] = tf->lbal;
  106. fis[5] = tf->lbam;
  107. fis[6] = tf->lbah;
  108. fis[7] = tf->device;
  109. fis[8] = tf->hob_lbal;
  110. fis[9] = tf->hob_lbam;
  111. fis[10] = tf->hob_lbah;
  112. fis[11] = tf->hob_feature;
  113. fis[12] = tf->nsect;
  114. fis[13] = tf->hob_nsect;
  115. fis[14] = 0;
  116. fis[15] = tf->ctl;
  117. fis[16] = 0;
  118. fis[17] = 0;
  119. fis[18] = 0;
  120. fis[19] = 0;
  121. }
  122. /**
  123. * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
  124. * @fis: Buffer from which data will be input
  125. * @tf: Taskfile to output
  126. *
  127. * Converts a serial ATA FIS structure to a standard ATA taskfile.
  128. *
  129. * LOCKING:
  130. * Inherited from caller.
  131. */
  132. void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
  133. {
  134. tf->command = fis[2]; /* status */
  135. tf->feature = fis[3]; /* error */
  136. tf->lbal = fis[4];
  137. tf->lbam = fis[5];
  138. tf->lbah = fis[6];
  139. tf->device = fis[7];
  140. tf->hob_lbal = fis[8];
  141. tf->hob_lbam = fis[9];
  142. tf->hob_lbah = fis[10];
  143. tf->nsect = fis[12];
  144. tf->hob_nsect = fis[13];
  145. }
  146. static const u8 ata_rw_cmds[] = {
  147. /* pio multi */
  148. ATA_CMD_READ_MULTI,
  149. ATA_CMD_WRITE_MULTI,
  150. ATA_CMD_READ_MULTI_EXT,
  151. ATA_CMD_WRITE_MULTI_EXT,
  152. 0,
  153. 0,
  154. 0,
  155. ATA_CMD_WRITE_MULTI_FUA_EXT,
  156. /* pio */
  157. ATA_CMD_PIO_READ,
  158. ATA_CMD_PIO_WRITE,
  159. ATA_CMD_PIO_READ_EXT,
  160. ATA_CMD_PIO_WRITE_EXT,
  161. 0,
  162. 0,
  163. 0,
  164. 0,
  165. /* dma */
  166. ATA_CMD_READ,
  167. ATA_CMD_WRITE,
  168. ATA_CMD_READ_EXT,
  169. ATA_CMD_WRITE_EXT,
  170. 0,
  171. 0,
  172. 0,
  173. ATA_CMD_WRITE_FUA_EXT
  174. };
  175. /**
  176. * ata_rwcmd_protocol - set taskfile r/w commands and protocol
  177. * @qc: command to examine and configure
  178. *
  179. * Examine the device configuration and tf->flags to calculate
  180. * the proper read/write commands and protocol to use.
  181. *
  182. * LOCKING:
  183. * caller.
  184. */
  185. int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
  186. {
  187. struct ata_taskfile *tf = &qc->tf;
  188. struct ata_device *dev = qc->dev;
  189. u8 cmd;
  190. int index, fua, lba48, write;
  191. fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
  192. lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
  193. write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
  194. if (dev->flags & ATA_DFLAG_PIO) {
  195. tf->protocol = ATA_PROT_PIO;
  196. index = dev->multi_count ? 0 : 8;
  197. } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
  198. /* Unable to use DMA due to host limitation */
  199. tf->protocol = ATA_PROT_PIO;
  200. index = dev->multi_count ? 0 : 8;
  201. } else {
  202. tf->protocol = ATA_PROT_DMA;
  203. index = 16;
  204. }
  205. cmd = ata_rw_cmds[index + fua + lba48 + write];
  206. if (cmd) {
  207. tf->command = cmd;
  208. return 0;
  209. }
  210. return -1;
  211. }
  212. /**
  213. * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
  214. * @pio_mask: pio_mask
  215. * @mwdma_mask: mwdma_mask
  216. * @udma_mask: udma_mask
  217. *
  218. * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
  219. * unsigned int xfer_mask.
  220. *
  221. * LOCKING:
  222. * None.
  223. *
  224. * RETURNS:
  225. * Packed xfer_mask.
  226. */
  227. static unsigned int ata_pack_xfermask(unsigned int pio_mask,
  228. unsigned int mwdma_mask,
  229. unsigned int udma_mask)
  230. {
  231. return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
  232. ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
  233. ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
  234. }
  235. /**
  236. * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
  237. * @xfer_mask: xfer_mask to unpack
  238. * @pio_mask: resulting pio_mask
  239. * @mwdma_mask: resulting mwdma_mask
  240. * @udma_mask: resulting udma_mask
  241. *
  242. * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
  243. * Any NULL distination masks will be ignored.
  244. */
  245. static void ata_unpack_xfermask(unsigned int xfer_mask,
  246. unsigned int *pio_mask,
  247. unsigned int *mwdma_mask,
  248. unsigned int *udma_mask)
  249. {
  250. if (pio_mask)
  251. *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
  252. if (mwdma_mask)
  253. *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
  254. if (udma_mask)
  255. *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
  256. }
  257. static const struct ata_xfer_ent {
  258. int shift, bits;
  259. u8 base;
  260. } ata_xfer_tbl[] = {
  261. { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
  262. { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
  263. { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
  264. { -1, },
  265. };
  266. /**
  267. * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
  268. * @xfer_mask: xfer_mask of interest
  269. *
  270. * Return matching XFER_* value for @xfer_mask. Only the highest
  271. * bit of @xfer_mask is considered.
  272. *
  273. * LOCKING:
  274. * None.
  275. *
  276. * RETURNS:
  277. * Matching XFER_* value, 0 if no match found.
  278. */
  279. static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
  280. {
  281. int highbit = fls(xfer_mask) - 1;
  282. const struct ata_xfer_ent *ent;
  283. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  284. if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
  285. return ent->base + highbit - ent->shift;
  286. return 0;
  287. }
  288. /**
  289. * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
  290. * @xfer_mode: XFER_* of interest
  291. *
  292. * Return matching xfer_mask for @xfer_mode.
  293. *
  294. * LOCKING:
  295. * None.
  296. *
  297. * RETURNS:
  298. * Matching xfer_mask, 0 if no match found.
  299. */
  300. static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
  301. {
  302. const struct ata_xfer_ent *ent;
  303. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  304. if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
  305. return 1 << (ent->shift + xfer_mode - ent->base);
  306. return 0;
  307. }
  308. /**
  309. * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
  310. * @xfer_mode: XFER_* of interest
  311. *
  312. * Return matching xfer_shift for @xfer_mode.
  313. *
  314. * LOCKING:
  315. * None.
  316. *
  317. * RETURNS:
  318. * Matching xfer_shift, -1 if no match found.
  319. */
  320. static int ata_xfer_mode2shift(unsigned int xfer_mode)
  321. {
  322. const struct ata_xfer_ent *ent;
  323. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  324. if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
  325. return ent->shift;
  326. return -1;
  327. }
  328. /**
  329. * ata_mode_string - convert xfer_mask to string
  330. * @xfer_mask: mask of bits supported; only highest bit counts.
  331. *
  332. * Determine string which represents the highest speed
  333. * (highest bit in @modemask).
  334. *
  335. * LOCKING:
  336. * None.
  337. *
  338. * RETURNS:
  339. * Constant C string representing highest speed listed in
  340. * @mode_mask, or the constant C string "<n/a>".
  341. */
  342. static const char *ata_mode_string(unsigned int xfer_mask)
  343. {
  344. static const char * const xfer_mode_str[] = {
  345. "PIO0",
  346. "PIO1",
  347. "PIO2",
  348. "PIO3",
  349. "PIO4",
  350. "PIO5",
  351. "PIO6",
  352. "MWDMA0",
  353. "MWDMA1",
  354. "MWDMA2",
  355. "MWDMA3",
  356. "MWDMA4",
  357. "UDMA/16",
  358. "UDMA/25",
  359. "UDMA/33",
  360. "UDMA/44",
  361. "UDMA/66",
  362. "UDMA/100",
  363. "UDMA/133",
  364. "UDMA7",
  365. };
  366. int highbit;
  367. highbit = fls(xfer_mask) - 1;
  368. if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
  369. return xfer_mode_str[highbit];
  370. return "<n/a>";
  371. }
  372. static const char *sata_spd_string(unsigned int spd)
  373. {
  374. static const char * const spd_str[] = {
  375. "1.5 Gbps",
  376. "3.0 Gbps",
  377. };
  378. if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
  379. return "<unknown>";
  380. return spd_str[spd - 1];
  381. }
  382. void ata_dev_disable(struct ata_device *dev)
  383. {
  384. if (ata_dev_enabled(dev) && ata_msg_drv(dev->ap)) {
  385. ata_dev_printk(dev, KERN_WARNING, "disabled\n");
  386. dev->class++;
  387. }
  388. }
  389. /**
  390. * ata_pio_devchk - PATA device presence detection
  391. * @ap: ATA channel to examine
  392. * @device: Device to examine (starting at zero)
  393. *
  394. * This technique was originally described in
  395. * Hale Landis's ATADRVR (www.ata-atapi.com), and
  396. * later found its way into the ATA/ATAPI spec.
  397. *
  398. * Write a pattern to the ATA shadow registers,
  399. * and if a device is present, it will respond by
  400. * correctly storing and echoing back the
  401. * ATA shadow register contents.
  402. *
  403. * LOCKING:
  404. * caller.
  405. */
  406. static unsigned int ata_pio_devchk(struct ata_port *ap,
  407. unsigned int device)
  408. {
  409. struct ata_ioports *ioaddr = &ap->ioaddr;
  410. u8 nsect, lbal;
  411. ap->ops->dev_select(ap, device);
  412. outb(0x55, ioaddr->nsect_addr);
  413. outb(0xaa, ioaddr->lbal_addr);
  414. outb(0xaa, ioaddr->nsect_addr);
  415. outb(0x55, ioaddr->lbal_addr);
  416. outb(0x55, ioaddr->nsect_addr);
  417. outb(0xaa, ioaddr->lbal_addr);
  418. nsect = inb(ioaddr->nsect_addr);
  419. lbal = inb(ioaddr->lbal_addr);
  420. if ((nsect == 0x55) && (lbal == 0xaa))
  421. return 1; /* we found a device */
  422. return 0; /* nothing found */
  423. }
  424. /**
  425. * ata_mmio_devchk - PATA device presence detection
  426. * @ap: ATA channel to examine
  427. * @device: Device to examine (starting at zero)
  428. *
  429. * This technique was originally described in
  430. * Hale Landis's ATADRVR (www.ata-atapi.com), and
  431. * later found its way into the ATA/ATAPI spec.
  432. *
  433. * Write a pattern to the ATA shadow registers,
  434. * and if a device is present, it will respond by
  435. * correctly storing and echoing back the
  436. * ATA shadow register contents.
  437. *
  438. * LOCKING:
  439. * caller.
  440. */
  441. static unsigned int ata_mmio_devchk(struct ata_port *ap,
  442. unsigned int device)
  443. {
  444. struct ata_ioports *ioaddr = &ap->ioaddr;
  445. u8 nsect, lbal;
  446. ap->ops->dev_select(ap, device);
  447. writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
  448. writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
  449. writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
  450. writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
  451. writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
  452. writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
  453. nsect = readb((void __iomem *) ioaddr->nsect_addr);
  454. lbal = readb((void __iomem *) ioaddr->lbal_addr);
  455. if ((nsect == 0x55) && (lbal == 0xaa))
  456. return 1; /* we found a device */
  457. return 0; /* nothing found */
  458. }
  459. /**
  460. * ata_devchk - PATA device presence detection
  461. * @ap: ATA channel to examine
  462. * @device: Device to examine (starting at zero)
  463. *
  464. * Dispatch ATA device presence detection, depending
  465. * on whether we are using PIO or MMIO to talk to the
  466. * ATA shadow registers.
  467. *
  468. * LOCKING:
  469. * caller.
  470. */
  471. static unsigned int ata_devchk(struct ata_port *ap,
  472. unsigned int device)
  473. {
  474. if (ap->flags & ATA_FLAG_MMIO)
  475. return ata_mmio_devchk(ap, device);
  476. return ata_pio_devchk(ap, device);
  477. }
  478. /**
  479. * ata_dev_classify - determine device type based on ATA-spec signature
  480. * @tf: ATA taskfile register set for device to be identified
  481. *
  482. * Determine from taskfile register contents whether a device is
  483. * ATA or ATAPI, as per "Signature and persistence" section
  484. * of ATA/PI spec (volume 1, sect 5.14).
  485. *
  486. * LOCKING:
  487. * None.
  488. *
  489. * RETURNS:
  490. * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
  491. * the event of failure.
  492. */
  493. unsigned int ata_dev_classify(const struct ata_taskfile *tf)
  494. {
  495. /* Apple's open source Darwin code hints that some devices only
  496. * put a proper signature into the LBA mid/high registers,
  497. * So, we only check those. It's sufficient for uniqueness.
  498. */
  499. if (((tf->lbam == 0) && (tf->lbah == 0)) ||
  500. ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
  501. DPRINTK("found ATA device by sig\n");
  502. return ATA_DEV_ATA;
  503. }
  504. if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
  505. ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
  506. DPRINTK("found ATAPI device by sig\n");
  507. return ATA_DEV_ATAPI;
  508. }
  509. DPRINTK("unknown device\n");
  510. return ATA_DEV_UNKNOWN;
  511. }
  512. /**
  513. * ata_dev_try_classify - Parse returned ATA device signature
  514. * @ap: ATA channel to examine
  515. * @device: Device to examine (starting at zero)
  516. * @r_err: Value of error register on completion
  517. *
  518. * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
  519. * an ATA/ATAPI-defined set of values is placed in the ATA
  520. * shadow registers, indicating the results of device detection
  521. * and diagnostics.
  522. *
  523. * Select the ATA device, and read the values from the ATA shadow
  524. * registers. Then parse according to the Error register value,
  525. * and the spec-defined values examined by ata_dev_classify().
  526. *
  527. * LOCKING:
  528. * caller.
  529. *
  530. * RETURNS:
  531. * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
  532. */
  533. static unsigned int
  534. ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
  535. {
  536. struct ata_taskfile tf;
  537. unsigned int class;
  538. u8 err;
  539. ap->ops->dev_select(ap, device);
  540. memset(&tf, 0, sizeof(tf));
  541. ap->ops->tf_read(ap, &tf);
  542. err = tf.feature;
  543. if (r_err)
  544. *r_err = err;
  545. /* see if device passed diags: if master then continue and warn later */
  546. if (err == 0 && device == 0)
  547. /* diagnostic fail : do nothing _YET_ */
  548. ap->device[device].horkage |= ATA_HORKAGE_DIAGNOSTIC;
  549. else if (err == 1)
  550. /* do nothing */ ;
  551. else if ((device == 0) && (err == 0x81))
  552. /* do nothing */ ;
  553. else
  554. return ATA_DEV_NONE;
  555. /* determine if device is ATA or ATAPI */
  556. class = ata_dev_classify(&tf);
  557. if (class == ATA_DEV_UNKNOWN)
  558. return ATA_DEV_NONE;
  559. if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
  560. return ATA_DEV_NONE;
  561. return class;
  562. }
  563. /**
  564. * ata_id_string - Convert IDENTIFY DEVICE page into string
  565. * @id: IDENTIFY DEVICE results we will examine
  566. * @s: string into which data is output
  567. * @ofs: offset into identify device page
  568. * @len: length of string to return. must be an even number.
  569. *
  570. * The strings in the IDENTIFY DEVICE page are broken up into
  571. * 16-bit chunks. Run through the string, and output each
  572. * 8-bit chunk linearly, regardless of platform.
  573. *
  574. * LOCKING:
  575. * caller.
  576. */
  577. void ata_id_string(const u16 *id, unsigned char *s,
  578. unsigned int ofs, unsigned int len)
  579. {
  580. unsigned int c;
  581. while (len > 0) {
  582. c = id[ofs] >> 8;
  583. *s = c;
  584. s++;
  585. c = id[ofs] & 0xff;
  586. *s = c;
  587. s++;
  588. ofs++;
  589. len -= 2;
  590. }
  591. }
  592. /**
  593. * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
  594. * @id: IDENTIFY DEVICE results we will examine
  595. * @s: string into which data is output
  596. * @ofs: offset into identify device page
  597. * @len: length of string to return. must be an odd number.
  598. *
  599. * This function is identical to ata_id_string except that it
  600. * trims trailing spaces and terminates the resulting string with
  601. * null. @len must be actual maximum length (even number) + 1.
  602. *
  603. * LOCKING:
  604. * caller.
  605. */
  606. void ata_id_c_string(const u16 *id, unsigned char *s,
  607. unsigned int ofs, unsigned int len)
  608. {
  609. unsigned char *p;
  610. WARN_ON(!(len & 1));
  611. ata_id_string(id, s, ofs, len - 1);
  612. p = s + strnlen(s, len - 1);
  613. while (p > s && p[-1] == ' ')
  614. p--;
  615. *p = '\0';
  616. }
  617. static u64 ata_id_n_sectors(const u16 *id)
  618. {
  619. if (ata_id_has_lba(id)) {
  620. if (ata_id_has_lba48(id))
  621. return ata_id_u64(id, 100);
  622. else
  623. return ata_id_u32(id, 60);
  624. } else {
  625. if (ata_id_current_chs_valid(id))
  626. return ata_id_u32(id, 57);
  627. else
  628. return id[1] * id[3] * id[6];
  629. }
  630. }
  631. /**
  632. * ata_noop_dev_select - Select device 0/1 on ATA bus
  633. * @ap: ATA channel to manipulate
  634. * @device: ATA device (numbered from zero) to select
  635. *
  636. * This function performs no actual function.
  637. *
  638. * May be used as the dev_select() entry in ata_port_operations.
  639. *
  640. * LOCKING:
  641. * caller.
  642. */
  643. void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
  644. {
  645. }
  646. /**
  647. * ata_std_dev_select - Select device 0/1 on ATA bus
  648. * @ap: ATA channel to manipulate
  649. * @device: ATA device (numbered from zero) to select
  650. *
  651. * Use the method defined in the ATA specification to
  652. * make either device 0, or device 1, active on the
  653. * ATA channel. Works with both PIO and MMIO.
  654. *
  655. * May be used as the dev_select() entry in ata_port_operations.
  656. *
  657. * LOCKING:
  658. * caller.
  659. */
  660. void ata_std_dev_select (struct ata_port *ap, unsigned int device)
  661. {
  662. u8 tmp;
  663. if (device == 0)
  664. tmp = ATA_DEVICE_OBS;
  665. else
  666. tmp = ATA_DEVICE_OBS | ATA_DEV1;
  667. if (ap->flags & ATA_FLAG_MMIO) {
  668. writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
  669. } else {
  670. outb(tmp, ap->ioaddr.device_addr);
  671. }
  672. ata_pause(ap); /* needed; also flushes, for mmio */
  673. }
  674. /**
  675. * ata_dev_select - Select device 0/1 on ATA bus
  676. * @ap: ATA channel to manipulate
  677. * @device: ATA device (numbered from zero) to select
  678. * @wait: non-zero to wait for Status register BSY bit to clear
  679. * @can_sleep: non-zero if context allows sleeping
  680. *
  681. * Use the method defined in the ATA specification to
  682. * make either device 0, or device 1, active on the
  683. * ATA channel.
  684. *
  685. * This is a high-level version of ata_std_dev_select(),
  686. * which additionally provides the services of inserting
  687. * the proper pauses and status polling, where needed.
  688. *
  689. * LOCKING:
  690. * caller.
  691. */
  692. void ata_dev_select(struct ata_port *ap, unsigned int device,
  693. unsigned int wait, unsigned int can_sleep)
  694. {
  695. if (ata_msg_probe(ap))
  696. ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, ata%u: "
  697. "device %u, wait %u\n", ap->id, device, wait);
  698. if (wait)
  699. ata_wait_idle(ap);
  700. ap->ops->dev_select(ap, device);
  701. if (wait) {
  702. if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
  703. msleep(150);
  704. ata_wait_idle(ap);
  705. }
  706. }
  707. /**
  708. * ata_dump_id - IDENTIFY DEVICE info debugging output
  709. * @id: IDENTIFY DEVICE page to dump
  710. *
  711. * Dump selected 16-bit words from the given IDENTIFY DEVICE
  712. * page.
  713. *
  714. * LOCKING:
  715. * caller.
  716. */
  717. static inline void ata_dump_id(const u16 *id)
  718. {
  719. DPRINTK("49==0x%04x "
  720. "53==0x%04x "
  721. "63==0x%04x "
  722. "64==0x%04x "
  723. "75==0x%04x \n",
  724. id[49],
  725. id[53],
  726. id[63],
  727. id[64],
  728. id[75]);
  729. DPRINTK("80==0x%04x "
  730. "81==0x%04x "
  731. "82==0x%04x "
  732. "83==0x%04x "
  733. "84==0x%04x \n",
  734. id[80],
  735. id[81],
  736. id[82],
  737. id[83],
  738. id[84]);
  739. DPRINTK("88==0x%04x "
  740. "93==0x%04x\n",
  741. id[88],
  742. id[93]);
  743. }
  744. /**
  745. * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
  746. * @id: IDENTIFY data to compute xfer mask from
  747. *
  748. * Compute the xfermask for this device. This is not as trivial
  749. * as it seems if we must consider early devices correctly.
  750. *
  751. * FIXME: pre IDE drive timing (do we care ?).
  752. *
  753. * LOCKING:
  754. * None.
  755. *
  756. * RETURNS:
  757. * Computed xfermask
  758. */
  759. static unsigned int ata_id_xfermask(const u16 *id)
  760. {
  761. unsigned int pio_mask, mwdma_mask, udma_mask;
  762. /* Usual case. Word 53 indicates word 64 is valid */
  763. if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
  764. pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
  765. pio_mask <<= 3;
  766. pio_mask |= 0x7;
  767. } else {
  768. /* If word 64 isn't valid then Word 51 high byte holds
  769. * the PIO timing number for the maximum. Turn it into
  770. * a mask.
  771. */
  772. u8 mode = id[ATA_ID_OLD_PIO_MODES] & 0xFF;
  773. if (mode < 5) /* Valid PIO range */
  774. pio_mask = (2 << mode) - 1;
  775. else
  776. pio_mask = 1;
  777. /* But wait.. there's more. Design your standards by
  778. * committee and you too can get a free iordy field to
  779. * process. However its the speeds not the modes that
  780. * are supported... Note drivers using the timing API
  781. * will get this right anyway
  782. */
  783. }
  784. mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
  785. if (ata_id_is_cfa(id)) {
  786. /*
  787. * Process compact flash extended modes
  788. */
  789. int pio = id[163] & 0x7;
  790. int dma = (id[163] >> 3) & 7;
  791. if (pio)
  792. pio_mask |= (1 << 5);
  793. if (pio > 1)
  794. pio_mask |= (1 << 6);
  795. if (dma)
  796. mwdma_mask |= (1 << 3);
  797. if (dma > 1)
  798. mwdma_mask |= (1 << 4);
  799. }
  800. udma_mask = 0;
  801. if (id[ATA_ID_FIELD_VALID] & (1 << 2))
  802. udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
  803. return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
  804. }
  805. /**
  806. * ata_port_queue_task - Queue port_task
  807. * @ap: The ata_port to queue port_task for
  808. * @fn: workqueue function to be scheduled
  809. * @data: data value to pass to workqueue function
  810. * @delay: delay time for workqueue function
  811. *
  812. * Schedule @fn(@data) for execution after @delay jiffies using
  813. * port_task. There is one port_task per port and it's the
  814. * user(low level driver)'s responsibility to make sure that only
  815. * one task is active at any given time.
  816. *
  817. * libata core layer takes care of synchronization between
  818. * port_task and EH. ata_port_queue_task() may be ignored for EH
  819. * synchronization.
  820. *
  821. * LOCKING:
  822. * Inherited from caller.
  823. */
  824. void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
  825. unsigned long delay)
  826. {
  827. int rc;
  828. if (ap->pflags & ATA_PFLAG_FLUSH_PORT_TASK)
  829. return;
  830. PREPARE_WORK(&ap->port_task, fn, data);
  831. if (!delay)
  832. rc = queue_work(ata_wq, &ap->port_task);
  833. else
  834. rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
  835. /* rc == 0 means that another user is using port task */
  836. WARN_ON(rc == 0);
  837. }
  838. /**
  839. * ata_port_flush_task - Flush port_task
  840. * @ap: The ata_port to flush port_task for
  841. *
  842. * After this function completes, port_task is guranteed not to
  843. * be running or scheduled.
  844. *
  845. * LOCKING:
  846. * Kernel thread context (may sleep)
  847. */
  848. void ata_port_flush_task(struct ata_port *ap)
  849. {
  850. unsigned long flags;
  851. DPRINTK("ENTER\n");
  852. spin_lock_irqsave(ap->lock, flags);
  853. ap->pflags |= ATA_PFLAG_FLUSH_PORT_TASK;
  854. spin_unlock_irqrestore(ap->lock, flags);
  855. DPRINTK("flush #1\n");
  856. flush_workqueue(ata_wq);
  857. /*
  858. * At this point, if a task is running, it's guaranteed to see
  859. * the FLUSH flag; thus, it will never queue pio tasks again.
  860. * Cancel and flush.
  861. */
  862. if (!cancel_delayed_work(&ap->port_task)) {
  863. if (ata_msg_ctl(ap))
  864. ata_port_printk(ap, KERN_DEBUG, "%s: flush #2\n",
  865. __FUNCTION__);
  866. flush_workqueue(ata_wq);
  867. }
  868. spin_lock_irqsave(ap->lock, flags);
  869. ap->pflags &= ~ATA_PFLAG_FLUSH_PORT_TASK;
  870. spin_unlock_irqrestore(ap->lock, flags);
  871. if (ata_msg_ctl(ap))
  872. ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __FUNCTION__);
  873. }
  874. void ata_qc_complete_internal(struct ata_queued_cmd *qc)
  875. {
  876. struct completion *waiting = qc->private_data;
  877. complete(waiting);
  878. }
  879. /**
  880. * ata_exec_internal - execute libata internal command
  881. * @dev: Device to which the command is sent
  882. * @tf: Taskfile registers for the command and the result
  883. * @cdb: CDB for packet command
  884. * @dma_dir: Data tranfer direction of the command
  885. * @buf: Data buffer of the command
  886. * @buflen: Length of data buffer
  887. *
  888. * Executes libata internal command with timeout. @tf contains
  889. * command on entry and result on return. Timeout and error
  890. * conditions are reported via return value. No recovery action
  891. * is taken after a command times out. It's caller's duty to
  892. * clean up after timeout.
  893. *
  894. * LOCKING:
  895. * None. Should be called with kernel context, might sleep.
  896. *
  897. * RETURNS:
  898. * Zero on success, AC_ERR_* mask on failure
  899. */
  900. unsigned ata_exec_internal(struct ata_device *dev,
  901. struct ata_taskfile *tf, const u8 *cdb,
  902. int dma_dir, void *buf, unsigned int buflen)
  903. {
  904. struct ata_port *ap = dev->ap;
  905. u8 command = tf->command;
  906. struct ata_queued_cmd *qc;
  907. unsigned int tag, preempted_tag;
  908. u32 preempted_sactive, preempted_qc_active;
  909. DECLARE_COMPLETION_ONSTACK(wait);
  910. unsigned long flags;
  911. unsigned int err_mask;
  912. int rc;
  913. spin_lock_irqsave(ap->lock, flags);
  914. /* no internal command while frozen */
  915. if (ap->pflags & ATA_PFLAG_FROZEN) {
  916. spin_unlock_irqrestore(ap->lock, flags);
  917. return AC_ERR_SYSTEM;
  918. }
  919. /* initialize internal qc */
  920. /* XXX: Tag 0 is used for drivers with legacy EH as some
  921. * drivers choke if any other tag is given. This breaks
  922. * ata_tag_internal() test for those drivers. Don't use new
  923. * EH stuff without converting to it.
  924. */
  925. if (ap->ops->error_handler)
  926. tag = ATA_TAG_INTERNAL;
  927. else
  928. tag = 0;
  929. if (test_and_set_bit(tag, &ap->qc_allocated))
  930. BUG();
  931. qc = __ata_qc_from_tag(ap, tag);
  932. qc->tag = tag;
  933. qc->scsicmd = NULL;
  934. qc->ap = ap;
  935. qc->dev = dev;
  936. ata_qc_reinit(qc);
  937. preempted_tag = ap->active_tag;
  938. preempted_sactive = ap->sactive;
  939. preempted_qc_active = ap->qc_active;
  940. ap->active_tag = ATA_TAG_POISON;
  941. ap->sactive = 0;
  942. ap->qc_active = 0;
  943. /* prepare & issue qc */
  944. qc->tf = *tf;
  945. if (cdb)
  946. memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
  947. qc->flags |= ATA_QCFLAG_RESULT_TF;
  948. qc->dma_dir = dma_dir;
  949. if (dma_dir != DMA_NONE) {
  950. ata_sg_init_one(qc, buf, buflen);
  951. qc->nsect = buflen / ATA_SECT_SIZE;
  952. }
  953. qc->private_data = &wait;
  954. qc->complete_fn = ata_qc_complete_internal;
  955. ata_qc_issue(qc);
  956. spin_unlock_irqrestore(ap->lock, flags);
  957. rc = wait_for_completion_timeout(&wait, ata_probe_timeout);
  958. ata_port_flush_task(ap);
  959. if (!rc) {
  960. spin_lock_irqsave(ap->lock, flags);
  961. /* We're racing with irq here. If we lose, the
  962. * following test prevents us from completing the qc
  963. * twice. If we win, the port is frozen and will be
  964. * cleaned up by ->post_internal_cmd().
  965. */
  966. if (qc->flags & ATA_QCFLAG_ACTIVE) {
  967. qc->err_mask |= AC_ERR_TIMEOUT;
  968. if (ap->ops->error_handler)
  969. ata_port_freeze(ap);
  970. else
  971. ata_qc_complete(qc);
  972. if (ata_msg_warn(ap))
  973. ata_dev_printk(dev, KERN_WARNING,
  974. "qc timeout (cmd 0x%x)\n", command);
  975. }
  976. spin_unlock_irqrestore(ap->lock, flags);
  977. }
  978. /* do post_internal_cmd */
  979. if (ap->ops->post_internal_cmd)
  980. ap->ops->post_internal_cmd(qc);
  981. if (qc->flags & ATA_QCFLAG_FAILED && !qc->err_mask) {
  982. if (ata_msg_warn(ap))
  983. ata_dev_printk(dev, KERN_WARNING,
  984. "zero err_mask for failed "
  985. "internal command, assuming AC_ERR_OTHER\n");
  986. qc->err_mask |= AC_ERR_OTHER;
  987. }
  988. /* finish up */
  989. spin_lock_irqsave(ap->lock, flags);
  990. *tf = qc->result_tf;
  991. err_mask = qc->err_mask;
  992. ata_qc_free(qc);
  993. ap->active_tag = preempted_tag;
  994. ap->sactive = preempted_sactive;
  995. ap->qc_active = preempted_qc_active;
  996. /* XXX - Some LLDDs (sata_mv) disable port on command failure.
  997. * Until those drivers are fixed, we detect the condition
  998. * here, fail the command with AC_ERR_SYSTEM and reenable the
  999. * port.
  1000. *
  1001. * Note that this doesn't change any behavior as internal
  1002. * command failure results in disabling the device in the
  1003. * higher layer for LLDDs without new reset/EH callbacks.
  1004. *
  1005. * Kill the following code as soon as those drivers are fixed.
  1006. */
  1007. if (ap->flags & ATA_FLAG_DISABLED) {
  1008. err_mask |= AC_ERR_SYSTEM;
  1009. ata_port_probe(ap);
  1010. }
  1011. spin_unlock_irqrestore(ap->lock, flags);
  1012. return err_mask;
  1013. }
  1014. /**
  1015. * ata_do_simple_cmd - execute simple internal command
  1016. * @dev: Device to which the command is sent
  1017. * @cmd: Opcode to execute
  1018. *
  1019. * Execute a 'simple' command, that only consists of the opcode
  1020. * 'cmd' itself, without filling any other registers
  1021. *
  1022. * LOCKING:
  1023. * Kernel thread context (may sleep).
  1024. *
  1025. * RETURNS:
  1026. * Zero on success, AC_ERR_* mask on failure
  1027. */
  1028. unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
  1029. {
  1030. struct ata_taskfile tf;
  1031. ata_tf_init(dev, &tf);
  1032. tf.command = cmd;
  1033. tf.flags |= ATA_TFLAG_DEVICE;
  1034. tf.protocol = ATA_PROT_NODATA;
  1035. return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
  1036. }
  1037. /**
  1038. * ata_pio_need_iordy - check if iordy needed
  1039. * @adev: ATA device
  1040. *
  1041. * Check if the current speed of the device requires IORDY. Used
  1042. * by various controllers for chip configuration.
  1043. */
  1044. unsigned int ata_pio_need_iordy(const struct ata_device *adev)
  1045. {
  1046. int pio;
  1047. int speed = adev->pio_mode - XFER_PIO_0;
  1048. if (speed < 2)
  1049. return 0;
  1050. if (speed > 2)
  1051. return 1;
  1052. /* If we have no drive specific rule, then PIO 2 is non IORDY */
  1053. if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
  1054. pio = adev->id[ATA_ID_EIDE_PIO];
  1055. /* Is the speed faster than the drive allows non IORDY ? */
  1056. if (pio) {
  1057. /* This is cycle times not frequency - watch the logic! */
  1058. if (pio > 240) /* PIO2 is 240nS per cycle */
  1059. return 1;
  1060. return 0;
  1061. }
  1062. }
  1063. return 0;
  1064. }
  1065. /**
  1066. * ata_dev_read_id - Read ID data from the specified device
  1067. * @dev: target device
  1068. * @p_class: pointer to class of the target device (may be changed)
  1069. * @flags: ATA_READID_* flags
  1070. * @id: buffer to read IDENTIFY data into
  1071. *
  1072. * Read ID data from the specified device. ATA_CMD_ID_ATA is
  1073. * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
  1074. * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
  1075. * for pre-ATA4 drives.
  1076. *
  1077. * LOCKING:
  1078. * Kernel thread context (may sleep)
  1079. *
  1080. * RETURNS:
  1081. * 0 on success, -errno otherwise.
  1082. */
  1083. int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
  1084. unsigned int flags, u16 *id)
  1085. {
  1086. struct ata_port *ap = dev->ap;
  1087. unsigned int class = *p_class;
  1088. struct ata_taskfile tf;
  1089. unsigned int err_mask = 0;
  1090. const char *reason;
  1091. int rc;
  1092. if (ata_msg_ctl(ap))
  1093. ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER, host %u, dev %u\n",
  1094. __FUNCTION__, ap->id, dev->devno);
  1095. ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
  1096. retry:
  1097. ata_tf_init(dev, &tf);
  1098. switch (class) {
  1099. case ATA_DEV_ATA:
  1100. tf.command = ATA_CMD_ID_ATA;
  1101. break;
  1102. case ATA_DEV_ATAPI:
  1103. tf.command = ATA_CMD_ID_ATAPI;
  1104. break;
  1105. default:
  1106. rc = -ENODEV;
  1107. reason = "unsupported class";
  1108. goto err_out;
  1109. }
  1110. tf.protocol = ATA_PROT_PIO;
  1111. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
  1112. id, sizeof(id[0]) * ATA_ID_WORDS);
  1113. if (err_mask) {
  1114. rc = -EIO;
  1115. reason = "I/O error";
  1116. goto err_out;
  1117. }
  1118. swap_buf_le16(id, ATA_ID_WORDS);
  1119. /* sanity check */
  1120. rc = -EINVAL;
  1121. reason = "device reports illegal type";
  1122. if (class == ATA_DEV_ATA) {
  1123. if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
  1124. goto err_out;
  1125. } else {
  1126. if (ata_id_is_ata(id))
  1127. goto err_out;
  1128. }
  1129. if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
  1130. /*
  1131. * The exact sequence expected by certain pre-ATA4 drives is:
  1132. * SRST RESET
  1133. * IDENTIFY
  1134. * INITIALIZE DEVICE PARAMETERS
  1135. * anything else..
  1136. * Some drives were very specific about that exact sequence.
  1137. */
  1138. if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
  1139. err_mask = ata_dev_init_params(dev, id[3], id[6]);
  1140. if (err_mask) {
  1141. rc = -EIO;
  1142. reason = "INIT_DEV_PARAMS failed";
  1143. goto err_out;
  1144. }
  1145. /* current CHS translation info (id[53-58]) might be
  1146. * changed. reread the identify device info.
  1147. */
  1148. flags &= ~ATA_READID_POSTRESET;
  1149. goto retry;
  1150. }
  1151. }
  1152. *p_class = class;
  1153. return 0;
  1154. err_out:
  1155. if (ata_msg_warn(ap))
  1156. ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
  1157. "(%s, err_mask=0x%x)\n", reason, err_mask);
  1158. return rc;
  1159. }
  1160. static inline u8 ata_dev_knobble(struct ata_device *dev)
  1161. {
  1162. return ((dev->ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
  1163. }
  1164. static void ata_dev_config_ncq(struct ata_device *dev,
  1165. char *desc, size_t desc_sz)
  1166. {
  1167. struct ata_port *ap = dev->ap;
  1168. int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
  1169. if (!ata_id_has_ncq(dev->id)) {
  1170. desc[0] = '\0';
  1171. return;
  1172. }
  1173. if (ata_device_blacklisted(dev) & ATA_HORKAGE_NONCQ) {
  1174. snprintf(desc, desc_sz, "NCQ (not used)");
  1175. return;
  1176. }
  1177. if (ap->flags & ATA_FLAG_NCQ) {
  1178. hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
  1179. dev->flags |= ATA_DFLAG_NCQ;
  1180. }
  1181. if (hdepth >= ddepth)
  1182. snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
  1183. else
  1184. snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
  1185. }
  1186. static void ata_set_port_max_cmd_len(struct ata_port *ap)
  1187. {
  1188. int i;
  1189. if (ap->scsi_host) {
  1190. unsigned int len = 0;
  1191. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1192. len = max(len, ap->device[i].cdb_len);
  1193. ap->scsi_host->max_cmd_len = len;
  1194. }
  1195. }
  1196. /**
  1197. * ata_dev_configure - Configure the specified ATA/ATAPI device
  1198. * @dev: Target device to configure
  1199. *
  1200. * Configure @dev according to @dev->id. Generic and low-level
  1201. * driver specific fixups are also applied.
  1202. *
  1203. * LOCKING:
  1204. * Kernel thread context (may sleep)
  1205. *
  1206. * RETURNS:
  1207. * 0 on success, -errno otherwise
  1208. */
  1209. int ata_dev_configure(struct ata_device *dev)
  1210. {
  1211. struct ata_port *ap = dev->ap;
  1212. int print_info = ap->eh_context.i.flags & ATA_EHI_PRINTINFO;
  1213. const u16 *id = dev->id;
  1214. unsigned int xfer_mask;
  1215. char revbuf[7]; /* XYZ-99\0 */
  1216. int rc;
  1217. if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
  1218. ata_dev_printk(dev, KERN_INFO,
  1219. "%s: ENTER/EXIT (host %u, dev %u) -- nodev\n",
  1220. __FUNCTION__, ap->id, dev->devno);
  1221. return 0;
  1222. }
  1223. if (ata_msg_probe(ap))
  1224. ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER, host %u, dev %u\n",
  1225. __FUNCTION__, ap->id, dev->devno);
  1226. /* print device capabilities */
  1227. if (ata_msg_probe(ap))
  1228. ata_dev_printk(dev, KERN_DEBUG,
  1229. "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
  1230. "85:%04x 86:%04x 87:%04x 88:%04x\n",
  1231. __FUNCTION__,
  1232. id[49], id[82], id[83], id[84],
  1233. id[85], id[86], id[87], id[88]);
  1234. /* initialize to-be-configured parameters */
  1235. dev->flags &= ~ATA_DFLAG_CFG_MASK;
  1236. dev->max_sectors = 0;
  1237. dev->cdb_len = 0;
  1238. dev->n_sectors = 0;
  1239. dev->cylinders = 0;
  1240. dev->heads = 0;
  1241. dev->sectors = 0;
  1242. /*
  1243. * common ATA, ATAPI feature tests
  1244. */
  1245. /* find max transfer mode; for printk only */
  1246. xfer_mask = ata_id_xfermask(id);
  1247. if (ata_msg_probe(ap))
  1248. ata_dump_id(id);
  1249. /* ATA-specific feature tests */
  1250. if (dev->class == ATA_DEV_ATA) {
  1251. if (ata_id_is_cfa(id)) {
  1252. if (id[162] & 1) /* CPRM may make this media unusable */
  1253. ata_dev_printk(dev, KERN_WARNING, "ata%u: device %u supports DRM functions and may not be fully accessable.\n",
  1254. ap->id, dev->devno);
  1255. snprintf(revbuf, 7, "CFA");
  1256. }
  1257. else
  1258. snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
  1259. dev->n_sectors = ata_id_n_sectors(id);
  1260. if (ata_id_has_lba(id)) {
  1261. const char *lba_desc;
  1262. char ncq_desc[20];
  1263. lba_desc = "LBA";
  1264. dev->flags |= ATA_DFLAG_LBA;
  1265. if (ata_id_has_lba48(id)) {
  1266. dev->flags |= ATA_DFLAG_LBA48;
  1267. lba_desc = "LBA48";
  1268. if (dev->n_sectors >= (1UL << 28) &&
  1269. ata_id_has_flush_ext(id))
  1270. dev->flags |= ATA_DFLAG_FLUSH_EXT;
  1271. }
  1272. /* config NCQ */
  1273. ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
  1274. /* print device info to dmesg */
  1275. if (ata_msg_drv(ap) && print_info)
  1276. ata_dev_printk(dev, KERN_INFO, "%s, "
  1277. "max %s, %Lu sectors: %s %s\n",
  1278. revbuf,
  1279. ata_mode_string(xfer_mask),
  1280. (unsigned long long)dev->n_sectors,
  1281. lba_desc, ncq_desc);
  1282. } else {
  1283. /* CHS */
  1284. /* Default translation */
  1285. dev->cylinders = id[1];
  1286. dev->heads = id[3];
  1287. dev->sectors = id[6];
  1288. if (ata_id_current_chs_valid(id)) {
  1289. /* Current CHS translation is valid. */
  1290. dev->cylinders = id[54];
  1291. dev->heads = id[55];
  1292. dev->sectors = id[56];
  1293. }
  1294. /* print device info to dmesg */
  1295. if (ata_msg_drv(ap) && print_info)
  1296. ata_dev_printk(dev, KERN_INFO, "%s, "
  1297. "max %s, %Lu sectors: CHS %u/%u/%u\n",
  1298. revbuf,
  1299. ata_mode_string(xfer_mask),
  1300. (unsigned long long)dev->n_sectors,
  1301. dev->cylinders, dev->heads,
  1302. dev->sectors);
  1303. }
  1304. if (dev->id[59] & 0x100) {
  1305. dev->multi_count = dev->id[59] & 0xff;
  1306. if (ata_msg_drv(ap) && print_info)
  1307. ata_dev_printk(dev, KERN_INFO,
  1308. "ata%u: dev %u multi count %u\n",
  1309. ap->id, dev->devno, dev->multi_count);
  1310. }
  1311. dev->cdb_len = 16;
  1312. }
  1313. /* ATAPI-specific feature tests */
  1314. else if (dev->class == ATA_DEV_ATAPI) {
  1315. char *cdb_intr_string = "";
  1316. rc = atapi_cdb_len(id);
  1317. if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
  1318. if (ata_msg_warn(ap))
  1319. ata_dev_printk(dev, KERN_WARNING,
  1320. "unsupported CDB len\n");
  1321. rc = -EINVAL;
  1322. goto err_out_nosup;
  1323. }
  1324. dev->cdb_len = (unsigned int) rc;
  1325. if (ata_id_cdb_intr(dev->id)) {
  1326. dev->flags |= ATA_DFLAG_CDB_INTR;
  1327. cdb_intr_string = ", CDB intr";
  1328. }
  1329. /* print device info to dmesg */
  1330. if (ata_msg_drv(ap) && print_info)
  1331. ata_dev_printk(dev, KERN_INFO, "ATAPI, max %s%s\n",
  1332. ata_mode_string(xfer_mask),
  1333. cdb_intr_string);
  1334. }
  1335. /* determine max_sectors */
  1336. dev->max_sectors = ATA_MAX_SECTORS;
  1337. if (dev->flags & ATA_DFLAG_LBA48)
  1338. dev->max_sectors = ATA_MAX_SECTORS_LBA48;
  1339. if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
  1340. /* Let the user know. We don't want to disallow opens for
  1341. rescue purposes, or in case the vendor is just a blithering
  1342. idiot */
  1343. if (print_info) {
  1344. ata_dev_printk(dev, KERN_WARNING,
  1345. "Drive reports diagnostics failure. This may indicate a drive\n");
  1346. ata_dev_printk(dev, KERN_WARNING,
  1347. "fault or invalid emulation. Contact drive vendor for information.\n");
  1348. }
  1349. }
  1350. ata_set_port_max_cmd_len(ap);
  1351. /* limit bridge transfers to udma5, 200 sectors */
  1352. if (ata_dev_knobble(dev)) {
  1353. if (ata_msg_drv(ap) && print_info)
  1354. ata_dev_printk(dev, KERN_INFO,
  1355. "applying bridge limits\n");
  1356. dev->udma_mask &= ATA_UDMA5;
  1357. dev->max_sectors = ATA_MAX_SECTORS;
  1358. }
  1359. if (ap->ops->dev_config)
  1360. ap->ops->dev_config(ap, dev);
  1361. if (ata_msg_probe(ap))
  1362. ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n",
  1363. __FUNCTION__, ata_chk_status(ap));
  1364. return 0;
  1365. err_out_nosup:
  1366. if (ata_msg_probe(ap))
  1367. ata_dev_printk(dev, KERN_DEBUG,
  1368. "%s: EXIT, err\n", __FUNCTION__);
  1369. return rc;
  1370. }
  1371. /**
  1372. * ata_bus_probe - Reset and probe ATA bus
  1373. * @ap: Bus to probe
  1374. *
  1375. * Master ATA bus probing function. Initiates a hardware-dependent
  1376. * bus reset, then attempts to identify any devices found on
  1377. * the bus.
  1378. *
  1379. * LOCKING:
  1380. * PCI/etc. bus probe sem.
  1381. *
  1382. * RETURNS:
  1383. * Zero on success, negative errno otherwise.
  1384. */
  1385. int ata_bus_probe(struct ata_port *ap)
  1386. {
  1387. unsigned int classes[ATA_MAX_DEVICES];
  1388. int tries[ATA_MAX_DEVICES];
  1389. int i, rc, down_xfermask;
  1390. struct ata_device *dev;
  1391. ata_port_probe(ap);
  1392. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1393. tries[i] = ATA_PROBE_MAX_TRIES;
  1394. retry:
  1395. down_xfermask = 0;
  1396. /* reset and determine device classes */
  1397. ap->ops->phy_reset(ap);
  1398. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1399. dev = &ap->device[i];
  1400. if (!(ap->flags & ATA_FLAG_DISABLED) &&
  1401. dev->class != ATA_DEV_UNKNOWN)
  1402. classes[dev->devno] = dev->class;
  1403. else
  1404. classes[dev->devno] = ATA_DEV_NONE;
  1405. dev->class = ATA_DEV_UNKNOWN;
  1406. }
  1407. ata_port_probe(ap);
  1408. /* after the reset the device state is PIO 0 and the controller
  1409. state is undefined. Record the mode */
  1410. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1411. ap->device[i].pio_mode = XFER_PIO_0;
  1412. /* read IDENTIFY page and configure devices */
  1413. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1414. dev = &ap->device[i];
  1415. if (tries[i])
  1416. dev->class = classes[i];
  1417. if (!ata_dev_enabled(dev))
  1418. continue;
  1419. rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
  1420. dev->id);
  1421. if (rc)
  1422. goto fail;
  1423. ap->eh_context.i.flags |= ATA_EHI_PRINTINFO;
  1424. rc = ata_dev_configure(dev);
  1425. ap->eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
  1426. if (rc)
  1427. goto fail;
  1428. }
  1429. /* configure transfer mode */
  1430. rc = ata_set_mode(ap, &dev);
  1431. if (rc) {
  1432. down_xfermask = 1;
  1433. goto fail;
  1434. }
  1435. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1436. if (ata_dev_enabled(&ap->device[i]))
  1437. return 0;
  1438. /* no device present, disable port */
  1439. ata_port_disable(ap);
  1440. ap->ops->port_disable(ap);
  1441. return -ENODEV;
  1442. fail:
  1443. switch (rc) {
  1444. case -EINVAL:
  1445. case -ENODEV:
  1446. tries[dev->devno] = 0;
  1447. break;
  1448. case -EIO:
  1449. sata_down_spd_limit(ap);
  1450. /* fall through */
  1451. default:
  1452. tries[dev->devno]--;
  1453. if (down_xfermask &&
  1454. ata_down_xfermask_limit(dev, tries[dev->devno] == 1))
  1455. tries[dev->devno] = 0;
  1456. }
  1457. if (!tries[dev->devno]) {
  1458. ata_down_xfermask_limit(dev, 1);
  1459. ata_dev_disable(dev);
  1460. }
  1461. goto retry;
  1462. }
  1463. /**
  1464. * ata_port_probe - Mark port as enabled
  1465. * @ap: Port for which we indicate enablement
  1466. *
  1467. * Modify @ap data structure such that the system
  1468. * thinks that the entire port is enabled.
  1469. *
  1470. * LOCKING: host lock, or some other form of
  1471. * serialization.
  1472. */
  1473. void ata_port_probe(struct ata_port *ap)
  1474. {
  1475. ap->flags &= ~ATA_FLAG_DISABLED;
  1476. }
  1477. /**
  1478. * sata_print_link_status - Print SATA link status
  1479. * @ap: SATA port to printk link status about
  1480. *
  1481. * This function prints link speed and status of a SATA link.
  1482. *
  1483. * LOCKING:
  1484. * None.
  1485. */
  1486. static void sata_print_link_status(struct ata_port *ap)
  1487. {
  1488. u32 sstatus, scontrol, tmp;
  1489. if (sata_scr_read(ap, SCR_STATUS, &sstatus))
  1490. return;
  1491. sata_scr_read(ap, SCR_CONTROL, &scontrol);
  1492. if (ata_port_online(ap)) {
  1493. tmp = (sstatus >> 4) & 0xf;
  1494. ata_port_printk(ap, KERN_INFO,
  1495. "SATA link up %s (SStatus %X SControl %X)\n",
  1496. sata_spd_string(tmp), sstatus, scontrol);
  1497. } else {
  1498. ata_port_printk(ap, KERN_INFO,
  1499. "SATA link down (SStatus %X SControl %X)\n",
  1500. sstatus, scontrol);
  1501. }
  1502. }
  1503. /**
  1504. * __sata_phy_reset - Wake/reset a low-level SATA PHY
  1505. * @ap: SATA port associated with target SATA PHY.
  1506. *
  1507. * This function issues commands to standard SATA Sxxx
  1508. * PHY registers, to wake up the phy (and device), and
  1509. * clear any reset condition.
  1510. *
  1511. * LOCKING:
  1512. * PCI/etc. bus probe sem.
  1513. *
  1514. */
  1515. void __sata_phy_reset(struct ata_port *ap)
  1516. {
  1517. u32 sstatus;
  1518. unsigned long timeout = jiffies + (HZ * 5);
  1519. if (ap->flags & ATA_FLAG_SATA_RESET) {
  1520. /* issue phy wake/reset */
  1521. sata_scr_write_flush(ap, SCR_CONTROL, 0x301);
  1522. /* Couldn't find anything in SATA I/II specs, but
  1523. * AHCI-1.1 10.4.2 says at least 1 ms. */
  1524. mdelay(1);
  1525. }
  1526. /* phy wake/clear reset */
  1527. sata_scr_write_flush(ap, SCR_CONTROL, 0x300);
  1528. /* wait for phy to become ready, if necessary */
  1529. do {
  1530. msleep(200);
  1531. sata_scr_read(ap, SCR_STATUS, &sstatus);
  1532. if ((sstatus & 0xf) != 1)
  1533. break;
  1534. } while (time_before(jiffies, timeout));
  1535. /* print link status */
  1536. sata_print_link_status(ap);
  1537. /* TODO: phy layer with polling, timeouts, etc. */
  1538. if (!ata_port_offline(ap))
  1539. ata_port_probe(ap);
  1540. else
  1541. ata_port_disable(ap);
  1542. if (ap->flags & ATA_FLAG_DISABLED)
  1543. return;
  1544. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  1545. ata_port_disable(ap);
  1546. return;
  1547. }
  1548. ap->cbl = ATA_CBL_SATA;
  1549. }
  1550. /**
  1551. * sata_phy_reset - Reset SATA bus.
  1552. * @ap: SATA port associated with target SATA PHY.
  1553. *
  1554. * This function resets the SATA bus, and then probes
  1555. * the bus for devices.
  1556. *
  1557. * LOCKING:
  1558. * PCI/etc. bus probe sem.
  1559. *
  1560. */
  1561. void sata_phy_reset(struct ata_port *ap)
  1562. {
  1563. __sata_phy_reset(ap);
  1564. if (ap->flags & ATA_FLAG_DISABLED)
  1565. return;
  1566. ata_bus_reset(ap);
  1567. }
  1568. /**
  1569. * ata_dev_pair - return other device on cable
  1570. * @adev: device
  1571. *
  1572. * Obtain the other device on the same cable, or if none is
  1573. * present NULL is returned
  1574. */
  1575. struct ata_device *ata_dev_pair(struct ata_device *adev)
  1576. {
  1577. struct ata_port *ap = adev->ap;
  1578. struct ata_device *pair = &ap->device[1 - adev->devno];
  1579. if (!ata_dev_enabled(pair))
  1580. return NULL;
  1581. return pair;
  1582. }
  1583. /**
  1584. * ata_port_disable - Disable port.
  1585. * @ap: Port to be disabled.
  1586. *
  1587. * Modify @ap data structure such that the system
  1588. * thinks that the entire port is disabled, and should
  1589. * never attempt to probe or communicate with devices
  1590. * on this port.
  1591. *
  1592. * LOCKING: host lock, or some other form of
  1593. * serialization.
  1594. */
  1595. void ata_port_disable(struct ata_port *ap)
  1596. {
  1597. ap->device[0].class = ATA_DEV_NONE;
  1598. ap->device[1].class = ATA_DEV_NONE;
  1599. ap->flags |= ATA_FLAG_DISABLED;
  1600. }
  1601. /**
  1602. * sata_down_spd_limit - adjust SATA spd limit downward
  1603. * @ap: Port to adjust SATA spd limit for
  1604. *
  1605. * Adjust SATA spd limit of @ap downward. Note that this
  1606. * function only adjusts the limit. The change must be applied
  1607. * using sata_set_spd().
  1608. *
  1609. * LOCKING:
  1610. * Inherited from caller.
  1611. *
  1612. * RETURNS:
  1613. * 0 on success, negative errno on failure
  1614. */
  1615. int sata_down_spd_limit(struct ata_port *ap)
  1616. {
  1617. u32 sstatus, spd, mask;
  1618. int rc, highbit;
  1619. rc = sata_scr_read(ap, SCR_STATUS, &sstatus);
  1620. if (rc)
  1621. return rc;
  1622. mask = ap->sata_spd_limit;
  1623. if (mask <= 1)
  1624. return -EINVAL;
  1625. highbit = fls(mask) - 1;
  1626. mask &= ~(1 << highbit);
  1627. spd = (sstatus >> 4) & 0xf;
  1628. if (spd <= 1)
  1629. return -EINVAL;
  1630. spd--;
  1631. mask &= (1 << spd) - 1;
  1632. if (!mask)
  1633. return -EINVAL;
  1634. ap->sata_spd_limit = mask;
  1635. ata_port_printk(ap, KERN_WARNING, "limiting SATA link speed to %s\n",
  1636. sata_spd_string(fls(mask)));
  1637. return 0;
  1638. }
  1639. static int __sata_set_spd_needed(struct ata_port *ap, u32 *scontrol)
  1640. {
  1641. u32 spd, limit;
  1642. if (ap->sata_spd_limit == UINT_MAX)
  1643. limit = 0;
  1644. else
  1645. limit = fls(ap->sata_spd_limit);
  1646. spd = (*scontrol >> 4) & 0xf;
  1647. *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
  1648. return spd != limit;
  1649. }
  1650. /**
  1651. * sata_set_spd_needed - is SATA spd configuration needed
  1652. * @ap: Port in question
  1653. *
  1654. * Test whether the spd limit in SControl matches
  1655. * @ap->sata_spd_limit. This function is used to determine
  1656. * whether hardreset is necessary to apply SATA spd
  1657. * configuration.
  1658. *
  1659. * LOCKING:
  1660. * Inherited from caller.
  1661. *
  1662. * RETURNS:
  1663. * 1 if SATA spd configuration is needed, 0 otherwise.
  1664. */
  1665. int sata_set_spd_needed(struct ata_port *ap)
  1666. {
  1667. u32 scontrol;
  1668. if (sata_scr_read(ap, SCR_CONTROL, &scontrol))
  1669. return 0;
  1670. return __sata_set_spd_needed(ap, &scontrol);
  1671. }
  1672. /**
  1673. * sata_set_spd - set SATA spd according to spd limit
  1674. * @ap: Port to set SATA spd for
  1675. *
  1676. * Set SATA spd of @ap according to sata_spd_limit.
  1677. *
  1678. * LOCKING:
  1679. * Inherited from caller.
  1680. *
  1681. * RETURNS:
  1682. * 0 if spd doesn't need to be changed, 1 if spd has been
  1683. * changed. Negative errno if SCR registers are inaccessible.
  1684. */
  1685. int sata_set_spd(struct ata_port *ap)
  1686. {
  1687. u32 scontrol;
  1688. int rc;
  1689. if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
  1690. return rc;
  1691. if (!__sata_set_spd_needed(ap, &scontrol))
  1692. return 0;
  1693. if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
  1694. return rc;
  1695. return 1;
  1696. }
  1697. /*
  1698. * This mode timing computation functionality is ported over from
  1699. * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
  1700. */
  1701. /*
  1702. * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
  1703. * These were taken from ATA/ATAPI-6 standard, rev 0a, except
  1704. * for UDMA6, which is currently supported only by Maxtor drives.
  1705. *
  1706. * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
  1707. */
  1708. static const struct ata_timing ata_timing[] = {
  1709. { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
  1710. { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
  1711. { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
  1712. { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
  1713. { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
  1714. { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
  1715. { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
  1716. { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
  1717. { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
  1718. /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
  1719. { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
  1720. { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
  1721. { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
  1722. { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
  1723. { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
  1724. { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
  1725. { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
  1726. { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
  1727. { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
  1728. { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
  1729. { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
  1730. { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
  1731. { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
  1732. /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
  1733. { 0xFF }
  1734. };
  1735. #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
  1736. #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
  1737. static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
  1738. {
  1739. q->setup = EZ(t->setup * 1000, T);
  1740. q->act8b = EZ(t->act8b * 1000, T);
  1741. q->rec8b = EZ(t->rec8b * 1000, T);
  1742. q->cyc8b = EZ(t->cyc8b * 1000, T);
  1743. q->active = EZ(t->active * 1000, T);
  1744. q->recover = EZ(t->recover * 1000, T);
  1745. q->cycle = EZ(t->cycle * 1000, T);
  1746. q->udma = EZ(t->udma * 1000, UT);
  1747. }
  1748. void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
  1749. struct ata_timing *m, unsigned int what)
  1750. {
  1751. if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
  1752. if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
  1753. if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
  1754. if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
  1755. if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
  1756. if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
  1757. if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
  1758. if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
  1759. }
  1760. static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
  1761. {
  1762. const struct ata_timing *t;
  1763. for (t = ata_timing; t->mode != speed; t++)
  1764. if (t->mode == 0xFF)
  1765. return NULL;
  1766. return t;
  1767. }
  1768. int ata_timing_compute(struct ata_device *adev, unsigned short speed,
  1769. struct ata_timing *t, int T, int UT)
  1770. {
  1771. const struct ata_timing *s;
  1772. struct ata_timing p;
  1773. /*
  1774. * Find the mode.
  1775. */
  1776. if (!(s = ata_timing_find_mode(speed)))
  1777. return -EINVAL;
  1778. memcpy(t, s, sizeof(*s));
  1779. /*
  1780. * If the drive is an EIDE drive, it can tell us it needs extended
  1781. * PIO/MW_DMA cycle timing.
  1782. */
  1783. if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
  1784. memset(&p, 0, sizeof(p));
  1785. if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
  1786. if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
  1787. else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
  1788. } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
  1789. p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
  1790. }
  1791. ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
  1792. }
  1793. /*
  1794. * Convert the timing to bus clock counts.
  1795. */
  1796. ata_timing_quantize(t, t, T, UT);
  1797. /*
  1798. * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
  1799. * S.M.A.R.T * and some other commands. We have to ensure that the
  1800. * DMA cycle timing is slower/equal than the fastest PIO timing.
  1801. */
  1802. if (speed > XFER_PIO_4) {
  1803. ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
  1804. ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
  1805. }
  1806. /*
  1807. * Lengthen active & recovery time so that cycle time is correct.
  1808. */
  1809. if (t->act8b + t->rec8b < t->cyc8b) {
  1810. t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
  1811. t->rec8b = t->cyc8b - t->act8b;
  1812. }
  1813. if (t->active + t->recover < t->cycle) {
  1814. t->active += (t->cycle - (t->active + t->recover)) / 2;
  1815. t->recover = t->cycle - t->active;
  1816. }
  1817. return 0;
  1818. }
  1819. /**
  1820. * ata_down_xfermask_limit - adjust dev xfer masks downward
  1821. * @dev: Device to adjust xfer masks
  1822. * @force_pio0: Force PIO0
  1823. *
  1824. * Adjust xfer masks of @dev downward. Note that this function
  1825. * does not apply the change. Invoking ata_set_mode() afterwards
  1826. * will apply the limit.
  1827. *
  1828. * LOCKING:
  1829. * Inherited from caller.
  1830. *
  1831. * RETURNS:
  1832. * 0 on success, negative errno on failure
  1833. */
  1834. int ata_down_xfermask_limit(struct ata_device *dev, int force_pio0)
  1835. {
  1836. unsigned long xfer_mask;
  1837. int highbit;
  1838. xfer_mask = ata_pack_xfermask(dev->pio_mask, dev->mwdma_mask,
  1839. dev->udma_mask);
  1840. if (!xfer_mask)
  1841. goto fail;
  1842. /* don't gear down to MWDMA from UDMA, go directly to PIO */
  1843. if (xfer_mask & ATA_MASK_UDMA)
  1844. xfer_mask &= ~ATA_MASK_MWDMA;
  1845. highbit = fls(xfer_mask) - 1;
  1846. xfer_mask &= ~(1 << highbit);
  1847. if (force_pio0)
  1848. xfer_mask &= 1 << ATA_SHIFT_PIO;
  1849. if (!xfer_mask)
  1850. goto fail;
  1851. ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
  1852. &dev->udma_mask);
  1853. ata_dev_printk(dev, KERN_WARNING, "limiting speed to %s\n",
  1854. ata_mode_string(xfer_mask));
  1855. return 0;
  1856. fail:
  1857. return -EINVAL;
  1858. }
  1859. static int ata_dev_set_mode(struct ata_device *dev)
  1860. {
  1861. struct ata_eh_context *ehc = &dev->ap->eh_context;
  1862. unsigned int err_mask;
  1863. int rc;
  1864. dev->flags &= ~ATA_DFLAG_PIO;
  1865. if (dev->xfer_shift == ATA_SHIFT_PIO)
  1866. dev->flags |= ATA_DFLAG_PIO;
  1867. err_mask = ata_dev_set_xfermode(dev);
  1868. if (err_mask) {
  1869. ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
  1870. "(err_mask=0x%x)\n", err_mask);
  1871. return -EIO;
  1872. }
  1873. ehc->i.flags |= ATA_EHI_POST_SETMODE;
  1874. rc = ata_dev_revalidate(dev, 0);
  1875. ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
  1876. if (rc)
  1877. return rc;
  1878. DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
  1879. dev->xfer_shift, (int)dev->xfer_mode);
  1880. ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
  1881. ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
  1882. return 0;
  1883. }
  1884. /**
  1885. * ata_set_mode - Program timings and issue SET FEATURES - XFER
  1886. * @ap: port on which timings will be programmed
  1887. * @r_failed_dev: out paramter for failed device
  1888. *
  1889. * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
  1890. * ata_set_mode() fails, pointer to the failing device is
  1891. * returned in @r_failed_dev.
  1892. *
  1893. * LOCKING:
  1894. * PCI/etc. bus probe sem.
  1895. *
  1896. * RETURNS:
  1897. * 0 on success, negative errno otherwise
  1898. */
  1899. int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
  1900. {
  1901. struct ata_device *dev;
  1902. int i, rc = 0, used_dma = 0, found = 0;
  1903. /* has private set_mode? */
  1904. if (ap->ops->set_mode) {
  1905. /* FIXME: make ->set_mode handle no device case and
  1906. * return error code and failing device on failure.
  1907. */
  1908. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1909. if (ata_dev_ready(&ap->device[i])) {
  1910. ap->ops->set_mode(ap);
  1911. break;
  1912. }
  1913. }
  1914. return 0;
  1915. }
  1916. /* step 1: calculate xfer_mask */
  1917. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1918. unsigned int pio_mask, dma_mask;
  1919. dev = &ap->device[i];
  1920. if (!ata_dev_enabled(dev))
  1921. continue;
  1922. ata_dev_xfermask(dev);
  1923. pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
  1924. dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
  1925. dev->pio_mode = ata_xfer_mask2mode(pio_mask);
  1926. dev->dma_mode = ata_xfer_mask2mode(dma_mask);
  1927. found = 1;
  1928. if (dev->dma_mode)
  1929. used_dma = 1;
  1930. }
  1931. if (!found)
  1932. goto out;
  1933. /* step 2: always set host PIO timings */
  1934. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1935. dev = &ap->device[i];
  1936. if (!ata_dev_enabled(dev))
  1937. continue;
  1938. if (!dev->pio_mode) {
  1939. ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
  1940. rc = -EINVAL;
  1941. goto out;
  1942. }
  1943. dev->xfer_mode = dev->pio_mode;
  1944. dev->xfer_shift = ATA_SHIFT_PIO;
  1945. if (ap->ops->set_piomode)
  1946. ap->ops->set_piomode(ap, dev);
  1947. }
  1948. /* step 3: set host DMA timings */
  1949. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1950. dev = &ap->device[i];
  1951. if (!ata_dev_enabled(dev) || !dev->dma_mode)
  1952. continue;
  1953. dev->xfer_mode = dev->dma_mode;
  1954. dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
  1955. if (ap->ops->set_dmamode)
  1956. ap->ops->set_dmamode(ap, dev);
  1957. }
  1958. /* step 4: update devices' xfer mode */
  1959. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1960. dev = &ap->device[i];
  1961. /* don't udpate suspended devices' xfer mode */
  1962. if (!ata_dev_ready(dev))
  1963. continue;
  1964. rc = ata_dev_set_mode(dev);
  1965. if (rc)
  1966. goto out;
  1967. }
  1968. /* Record simplex status. If we selected DMA then the other
  1969. * host channels are not permitted to do so.
  1970. */
  1971. if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
  1972. ap->host->simplex_claimed = 1;
  1973. /* step5: chip specific finalisation */
  1974. if (ap->ops->post_set_mode)
  1975. ap->ops->post_set_mode(ap);
  1976. out:
  1977. if (rc)
  1978. *r_failed_dev = dev;
  1979. return rc;
  1980. }
  1981. /**
  1982. * ata_tf_to_host - issue ATA taskfile to host controller
  1983. * @ap: port to which command is being issued
  1984. * @tf: ATA taskfile register set
  1985. *
  1986. * Issues ATA taskfile register set to ATA host controller,
  1987. * with proper synchronization with interrupt handler and
  1988. * other threads.
  1989. *
  1990. * LOCKING:
  1991. * spin_lock_irqsave(host lock)
  1992. */
  1993. static inline void ata_tf_to_host(struct ata_port *ap,
  1994. const struct ata_taskfile *tf)
  1995. {
  1996. ap->ops->tf_load(ap, tf);
  1997. ap->ops->exec_command(ap, tf);
  1998. }
  1999. /**
  2000. * ata_busy_sleep - sleep until BSY clears, or timeout
  2001. * @ap: port containing status register to be polled
  2002. * @tmout_pat: impatience timeout
  2003. * @tmout: overall timeout
  2004. *
  2005. * Sleep until ATA Status register bit BSY clears,
  2006. * or a timeout occurs.
  2007. *
  2008. * LOCKING:
  2009. * Kernel thread context (may sleep).
  2010. *
  2011. * RETURNS:
  2012. * 0 on success, -errno otherwise.
  2013. */
  2014. int ata_busy_sleep(struct ata_port *ap,
  2015. unsigned long tmout_pat, unsigned long tmout)
  2016. {
  2017. unsigned long timer_start, timeout;
  2018. u8 status;
  2019. status = ata_busy_wait(ap, ATA_BUSY, 300);
  2020. timer_start = jiffies;
  2021. timeout = timer_start + tmout_pat;
  2022. while (status != 0xff && (status & ATA_BUSY) &&
  2023. time_before(jiffies, timeout)) {
  2024. msleep(50);
  2025. status = ata_busy_wait(ap, ATA_BUSY, 3);
  2026. }
  2027. if (status != 0xff && (status & ATA_BUSY))
  2028. ata_port_printk(ap, KERN_WARNING,
  2029. "port is slow to respond, please be patient "
  2030. "(Status 0x%x)\n", status);
  2031. timeout = timer_start + tmout;
  2032. while (status != 0xff && (status & ATA_BUSY) &&
  2033. time_before(jiffies, timeout)) {
  2034. msleep(50);
  2035. status = ata_chk_status(ap);
  2036. }
  2037. if (status == 0xff)
  2038. return -ENODEV;
  2039. if (status & ATA_BUSY) {
  2040. ata_port_printk(ap, KERN_ERR, "port failed to respond "
  2041. "(%lu secs, Status 0x%x)\n",
  2042. tmout / HZ, status);
  2043. return -EBUSY;
  2044. }
  2045. return 0;
  2046. }
  2047. static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
  2048. {
  2049. struct ata_ioports *ioaddr = &ap->ioaddr;
  2050. unsigned int dev0 = devmask & (1 << 0);
  2051. unsigned int dev1 = devmask & (1 << 1);
  2052. unsigned long timeout;
  2053. /* if device 0 was found in ata_devchk, wait for its
  2054. * BSY bit to clear
  2055. */
  2056. if (dev0)
  2057. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  2058. /* if device 1 was found in ata_devchk, wait for
  2059. * register access, then wait for BSY to clear
  2060. */
  2061. timeout = jiffies + ATA_TMOUT_BOOT;
  2062. while (dev1) {
  2063. u8 nsect, lbal;
  2064. ap->ops->dev_select(ap, 1);
  2065. if (ap->flags & ATA_FLAG_MMIO) {
  2066. nsect = readb((void __iomem *) ioaddr->nsect_addr);
  2067. lbal = readb((void __iomem *) ioaddr->lbal_addr);
  2068. } else {
  2069. nsect = inb(ioaddr->nsect_addr);
  2070. lbal = inb(ioaddr->lbal_addr);
  2071. }
  2072. if ((nsect == 1) && (lbal == 1))
  2073. break;
  2074. if (time_after(jiffies, timeout)) {
  2075. dev1 = 0;
  2076. break;
  2077. }
  2078. msleep(50); /* give drive a breather */
  2079. }
  2080. if (dev1)
  2081. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  2082. /* is all this really necessary? */
  2083. ap->ops->dev_select(ap, 0);
  2084. if (dev1)
  2085. ap->ops->dev_select(ap, 1);
  2086. if (dev0)
  2087. ap->ops->dev_select(ap, 0);
  2088. }
  2089. static unsigned int ata_bus_softreset(struct ata_port *ap,
  2090. unsigned int devmask)
  2091. {
  2092. struct ata_ioports *ioaddr = &ap->ioaddr;
  2093. DPRINTK("ata%u: bus reset via SRST\n", ap->id);
  2094. /* software reset. causes dev0 to be selected */
  2095. if (ap->flags & ATA_FLAG_MMIO) {
  2096. writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
  2097. udelay(20); /* FIXME: flush */
  2098. writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
  2099. udelay(20); /* FIXME: flush */
  2100. writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
  2101. } else {
  2102. outb(ap->ctl, ioaddr->ctl_addr);
  2103. udelay(10);
  2104. outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
  2105. udelay(10);
  2106. outb(ap->ctl, ioaddr->ctl_addr);
  2107. }
  2108. /* spec mandates ">= 2ms" before checking status.
  2109. * We wait 150ms, because that was the magic delay used for
  2110. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  2111. * between when the ATA command register is written, and then
  2112. * status is checked. Because waiting for "a while" before
  2113. * checking status is fine, post SRST, we perform this magic
  2114. * delay here as well.
  2115. *
  2116. * Old drivers/ide uses the 2mS rule and then waits for ready
  2117. */
  2118. msleep(150);
  2119. /* Before we perform post reset processing we want to see if
  2120. * the bus shows 0xFF because the odd clown forgets the D7
  2121. * pulldown resistor.
  2122. */
  2123. if (ata_check_status(ap) == 0xFF)
  2124. return 0;
  2125. ata_bus_post_reset(ap, devmask);
  2126. return 0;
  2127. }
  2128. /**
  2129. * ata_bus_reset - reset host port and associated ATA channel
  2130. * @ap: port to reset
  2131. *
  2132. * This is typically the first time we actually start issuing
  2133. * commands to the ATA channel. We wait for BSY to clear, then
  2134. * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
  2135. * result. Determine what devices, if any, are on the channel
  2136. * by looking at the device 0/1 error register. Look at the signature
  2137. * stored in each device's taskfile registers, to determine if
  2138. * the device is ATA or ATAPI.
  2139. *
  2140. * LOCKING:
  2141. * PCI/etc. bus probe sem.
  2142. * Obtains host lock.
  2143. *
  2144. * SIDE EFFECTS:
  2145. * Sets ATA_FLAG_DISABLED if bus reset fails.
  2146. */
  2147. void ata_bus_reset(struct ata_port *ap)
  2148. {
  2149. struct ata_ioports *ioaddr = &ap->ioaddr;
  2150. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  2151. u8 err;
  2152. unsigned int dev0, dev1 = 0, devmask = 0;
  2153. DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
  2154. /* determine if device 0/1 are present */
  2155. if (ap->flags & ATA_FLAG_SATA_RESET)
  2156. dev0 = 1;
  2157. else {
  2158. dev0 = ata_devchk(ap, 0);
  2159. if (slave_possible)
  2160. dev1 = ata_devchk(ap, 1);
  2161. }
  2162. if (dev0)
  2163. devmask |= (1 << 0);
  2164. if (dev1)
  2165. devmask |= (1 << 1);
  2166. /* select device 0 again */
  2167. ap->ops->dev_select(ap, 0);
  2168. /* issue bus reset */
  2169. if (ap->flags & ATA_FLAG_SRST)
  2170. if (ata_bus_softreset(ap, devmask))
  2171. goto err_out;
  2172. /*
  2173. * determine by signature whether we have ATA or ATAPI devices
  2174. */
  2175. ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
  2176. if ((slave_possible) && (err != 0x81))
  2177. ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
  2178. /* re-enable interrupts */
  2179. if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
  2180. ata_irq_on(ap);
  2181. /* is double-select really necessary? */
  2182. if (ap->device[1].class != ATA_DEV_NONE)
  2183. ap->ops->dev_select(ap, 1);
  2184. if (ap->device[0].class != ATA_DEV_NONE)
  2185. ap->ops->dev_select(ap, 0);
  2186. /* if no devices were detected, disable this port */
  2187. if ((ap->device[0].class == ATA_DEV_NONE) &&
  2188. (ap->device[1].class == ATA_DEV_NONE))
  2189. goto err_out;
  2190. if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
  2191. /* set up device control for ATA_FLAG_SATA_RESET */
  2192. if (ap->flags & ATA_FLAG_MMIO)
  2193. writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
  2194. else
  2195. outb(ap->ctl, ioaddr->ctl_addr);
  2196. }
  2197. DPRINTK("EXIT\n");
  2198. return;
  2199. err_out:
  2200. ata_port_printk(ap, KERN_ERR, "disabling port\n");
  2201. ap->ops->port_disable(ap);
  2202. DPRINTK("EXIT\n");
  2203. }
  2204. /**
  2205. * sata_phy_debounce - debounce SATA phy status
  2206. * @ap: ATA port to debounce SATA phy status for
  2207. * @params: timing parameters { interval, duratinon, timeout } in msec
  2208. *
  2209. * Make sure SStatus of @ap reaches stable state, determined by
  2210. * holding the same value where DET is not 1 for @duration polled
  2211. * every @interval, before @timeout. Timeout constraints the
  2212. * beginning of the stable state. Because, after hot unplugging,
  2213. * DET gets stuck at 1 on some controllers, this functions waits
  2214. * until timeout then returns 0 if DET is stable at 1.
  2215. *
  2216. * LOCKING:
  2217. * Kernel thread context (may sleep)
  2218. *
  2219. * RETURNS:
  2220. * 0 on success, -errno on failure.
  2221. */
  2222. int sata_phy_debounce(struct ata_port *ap, const unsigned long *params)
  2223. {
  2224. unsigned long interval_msec = params[0];
  2225. unsigned long duration = params[1] * HZ / 1000;
  2226. unsigned long timeout = jiffies + params[2] * HZ / 1000;
  2227. unsigned long last_jiffies;
  2228. u32 last, cur;
  2229. int rc;
  2230. if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
  2231. return rc;
  2232. cur &= 0xf;
  2233. last = cur;
  2234. last_jiffies = jiffies;
  2235. while (1) {
  2236. msleep(interval_msec);
  2237. if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
  2238. return rc;
  2239. cur &= 0xf;
  2240. /* DET stable? */
  2241. if (cur == last) {
  2242. if (cur == 1 && time_before(jiffies, timeout))
  2243. continue;
  2244. if (time_after(jiffies, last_jiffies + duration))
  2245. return 0;
  2246. continue;
  2247. }
  2248. /* unstable, start over */
  2249. last = cur;
  2250. last_jiffies = jiffies;
  2251. /* check timeout */
  2252. if (time_after(jiffies, timeout))
  2253. return -EBUSY;
  2254. }
  2255. }
  2256. /**
  2257. * sata_phy_resume - resume SATA phy
  2258. * @ap: ATA port to resume SATA phy for
  2259. * @params: timing parameters { interval, duratinon, timeout } in msec
  2260. *
  2261. * Resume SATA phy of @ap and debounce it.
  2262. *
  2263. * LOCKING:
  2264. * Kernel thread context (may sleep)
  2265. *
  2266. * RETURNS:
  2267. * 0 on success, -errno on failure.
  2268. */
  2269. int sata_phy_resume(struct ata_port *ap, const unsigned long *params)
  2270. {
  2271. u32 scontrol;
  2272. int rc;
  2273. if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
  2274. return rc;
  2275. scontrol = (scontrol & 0x0f0) | 0x300;
  2276. if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
  2277. return rc;
  2278. /* Some PHYs react badly if SStatus is pounded immediately
  2279. * after resuming. Delay 200ms before debouncing.
  2280. */
  2281. msleep(200);
  2282. return sata_phy_debounce(ap, params);
  2283. }
  2284. static void ata_wait_spinup(struct ata_port *ap)
  2285. {
  2286. struct ata_eh_context *ehc = &ap->eh_context;
  2287. unsigned long end, secs;
  2288. int rc;
  2289. /* first, debounce phy if SATA */
  2290. if (ap->cbl == ATA_CBL_SATA) {
  2291. rc = sata_phy_debounce(ap, sata_deb_timing_hotplug);
  2292. /* if debounced successfully and offline, no need to wait */
  2293. if ((rc == 0 || rc == -EOPNOTSUPP) && ata_port_offline(ap))
  2294. return;
  2295. }
  2296. /* okay, let's give the drive time to spin up */
  2297. end = ehc->i.hotplug_timestamp + ATA_SPINUP_WAIT * HZ / 1000;
  2298. secs = ((end - jiffies) + HZ - 1) / HZ;
  2299. if (time_after(jiffies, end))
  2300. return;
  2301. if (secs > 5)
  2302. ata_port_printk(ap, KERN_INFO, "waiting for device to spin up "
  2303. "(%lu secs)\n", secs);
  2304. schedule_timeout_uninterruptible(end - jiffies);
  2305. }
  2306. /**
  2307. * ata_std_prereset - prepare for reset
  2308. * @ap: ATA port to be reset
  2309. *
  2310. * @ap is about to be reset. Initialize it.
  2311. *
  2312. * LOCKING:
  2313. * Kernel thread context (may sleep)
  2314. *
  2315. * RETURNS:
  2316. * 0 on success, -errno otherwise.
  2317. */
  2318. int ata_std_prereset(struct ata_port *ap)
  2319. {
  2320. struct ata_eh_context *ehc = &ap->eh_context;
  2321. const unsigned long *timing = sata_ehc_deb_timing(ehc);
  2322. int rc;
  2323. /* handle link resume & hotplug spinup */
  2324. if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
  2325. (ap->flags & ATA_FLAG_HRST_TO_RESUME))
  2326. ehc->i.action |= ATA_EH_HARDRESET;
  2327. if ((ehc->i.flags & ATA_EHI_HOTPLUGGED) &&
  2328. (ap->flags & ATA_FLAG_SKIP_D2H_BSY))
  2329. ata_wait_spinup(ap);
  2330. /* if we're about to do hardreset, nothing more to do */
  2331. if (ehc->i.action & ATA_EH_HARDRESET)
  2332. return 0;
  2333. /* if SATA, resume phy */
  2334. if (ap->cbl == ATA_CBL_SATA) {
  2335. rc = sata_phy_resume(ap, timing);
  2336. if (rc && rc != -EOPNOTSUPP) {
  2337. /* phy resume failed */
  2338. ata_port_printk(ap, KERN_WARNING, "failed to resume "
  2339. "link for reset (errno=%d)\n", rc);
  2340. return rc;
  2341. }
  2342. }
  2343. /* Wait for !BSY if the controller can wait for the first D2H
  2344. * Reg FIS and we don't know that no device is attached.
  2345. */
  2346. if (!(ap->flags & ATA_FLAG_SKIP_D2H_BSY) && !ata_port_offline(ap))
  2347. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  2348. return 0;
  2349. }
  2350. /**
  2351. * ata_std_softreset - reset host port via ATA SRST
  2352. * @ap: port to reset
  2353. * @classes: resulting classes of attached devices
  2354. *
  2355. * Reset host port using ATA SRST.
  2356. *
  2357. * LOCKING:
  2358. * Kernel thread context (may sleep)
  2359. *
  2360. * RETURNS:
  2361. * 0 on success, -errno otherwise.
  2362. */
  2363. int ata_std_softreset(struct ata_port *ap, unsigned int *classes)
  2364. {
  2365. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  2366. unsigned int devmask = 0, err_mask;
  2367. u8 err;
  2368. DPRINTK("ENTER\n");
  2369. if (ata_port_offline(ap)) {
  2370. classes[0] = ATA_DEV_NONE;
  2371. goto out;
  2372. }
  2373. /* determine if device 0/1 are present */
  2374. if (ata_devchk(ap, 0))
  2375. devmask |= (1 << 0);
  2376. if (slave_possible && ata_devchk(ap, 1))
  2377. devmask |= (1 << 1);
  2378. /* select device 0 again */
  2379. ap->ops->dev_select(ap, 0);
  2380. /* issue bus reset */
  2381. DPRINTK("about to softreset, devmask=%x\n", devmask);
  2382. err_mask = ata_bus_softreset(ap, devmask);
  2383. if (err_mask) {
  2384. ata_port_printk(ap, KERN_ERR, "SRST failed (err_mask=0x%x)\n",
  2385. err_mask);
  2386. return -EIO;
  2387. }
  2388. /* determine by signature whether we have ATA or ATAPI devices */
  2389. classes[0] = ata_dev_try_classify(ap, 0, &err);
  2390. if (slave_possible && err != 0x81)
  2391. classes[1] = ata_dev_try_classify(ap, 1, &err);
  2392. out:
  2393. DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
  2394. return 0;
  2395. }
  2396. /**
  2397. * sata_port_hardreset - reset port via SATA phy reset
  2398. * @ap: port to reset
  2399. * @timing: timing parameters { interval, duratinon, timeout } in msec
  2400. *
  2401. * SATA phy-reset host port using DET bits of SControl register.
  2402. *
  2403. * LOCKING:
  2404. * Kernel thread context (may sleep)
  2405. *
  2406. * RETURNS:
  2407. * 0 on success, -errno otherwise.
  2408. */
  2409. int sata_port_hardreset(struct ata_port *ap, const unsigned long *timing)
  2410. {
  2411. u32 scontrol;
  2412. int rc;
  2413. DPRINTK("ENTER\n");
  2414. if (sata_set_spd_needed(ap)) {
  2415. /* SATA spec says nothing about how to reconfigure
  2416. * spd. To be on the safe side, turn off phy during
  2417. * reconfiguration. This works for at least ICH7 AHCI
  2418. * and Sil3124.
  2419. */
  2420. if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
  2421. goto out;
  2422. scontrol = (scontrol & 0x0f0) | 0x304;
  2423. if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
  2424. goto out;
  2425. sata_set_spd(ap);
  2426. }
  2427. /* issue phy wake/reset */
  2428. if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
  2429. goto out;
  2430. scontrol = (scontrol & 0x0f0) | 0x301;
  2431. if ((rc = sata_scr_write_flush(ap, SCR_CONTROL, scontrol)))
  2432. goto out;
  2433. /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
  2434. * 10.4.2 says at least 1 ms.
  2435. */
  2436. msleep(1);
  2437. /* bring phy back */
  2438. rc = sata_phy_resume(ap, timing);
  2439. out:
  2440. DPRINTK("EXIT, rc=%d\n", rc);
  2441. return rc;
  2442. }
  2443. /**
  2444. * sata_std_hardreset - reset host port via SATA phy reset
  2445. * @ap: port to reset
  2446. * @class: resulting class of attached device
  2447. *
  2448. * SATA phy-reset host port using DET bits of SControl register,
  2449. * wait for !BSY and classify the attached device.
  2450. *
  2451. * LOCKING:
  2452. * Kernel thread context (may sleep)
  2453. *
  2454. * RETURNS:
  2455. * 0 on success, -errno otherwise.
  2456. */
  2457. int sata_std_hardreset(struct ata_port *ap, unsigned int *class)
  2458. {
  2459. const unsigned long *timing = sata_ehc_deb_timing(&ap->eh_context);
  2460. int rc;
  2461. DPRINTK("ENTER\n");
  2462. /* do hardreset */
  2463. rc = sata_port_hardreset(ap, timing);
  2464. if (rc) {
  2465. ata_port_printk(ap, KERN_ERR,
  2466. "COMRESET failed (errno=%d)\n", rc);
  2467. return rc;
  2468. }
  2469. /* TODO: phy layer with polling, timeouts, etc. */
  2470. if (ata_port_offline(ap)) {
  2471. *class = ATA_DEV_NONE;
  2472. DPRINTK("EXIT, link offline\n");
  2473. return 0;
  2474. }
  2475. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  2476. ata_port_printk(ap, KERN_ERR,
  2477. "COMRESET failed (device not ready)\n");
  2478. return -EIO;
  2479. }
  2480. ap->ops->dev_select(ap, 0); /* probably unnecessary */
  2481. *class = ata_dev_try_classify(ap, 0, NULL);
  2482. DPRINTK("EXIT, class=%u\n", *class);
  2483. return 0;
  2484. }
  2485. /**
  2486. * ata_std_postreset - standard postreset callback
  2487. * @ap: the target ata_port
  2488. * @classes: classes of attached devices
  2489. *
  2490. * This function is invoked after a successful reset. Note that
  2491. * the device might have been reset more than once using
  2492. * different reset methods before postreset is invoked.
  2493. *
  2494. * LOCKING:
  2495. * Kernel thread context (may sleep)
  2496. */
  2497. void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
  2498. {
  2499. u32 serror;
  2500. DPRINTK("ENTER\n");
  2501. /* print link status */
  2502. sata_print_link_status(ap);
  2503. /* clear SError */
  2504. if (sata_scr_read(ap, SCR_ERROR, &serror) == 0)
  2505. sata_scr_write(ap, SCR_ERROR, serror);
  2506. /* re-enable interrupts */
  2507. if (!ap->ops->error_handler) {
  2508. /* FIXME: hack. create a hook instead */
  2509. if (ap->ioaddr.ctl_addr)
  2510. ata_irq_on(ap);
  2511. }
  2512. /* is double-select really necessary? */
  2513. if (classes[0] != ATA_DEV_NONE)
  2514. ap->ops->dev_select(ap, 1);
  2515. if (classes[1] != ATA_DEV_NONE)
  2516. ap->ops->dev_select(ap, 0);
  2517. /* bail out if no device is present */
  2518. if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
  2519. DPRINTK("EXIT, no device\n");
  2520. return;
  2521. }
  2522. /* set up device control */
  2523. if (ap->ioaddr.ctl_addr) {
  2524. if (ap->flags & ATA_FLAG_MMIO)
  2525. writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
  2526. else
  2527. outb(ap->ctl, ap->ioaddr.ctl_addr);
  2528. }
  2529. DPRINTK("EXIT\n");
  2530. }
  2531. /**
  2532. * ata_dev_same_device - Determine whether new ID matches configured device
  2533. * @dev: device to compare against
  2534. * @new_class: class of the new device
  2535. * @new_id: IDENTIFY page of the new device
  2536. *
  2537. * Compare @new_class and @new_id against @dev and determine
  2538. * whether @dev is the device indicated by @new_class and
  2539. * @new_id.
  2540. *
  2541. * LOCKING:
  2542. * None.
  2543. *
  2544. * RETURNS:
  2545. * 1 if @dev matches @new_class and @new_id, 0 otherwise.
  2546. */
  2547. static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
  2548. const u16 *new_id)
  2549. {
  2550. const u16 *old_id = dev->id;
  2551. unsigned char model[2][41], serial[2][21];
  2552. u64 new_n_sectors;
  2553. if (dev->class != new_class) {
  2554. ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
  2555. dev->class, new_class);
  2556. return 0;
  2557. }
  2558. ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
  2559. ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
  2560. ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
  2561. ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
  2562. new_n_sectors = ata_id_n_sectors(new_id);
  2563. if (strcmp(model[0], model[1])) {
  2564. ata_dev_printk(dev, KERN_INFO, "model number mismatch "
  2565. "'%s' != '%s'\n", model[0], model[1]);
  2566. return 0;
  2567. }
  2568. if (strcmp(serial[0], serial[1])) {
  2569. ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
  2570. "'%s' != '%s'\n", serial[0], serial[1]);
  2571. return 0;
  2572. }
  2573. if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
  2574. ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
  2575. "%llu != %llu\n",
  2576. (unsigned long long)dev->n_sectors,
  2577. (unsigned long long)new_n_sectors);
  2578. return 0;
  2579. }
  2580. return 1;
  2581. }
  2582. /**
  2583. * ata_dev_revalidate - Revalidate ATA device
  2584. * @dev: device to revalidate
  2585. * @readid_flags: read ID flags
  2586. *
  2587. * Re-read IDENTIFY page and make sure @dev is still attached to
  2588. * the port.
  2589. *
  2590. * LOCKING:
  2591. * Kernel thread context (may sleep)
  2592. *
  2593. * RETURNS:
  2594. * 0 on success, negative errno otherwise
  2595. */
  2596. int ata_dev_revalidate(struct ata_device *dev, unsigned int readid_flags)
  2597. {
  2598. unsigned int class = dev->class;
  2599. u16 *id = (void *)dev->ap->sector_buf;
  2600. int rc;
  2601. if (!ata_dev_enabled(dev)) {
  2602. rc = -ENODEV;
  2603. goto fail;
  2604. }
  2605. /* read ID data */
  2606. rc = ata_dev_read_id(dev, &class, readid_flags, id);
  2607. if (rc)
  2608. goto fail;
  2609. /* is the device still there? */
  2610. if (!ata_dev_same_device(dev, class, id)) {
  2611. rc = -ENODEV;
  2612. goto fail;
  2613. }
  2614. memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
  2615. /* configure device according to the new ID */
  2616. rc = ata_dev_configure(dev);
  2617. if (rc == 0)
  2618. return 0;
  2619. fail:
  2620. ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
  2621. return rc;
  2622. }
  2623. struct ata_blacklist_entry {
  2624. const char *model_num;
  2625. const char *model_rev;
  2626. unsigned long horkage;
  2627. };
  2628. static const struct ata_blacklist_entry ata_device_blacklist [] = {
  2629. /* Devices with DMA related problems under Linux */
  2630. { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
  2631. { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
  2632. { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
  2633. { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
  2634. { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
  2635. { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
  2636. { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
  2637. { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
  2638. { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
  2639. { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
  2640. { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
  2641. { "CRD-84", NULL, ATA_HORKAGE_NODMA },
  2642. { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
  2643. { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
  2644. { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
  2645. { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
  2646. { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
  2647. { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
  2648. { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
  2649. { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
  2650. { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
  2651. { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
  2652. { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
  2653. { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
  2654. { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
  2655. { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
  2656. { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
  2657. { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
  2658. { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
  2659. { "SAMSUNG CD-ROM SN-124","N001", ATA_HORKAGE_NODMA },
  2660. /* Devices we expect to fail diagnostics */
  2661. /* Devices where NCQ should be avoided */
  2662. /* NCQ is slow */
  2663. { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
  2664. /* Devices with NCQ limits */
  2665. /* End Marker */
  2666. { }
  2667. };
  2668. static int ata_strim(char *s, size_t len)
  2669. {
  2670. len = strnlen(s, len);
  2671. /* ATAPI specifies that empty space is blank-filled; remove blanks */
  2672. while ((len > 0) && (s[len - 1] == ' ')) {
  2673. len--;
  2674. s[len] = 0;
  2675. }
  2676. return len;
  2677. }
  2678. unsigned long ata_device_blacklisted(const struct ata_device *dev)
  2679. {
  2680. unsigned char model_num[40];
  2681. unsigned char model_rev[16];
  2682. unsigned int nlen, rlen;
  2683. const struct ata_blacklist_entry *ad = ata_device_blacklist;
  2684. ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
  2685. sizeof(model_num));
  2686. ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS,
  2687. sizeof(model_rev));
  2688. nlen = ata_strim(model_num, sizeof(model_num));
  2689. rlen = ata_strim(model_rev, sizeof(model_rev));
  2690. while (ad->model_num) {
  2691. if (!strncmp(ad->model_num, model_num, nlen)) {
  2692. if (ad->model_rev == NULL)
  2693. return ad->horkage;
  2694. if (!strncmp(ad->model_rev, model_rev, rlen))
  2695. return ad->horkage;
  2696. }
  2697. ad++;
  2698. }
  2699. return 0;
  2700. }
  2701. static int ata_dma_blacklisted(const struct ata_device *dev)
  2702. {
  2703. /* We don't support polling DMA.
  2704. * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
  2705. * if the LLDD handles only interrupts in the HSM_ST_LAST state.
  2706. */
  2707. if ((dev->ap->flags & ATA_FLAG_PIO_POLLING) &&
  2708. (dev->flags & ATA_DFLAG_CDB_INTR))
  2709. return 1;
  2710. return (ata_device_blacklisted(dev) & ATA_HORKAGE_NODMA) ? 1 : 0;
  2711. }
  2712. /**
  2713. * ata_dev_xfermask - Compute supported xfermask of the given device
  2714. * @dev: Device to compute xfermask for
  2715. *
  2716. * Compute supported xfermask of @dev and store it in
  2717. * dev->*_mask. This function is responsible for applying all
  2718. * known limits including host controller limits, device
  2719. * blacklist, etc...
  2720. *
  2721. * LOCKING:
  2722. * None.
  2723. */
  2724. static void ata_dev_xfermask(struct ata_device *dev)
  2725. {
  2726. struct ata_port *ap = dev->ap;
  2727. struct ata_host *host = ap->host;
  2728. unsigned long xfer_mask;
  2729. /* controller modes available */
  2730. xfer_mask = ata_pack_xfermask(ap->pio_mask,
  2731. ap->mwdma_mask, ap->udma_mask);
  2732. /* Apply cable rule here. Don't apply it early because when
  2733. * we handle hot plug the cable type can itself change.
  2734. */
  2735. if (ap->cbl == ATA_CBL_PATA40)
  2736. xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
  2737. /* Apply drive side cable rule. Unknown or 80 pin cables reported
  2738. * host side are checked drive side as well. Cases where we know a
  2739. * 40wire cable is used safely for 80 are not checked here.
  2740. */
  2741. if (ata_drive_40wire(dev->id) && (ap->cbl == ATA_CBL_PATA_UNK || ap->cbl == ATA_CBL_PATA80))
  2742. xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
  2743. xfer_mask &= ata_pack_xfermask(dev->pio_mask,
  2744. dev->mwdma_mask, dev->udma_mask);
  2745. xfer_mask &= ata_id_xfermask(dev->id);
  2746. /*
  2747. * CFA Advanced TrueIDE timings are not allowed on a shared
  2748. * cable
  2749. */
  2750. if (ata_dev_pair(dev)) {
  2751. /* No PIO5 or PIO6 */
  2752. xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
  2753. /* No MWDMA3 or MWDMA 4 */
  2754. xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
  2755. }
  2756. if (ata_dma_blacklisted(dev)) {
  2757. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  2758. ata_dev_printk(dev, KERN_WARNING,
  2759. "device is on DMA blacklist, disabling DMA\n");
  2760. }
  2761. if ((host->flags & ATA_HOST_SIMPLEX) && host->simplex_claimed) {
  2762. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  2763. ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
  2764. "other device, disabling DMA\n");
  2765. }
  2766. if (ap->ops->mode_filter)
  2767. xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask);
  2768. ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
  2769. &dev->mwdma_mask, &dev->udma_mask);
  2770. }
  2771. /**
  2772. * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
  2773. * @dev: Device to which command will be sent
  2774. *
  2775. * Issue SET FEATURES - XFER MODE command to device @dev
  2776. * on port @ap.
  2777. *
  2778. * LOCKING:
  2779. * PCI/etc. bus probe sem.
  2780. *
  2781. * RETURNS:
  2782. * 0 on success, AC_ERR_* mask otherwise.
  2783. */
  2784. static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
  2785. {
  2786. struct ata_taskfile tf;
  2787. unsigned int err_mask;
  2788. /* set up set-features taskfile */
  2789. DPRINTK("set features - xfer mode\n");
  2790. ata_tf_init(dev, &tf);
  2791. tf.command = ATA_CMD_SET_FEATURES;
  2792. tf.feature = SETFEATURES_XFER;
  2793. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2794. tf.protocol = ATA_PROT_NODATA;
  2795. tf.nsect = dev->xfer_mode;
  2796. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
  2797. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  2798. return err_mask;
  2799. }
  2800. /**
  2801. * ata_dev_init_params - Issue INIT DEV PARAMS command
  2802. * @dev: Device to which command will be sent
  2803. * @heads: Number of heads (taskfile parameter)
  2804. * @sectors: Number of sectors (taskfile parameter)
  2805. *
  2806. * LOCKING:
  2807. * Kernel thread context (may sleep)
  2808. *
  2809. * RETURNS:
  2810. * 0 on success, AC_ERR_* mask otherwise.
  2811. */
  2812. static unsigned int ata_dev_init_params(struct ata_device *dev,
  2813. u16 heads, u16 sectors)
  2814. {
  2815. struct ata_taskfile tf;
  2816. unsigned int err_mask;
  2817. /* Number of sectors per track 1-255. Number of heads 1-16 */
  2818. if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
  2819. return AC_ERR_INVALID;
  2820. /* set up init dev params taskfile */
  2821. DPRINTK("init dev params \n");
  2822. ata_tf_init(dev, &tf);
  2823. tf.command = ATA_CMD_INIT_DEV_PARAMS;
  2824. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2825. tf.protocol = ATA_PROT_NODATA;
  2826. tf.nsect = sectors;
  2827. tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
  2828. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
  2829. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  2830. return err_mask;
  2831. }
  2832. /**
  2833. * ata_sg_clean - Unmap DMA memory associated with command
  2834. * @qc: Command containing DMA memory to be released
  2835. *
  2836. * Unmap all mapped DMA memory associated with this command.
  2837. *
  2838. * LOCKING:
  2839. * spin_lock_irqsave(host lock)
  2840. */
  2841. static void ata_sg_clean(struct ata_queued_cmd *qc)
  2842. {
  2843. struct ata_port *ap = qc->ap;
  2844. struct scatterlist *sg = qc->__sg;
  2845. int dir = qc->dma_dir;
  2846. void *pad_buf = NULL;
  2847. WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
  2848. WARN_ON(sg == NULL);
  2849. if (qc->flags & ATA_QCFLAG_SINGLE)
  2850. WARN_ON(qc->n_elem > 1);
  2851. VPRINTK("unmapping %u sg elements\n", qc->n_elem);
  2852. /* if we padded the buffer out to 32-bit bound, and data
  2853. * xfer direction is from-device, we must copy from the
  2854. * pad buffer back into the supplied buffer
  2855. */
  2856. if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
  2857. pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  2858. if (qc->flags & ATA_QCFLAG_SG) {
  2859. if (qc->n_elem)
  2860. dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
  2861. /* restore last sg */
  2862. sg[qc->orig_n_elem - 1].length += qc->pad_len;
  2863. if (pad_buf) {
  2864. struct scatterlist *psg = &qc->pad_sgent;
  2865. void *addr = kmap_atomic(psg->page, KM_IRQ0);
  2866. memcpy(addr + psg->offset, pad_buf, qc->pad_len);
  2867. kunmap_atomic(addr, KM_IRQ0);
  2868. }
  2869. } else {
  2870. if (qc->n_elem)
  2871. dma_unmap_single(ap->dev,
  2872. sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
  2873. dir);
  2874. /* restore sg */
  2875. sg->length += qc->pad_len;
  2876. if (pad_buf)
  2877. memcpy(qc->buf_virt + sg->length - qc->pad_len,
  2878. pad_buf, qc->pad_len);
  2879. }
  2880. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  2881. qc->__sg = NULL;
  2882. }
  2883. /**
  2884. * ata_fill_sg - Fill PCI IDE PRD table
  2885. * @qc: Metadata associated with taskfile to be transferred
  2886. *
  2887. * Fill PCI IDE PRD (scatter-gather) table with segments
  2888. * associated with the current disk command.
  2889. *
  2890. * LOCKING:
  2891. * spin_lock_irqsave(host lock)
  2892. *
  2893. */
  2894. static void ata_fill_sg(struct ata_queued_cmd *qc)
  2895. {
  2896. struct ata_port *ap = qc->ap;
  2897. struct scatterlist *sg;
  2898. unsigned int idx;
  2899. WARN_ON(qc->__sg == NULL);
  2900. WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
  2901. idx = 0;
  2902. ata_for_each_sg(sg, qc) {
  2903. u32 addr, offset;
  2904. u32 sg_len, len;
  2905. /* determine if physical DMA addr spans 64K boundary.
  2906. * Note h/w doesn't support 64-bit, so we unconditionally
  2907. * truncate dma_addr_t to u32.
  2908. */
  2909. addr = (u32) sg_dma_address(sg);
  2910. sg_len = sg_dma_len(sg);
  2911. while (sg_len) {
  2912. offset = addr & 0xffff;
  2913. len = sg_len;
  2914. if ((offset + sg_len) > 0x10000)
  2915. len = 0x10000 - offset;
  2916. ap->prd[idx].addr = cpu_to_le32(addr);
  2917. ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
  2918. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
  2919. idx++;
  2920. sg_len -= len;
  2921. addr += len;
  2922. }
  2923. }
  2924. if (idx)
  2925. ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  2926. }
  2927. /**
  2928. * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
  2929. * @qc: Metadata associated with taskfile to check
  2930. *
  2931. * Allow low-level driver to filter ATA PACKET commands, returning
  2932. * a status indicating whether or not it is OK to use DMA for the
  2933. * supplied PACKET command.
  2934. *
  2935. * LOCKING:
  2936. * spin_lock_irqsave(host lock)
  2937. *
  2938. * RETURNS: 0 when ATAPI DMA can be used
  2939. * nonzero otherwise
  2940. */
  2941. int ata_check_atapi_dma(struct ata_queued_cmd *qc)
  2942. {
  2943. struct ata_port *ap = qc->ap;
  2944. int rc = 0; /* Assume ATAPI DMA is OK by default */
  2945. if (ap->ops->check_atapi_dma)
  2946. rc = ap->ops->check_atapi_dma(qc);
  2947. return rc;
  2948. }
  2949. /**
  2950. * ata_qc_prep - Prepare taskfile for submission
  2951. * @qc: Metadata associated with taskfile to be prepared
  2952. *
  2953. * Prepare ATA taskfile for submission.
  2954. *
  2955. * LOCKING:
  2956. * spin_lock_irqsave(host lock)
  2957. */
  2958. void ata_qc_prep(struct ata_queued_cmd *qc)
  2959. {
  2960. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  2961. return;
  2962. ata_fill_sg(qc);
  2963. }
  2964. void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
  2965. /**
  2966. * ata_sg_init_one - Associate command with memory buffer
  2967. * @qc: Command to be associated
  2968. * @buf: Memory buffer
  2969. * @buflen: Length of memory buffer, in bytes.
  2970. *
  2971. * Initialize the data-related elements of queued_cmd @qc
  2972. * to point to a single memory buffer, @buf of byte length @buflen.
  2973. *
  2974. * LOCKING:
  2975. * spin_lock_irqsave(host lock)
  2976. */
  2977. void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
  2978. {
  2979. struct scatterlist *sg;
  2980. qc->flags |= ATA_QCFLAG_SINGLE;
  2981. memset(&qc->sgent, 0, sizeof(qc->sgent));
  2982. qc->__sg = &qc->sgent;
  2983. qc->n_elem = 1;
  2984. qc->orig_n_elem = 1;
  2985. qc->buf_virt = buf;
  2986. qc->nbytes = buflen;
  2987. sg = qc->__sg;
  2988. sg_init_one(sg, buf, buflen);
  2989. }
  2990. /**
  2991. * ata_sg_init - Associate command with scatter-gather table.
  2992. * @qc: Command to be associated
  2993. * @sg: Scatter-gather table.
  2994. * @n_elem: Number of elements in s/g table.
  2995. *
  2996. * Initialize the data-related elements of queued_cmd @qc
  2997. * to point to a scatter-gather table @sg, containing @n_elem
  2998. * elements.
  2999. *
  3000. * LOCKING:
  3001. * spin_lock_irqsave(host lock)
  3002. */
  3003. void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
  3004. unsigned int n_elem)
  3005. {
  3006. qc->flags |= ATA_QCFLAG_SG;
  3007. qc->__sg = sg;
  3008. qc->n_elem = n_elem;
  3009. qc->orig_n_elem = n_elem;
  3010. }
  3011. /**
  3012. * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
  3013. * @qc: Command with memory buffer to be mapped.
  3014. *
  3015. * DMA-map the memory buffer associated with queued_cmd @qc.
  3016. *
  3017. * LOCKING:
  3018. * spin_lock_irqsave(host lock)
  3019. *
  3020. * RETURNS:
  3021. * Zero on success, negative on error.
  3022. */
  3023. static int ata_sg_setup_one(struct ata_queued_cmd *qc)
  3024. {
  3025. struct ata_port *ap = qc->ap;
  3026. int dir = qc->dma_dir;
  3027. struct scatterlist *sg = qc->__sg;
  3028. dma_addr_t dma_address;
  3029. int trim_sg = 0;
  3030. /* we must lengthen transfers to end on a 32-bit boundary */
  3031. qc->pad_len = sg->length & 3;
  3032. if (qc->pad_len) {
  3033. void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  3034. struct scatterlist *psg = &qc->pad_sgent;
  3035. WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
  3036. memset(pad_buf, 0, ATA_DMA_PAD_SZ);
  3037. if (qc->tf.flags & ATA_TFLAG_WRITE)
  3038. memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
  3039. qc->pad_len);
  3040. sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
  3041. sg_dma_len(psg) = ATA_DMA_PAD_SZ;
  3042. /* trim sg */
  3043. sg->length -= qc->pad_len;
  3044. if (sg->length == 0)
  3045. trim_sg = 1;
  3046. DPRINTK("padding done, sg->length=%u pad_len=%u\n",
  3047. sg->length, qc->pad_len);
  3048. }
  3049. if (trim_sg) {
  3050. qc->n_elem--;
  3051. goto skip_map;
  3052. }
  3053. dma_address = dma_map_single(ap->dev, qc->buf_virt,
  3054. sg->length, dir);
  3055. if (dma_mapping_error(dma_address)) {
  3056. /* restore sg */
  3057. sg->length += qc->pad_len;
  3058. return -1;
  3059. }
  3060. sg_dma_address(sg) = dma_address;
  3061. sg_dma_len(sg) = sg->length;
  3062. skip_map:
  3063. DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
  3064. qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  3065. return 0;
  3066. }
  3067. /**
  3068. * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
  3069. * @qc: Command with scatter-gather table to be mapped.
  3070. *
  3071. * DMA-map the scatter-gather table associated with queued_cmd @qc.
  3072. *
  3073. * LOCKING:
  3074. * spin_lock_irqsave(host lock)
  3075. *
  3076. * RETURNS:
  3077. * Zero on success, negative on error.
  3078. *
  3079. */
  3080. static int ata_sg_setup(struct ata_queued_cmd *qc)
  3081. {
  3082. struct ata_port *ap = qc->ap;
  3083. struct scatterlist *sg = qc->__sg;
  3084. struct scatterlist *lsg = &sg[qc->n_elem - 1];
  3085. int n_elem, pre_n_elem, dir, trim_sg = 0;
  3086. VPRINTK("ENTER, ata%u\n", ap->id);
  3087. WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
  3088. /* we must lengthen transfers to end on a 32-bit boundary */
  3089. qc->pad_len = lsg->length & 3;
  3090. if (qc->pad_len) {
  3091. void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  3092. struct scatterlist *psg = &qc->pad_sgent;
  3093. unsigned int offset;
  3094. WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
  3095. memset(pad_buf, 0, ATA_DMA_PAD_SZ);
  3096. /*
  3097. * psg->page/offset are used to copy to-be-written
  3098. * data in this function or read data in ata_sg_clean.
  3099. */
  3100. offset = lsg->offset + lsg->length - qc->pad_len;
  3101. psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
  3102. psg->offset = offset_in_page(offset);
  3103. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  3104. void *addr = kmap_atomic(psg->page, KM_IRQ0);
  3105. memcpy(pad_buf, addr + psg->offset, qc->pad_len);
  3106. kunmap_atomic(addr, KM_IRQ0);
  3107. }
  3108. sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
  3109. sg_dma_len(psg) = ATA_DMA_PAD_SZ;
  3110. /* trim last sg */
  3111. lsg->length -= qc->pad_len;
  3112. if (lsg->length == 0)
  3113. trim_sg = 1;
  3114. DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
  3115. qc->n_elem - 1, lsg->length, qc->pad_len);
  3116. }
  3117. pre_n_elem = qc->n_elem;
  3118. if (trim_sg && pre_n_elem)
  3119. pre_n_elem--;
  3120. if (!pre_n_elem) {
  3121. n_elem = 0;
  3122. goto skip_map;
  3123. }
  3124. dir = qc->dma_dir;
  3125. n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
  3126. if (n_elem < 1) {
  3127. /* restore last sg */
  3128. lsg->length += qc->pad_len;
  3129. return -1;
  3130. }
  3131. DPRINTK("%d sg elements mapped\n", n_elem);
  3132. skip_map:
  3133. qc->n_elem = n_elem;
  3134. return 0;
  3135. }
  3136. /**
  3137. * swap_buf_le16 - swap halves of 16-bit words in place
  3138. * @buf: Buffer to swap
  3139. * @buf_words: Number of 16-bit words in buffer.
  3140. *
  3141. * Swap halves of 16-bit words if needed to convert from
  3142. * little-endian byte order to native cpu byte order, or
  3143. * vice-versa.
  3144. *
  3145. * LOCKING:
  3146. * Inherited from caller.
  3147. */
  3148. void swap_buf_le16(u16 *buf, unsigned int buf_words)
  3149. {
  3150. #ifdef __BIG_ENDIAN
  3151. unsigned int i;
  3152. for (i = 0; i < buf_words; i++)
  3153. buf[i] = le16_to_cpu(buf[i]);
  3154. #endif /* __BIG_ENDIAN */
  3155. }
  3156. /**
  3157. * ata_mmio_data_xfer - Transfer data by MMIO
  3158. * @adev: device for this I/O
  3159. * @buf: data buffer
  3160. * @buflen: buffer length
  3161. * @write_data: read/write
  3162. *
  3163. * Transfer data from/to the device data register by MMIO.
  3164. *
  3165. * LOCKING:
  3166. * Inherited from caller.
  3167. */
  3168. void ata_mmio_data_xfer(struct ata_device *adev, unsigned char *buf,
  3169. unsigned int buflen, int write_data)
  3170. {
  3171. struct ata_port *ap = adev->ap;
  3172. unsigned int i;
  3173. unsigned int words = buflen >> 1;
  3174. u16 *buf16 = (u16 *) buf;
  3175. void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
  3176. /* Transfer multiple of 2 bytes */
  3177. if (write_data) {
  3178. for (i = 0; i < words; i++)
  3179. writew(le16_to_cpu(buf16[i]), mmio);
  3180. } else {
  3181. for (i = 0; i < words; i++)
  3182. buf16[i] = cpu_to_le16(readw(mmio));
  3183. }
  3184. /* Transfer trailing 1 byte, if any. */
  3185. if (unlikely(buflen & 0x01)) {
  3186. u16 align_buf[1] = { 0 };
  3187. unsigned char *trailing_buf = buf + buflen - 1;
  3188. if (write_data) {
  3189. memcpy(align_buf, trailing_buf, 1);
  3190. writew(le16_to_cpu(align_buf[0]), mmio);
  3191. } else {
  3192. align_buf[0] = cpu_to_le16(readw(mmio));
  3193. memcpy(trailing_buf, align_buf, 1);
  3194. }
  3195. }
  3196. }
  3197. /**
  3198. * ata_pio_data_xfer - Transfer data by PIO
  3199. * @adev: device to target
  3200. * @buf: data buffer
  3201. * @buflen: buffer length
  3202. * @write_data: read/write
  3203. *
  3204. * Transfer data from/to the device data register by PIO.
  3205. *
  3206. * LOCKING:
  3207. * Inherited from caller.
  3208. */
  3209. void ata_pio_data_xfer(struct ata_device *adev, unsigned char *buf,
  3210. unsigned int buflen, int write_data)
  3211. {
  3212. struct ata_port *ap = adev->ap;
  3213. unsigned int words = buflen >> 1;
  3214. /* Transfer multiple of 2 bytes */
  3215. if (write_data)
  3216. outsw(ap->ioaddr.data_addr, buf, words);
  3217. else
  3218. insw(ap->ioaddr.data_addr, buf, words);
  3219. /* Transfer trailing 1 byte, if any. */
  3220. if (unlikely(buflen & 0x01)) {
  3221. u16 align_buf[1] = { 0 };
  3222. unsigned char *trailing_buf = buf + buflen - 1;
  3223. if (write_data) {
  3224. memcpy(align_buf, trailing_buf, 1);
  3225. outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
  3226. } else {
  3227. align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
  3228. memcpy(trailing_buf, align_buf, 1);
  3229. }
  3230. }
  3231. }
  3232. /**
  3233. * ata_pio_data_xfer_noirq - Transfer data by PIO
  3234. * @adev: device to target
  3235. * @buf: data buffer
  3236. * @buflen: buffer length
  3237. * @write_data: read/write
  3238. *
  3239. * Transfer data from/to the device data register by PIO. Do the
  3240. * transfer with interrupts disabled.
  3241. *
  3242. * LOCKING:
  3243. * Inherited from caller.
  3244. */
  3245. void ata_pio_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
  3246. unsigned int buflen, int write_data)
  3247. {
  3248. unsigned long flags;
  3249. local_irq_save(flags);
  3250. ata_pio_data_xfer(adev, buf, buflen, write_data);
  3251. local_irq_restore(flags);
  3252. }
  3253. /**
  3254. * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
  3255. * @qc: Command on going
  3256. *
  3257. * Transfer ATA_SECT_SIZE of data from/to the ATA device.
  3258. *
  3259. * LOCKING:
  3260. * Inherited from caller.
  3261. */
  3262. static void ata_pio_sector(struct ata_queued_cmd *qc)
  3263. {
  3264. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  3265. struct scatterlist *sg = qc->__sg;
  3266. struct ata_port *ap = qc->ap;
  3267. struct page *page;
  3268. unsigned int offset;
  3269. unsigned char *buf;
  3270. if (qc->cursect == (qc->nsect - 1))
  3271. ap->hsm_task_state = HSM_ST_LAST;
  3272. page = sg[qc->cursg].page;
  3273. offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
  3274. /* get the current page and offset */
  3275. page = nth_page(page, (offset >> PAGE_SHIFT));
  3276. offset %= PAGE_SIZE;
  3277. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  3278. if (PageHighMem(page)) {
  3279. unsigned long flags;
  3280. /* FIXME: use a bounce buffer */
  3281. local_irq_save(flags);
  3282. buf = kmap_atomic(page, KM_IRQ0);
  3283. /* do the actual data transfer */
  3284. ap->ops->data_xfer(qc->dev, buf + offset, ATA_SECT_SIZE, do_write);
  3285. kunmap_atomic(buf, KM_IRQ0);
  3286. local_irq_restore(flags);
  3287. } else {
  3288. buf = page_address(page);
  3289. ap->ops->data_xfer(qc->dev, buf + offset, ATA_SECT_SIZE, do_write);
  3290. }
  3291. qc->cursect++;
  3292. qc->cursg_ofs++;
  3293. if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
  3294. qc->cursg++;
  3295. qc->cursg_ofs = 0;
  3296. }
  3297. }
  3298. /**
  3299. * ata_pio_sectors - Transfer one or many 512-byte sectors.
  3300. * @qc: Command on going
  3301. *
  3302. * Transfer one or many ATA_SECT_SIZE of data from/to the
  3303. * ATA device for the DRQ request.
  3304. *
  3305. * LOCKING:
  3306. * Inherited from caller.
  3307. */
  3308. static void ata_pio_sectors(struct ata_queued_cmd *qc)
  3309. {
  3310. if (is_multi_taskfile(&qc->tf)) {
  3311. /* READ/WRITE MULTIPLE */
  3312. unsigned int nsect;
  3313. WARN_ON(qc->dev->multi_count == 0);
  3314. nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
  3315. while (nsect--)
  3316. ata_pio_sector(qc);
  3317. } else
  3318. ata_pio_sector(qc);
  3319. }
  3320. /**
  3321. * atapi_send_cdb - Write CDB bytes to hardware
  3322. * @ap: Port to which ATAPI device is attached.
  3323. * @qc: Taskfile currently active
  3324. *
  3325. * When device has indicated its readiness to accept
  3326. * a CDB, this function is called. Send the CDB.
  3327. *
  3328. * LOCKING:
  3329. * caller.
  3330. */
  3331. static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
  3332. {
  3333. /* send SCSI cdb */
  3334. DPRINTK("send cdb\n");
  3335. WARN_ON(qc->dev->cdb_len < 12);
  3336. ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
  3337. ata_altstatus(ap); /* flush */
  3338. switch (qc->tf.protocol) {
  3339. case ATA_PROT_ATAPI:
  3340. ap->hsm_task_state = HSM_ST;
  3341. break;
  3342. case ATA_PROT_ATAPI_NODATA:
  3343. ap->hsm_task_state = HSM_ST_LAST;
  3344. break;
  3345. case ATA_PROT_ATAPI_DMA:
  3346. ap->hsm_task_state = HSM_ST_LAST;
  3347. /* initiate bmdma */
  3348. ap->ops->bmdma_start(qc);
  3349. break;
  3350. }
  3351. }
  3352. /**
  3353. * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
  3354. * @qc: Command on going
  3355. * @bytes: number of bytes
  3356. *
  3357. * Transfer Transfer data from/to the ATAPI device.
  3358. *
  3359. * LOCKING:
  3360. * Inherited from caller.
  3361. *
  3362. */
  3363. static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
  3364. {
  3365. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  3366. struct scatterlist *sg = qc->__sg;
  3367. struct ata_port *ap = qc->ap;
  3368. struct page *page;
  3369. unsigned char *buf;
  3370. unsigned int offset, count;
  3371. if (qc->curbytes + bytes >= qc->nbytes)
  3372. ap->hsm_task_state = HSM_ST_LAST;
  3373. next_sg:
  3374. if (unlikely(qc->cursg >= qc->n_elem)) {
  3375. /*
  3376. * The end of qc->sg is reached and the device expects
  3377. * more data to transfer. In order not to overrun qc->sg
  3378. * and fulfill length specified in the byte count register,
  3379. * - for read case, discard trailing data from the device
  3380. * - for write case, padding zero data to the device
  3381. */
  3382. u16 pad_buf[1] = { 0 };
  3383. unsigned int words = bytes >> 1;
  3384. unsigned int i;
  3385. if (words) /* warning if bytes > 1 */
  3386. ata_dev_printk(qc->dev, KERN_WARNING,
  3387. "%u bytes trailing data\n", bytes);
  3388. for (i = 0; i < words; i++)
  3389. ap->ops->data_xfer(qc->dev, (unsigned char*)pad_buf, 2, do_write);
  3390. ap->hsm_task_state = HSM_ST_LAST;
  3391. return;
  3392. }
  3393. sg = &qc->__sg[qc->cursg];
  3394. page = sg->page;
  3395. offset = sg->offset + qc->cursg_ofs;
  3396. /* get the current page and offset */
  3397. page = nth_page(page, (offset >> PAGE_SHIFT));
  3398. offset %= PAGE_SIZE;
  3399. /* don't overrun current sg */
  3400. count = min(sg->length - qc->cursg_ofs, bytes);
  3401. /* don't cross page boundaries */
  3402. count = min(count, (unsigned int)PAGE_SIZE - offset);
  3403. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  3404. if (PageHighMem(page)) {
  3405. unsigned long flags;
  3406. /* FIXME: use bounce buffer */
  3407. local_irq_save(flags);
  3408. buf = kmap_atomic(page, KM_IRQ0);
  3409. /* do the actual data transfer */
  3410. ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
  3411. kunmap_atomic(buf, KM_IRQ0);
  3412. local_irq_restore(flags);
  3413. } else {
  3414. buf = page_address(page);
  3415. ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
  3416. }
  3417. bytes -= count;
  3418. qc->curbytes += count;
  3419. qc->cursg_ofs += count;
  3420. if (qc->cursg_ofs == sg->length) {
  3421. qc->cursg++;
  3422. qc->cursg_ofs = 0;
  3423. }
  3424. if (bytes)
  3425. goto next_sg;
  3426. }
  3427. /**
  3428. * atapi_pio_bytes - Transfer data from/to the ATAPI device.
  3429. * @qc: Command on going
  3430. *
  3431. * Transfer Transfer data from/to the ATAPI device.
  3432. *
  3433. * LOCKING:
  3434. * Inherited from caller.
  3435. */
  3436. static void atapi_pio_bytes(struct ata_queued_cmd *qc)
  3437. {
  3438. struct ata_port *ap = qc->ap;
  3439. struct ata_device *dev = qc->dev;
  3440. unsigned int ireason, bc_lo, bc_hi, bytes;
  3441. int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
  3442. /* Abuse qc->result_tf for temp storage of intermediate TF
  3443. * here to save some kernel stack usage.
  3444. * For normal completion, qc->result_tf is not relevant. For
  3445. * error, qc->result_tf is later overwritten by ata_qc_complete().
  3446. * So, the correctness of qc->result_tf is not affected.
  3447. */
  3448. ap->ops->tf_read(ap, &qc->result_tf);
  3449. ireason = qc->result_tf.nsect;
  3450. bc_lo = qc->result_tf.lbam;
  3451. bc_hi = qc->result_tf.lbah;
  3452. bytes = (bc_hi << 8) | bc_lo;
  3453. /* shall be cleared to zero, indicating xfer of data */
  3454. if (ireason & (1 << 0))
  3455. goto err_out;
  3456. /* make sure transfer direction matches expected */
  3457. i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
  3458. if (do_write != i_write)
  3459. goto err_out;
  3460. VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
  3461. __atapi_pio_bytes(qc, bytes);
  3462. return;
  3463. err_out:
  3464. ata_dev_printk(dev, KERN_INFO, "ATAPI check failed\n");
  3465. qc->err_mask |= AC_ERR_HSM;
  3466. ap->hsm_task_state = HSM_ST_ERR;
  3467. }
  3468. /**
  3469. * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
  3470. * @ap: the target ata_port
  3471. * @qc: qc on going
  3472. *
  3473. * RETURNS:
  3474. * 1 if ok in workqueue, 0 otherwise.
  3475. */
  3476. static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
  3477. {
  3478. if (qc->tf.flags & ATA_TFLAG_POLLING)
  3479. return 1;
  3480. if (ap->hsm_task_state == HSM_ST_FIRST) {
  3481. if (qc->tf.protocol == ATA_PROT_PIO &&
  3482. (qc->tf.flags & ATA_TFLAG_WRITE))
  3483. return 1;
  3484. if (is_atapi_taskfile(&qc->tf) &&
  3485. !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  3486. return 1;
  3487. }
  3488. return 0;
  3489. }
  3490. /**
  3491. * ata_hsm_qc_complete - finish a qc running on standard HSM
  3492. * @qc: Command to complete
  3493. * @in_wq: 1 if called from workqueue, 0 otherwise
  3494. *
  3495. * Finish @qc which is running on standard HSM.
  3496. *
  3497. * LOCKING:
  3498. * If @in_wq is zero, spin_lock_irqsave(host lock).
  3499. * Otherwise, none on entry and grabs host lock.
  3500. */
  3501. static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
  3502. {
  3503. struct ata_port *ap = qc->ap;
  3504. unsigned long flags;
  3505. if (ap->ops->error_handler) {
  3506. if (in_wq) {
  3507. spin_lock_irqsave(ap->lock, flags);
  3508. /* EH might have kicked in while host lock is
  3509. * released.
  3510. */
  3511. qc = ata_qc_from_tag(ap, qc->tag);
  3512. if (qc) {
  3513. if (likely(!(qc->err_mask & AC_ERR_HSM))) {
  3514. ata_irq_on(ap);
  3515. ata_qc_complete(qc);
  3516. } else
  3517. ata_port_freeze(ap);
  3518. }
  3519. spin_unlock_irqrestore(ap->lock, flags);
  3520. } else {
  3521. if (likely(!(qc->err_mask & AC_ERR_HSM)))
  3522. ata_qc_complete(qc);
  3523. else
  3524. ata_port_freeze(ap);
  3525. }
  3526. } else {
  3527. if (in_wq) {
  3528. spin_lock_irqsave(ap->lock, flags);
  3529. ata_irq_on(ap);
  3530. ata_qc_complete(qc);
  3531. spin_unlock_irqrestore(ap->lock, flags);
  3532. } else
  3533. ata_qc_complete(qc);
  3534. }
  3535. ata_altstatus(ap); /* flush */
  3536. }
  3537. /**
  3538. * ata_hsm_move - move the HSM to the next state.
  3539. * @ap: the target ata_port
  3540. * @qc: qc on going
  3541. * @status: current device status
  3542. * @in_wq: 1 if called from workqueue, 0 otherwise
  3543. *
  3544. * RETURNS:
  3545. * 1 when poll next status needed, 0 otherwise.
  3546. */
  3547. int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
  3548. u8 status, int in_wq)
  3549. {
  3550. unsigned long flags = 0;
  3551. int poll_next;
  3552. WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
  3553. /* Make sure ata_qc_issue_prot() does not throw things
  3554. * like DMA polling into the workqueue. Notice that
  3555. * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
  3556. */
  3557. WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
  3558. fsm_start:
  3559. DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
  3560. ap->id, qc->tf.protocol, ap->hsm_task_state, status);
  3561. switch (ap->hsm_task_state) {
  3562. case HSM_ST_FIRST:
  3563. /* Send first data block or PACKET CDB */
  3564. /* If polling, we will stay in the work queue after
  3565. * sending the data. Otherwise, interrupt handler
  3566. * takes over after sending the data.
  3567. */
  3568. poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
  3569. /* check device status */
  3570. if (unlikely((status & ATA_DRQ) == 0)) {
  3571. /* handle BSY=0, DRQ=0 as error */
  3572. if (likely(status & (ATA_ERR | ATA_DF)))
  3573. /* device stops HSM for abort/error */
  3574. qc->err_mask |= AC_ERR_DEV;
  3575. else
  3576. /* HSM violation. Let EH handle this */
  3577. qc->err_mask |= AC_ERR_HSM;
  3578. ap->hsm_task_state = HSM_ST_ERR;
  3579. goto fsm_start;
  3580. }
  3581. /* Device should not ask for data transfer (DRQ=1)
  3582. * when it finds something wrong.
  3583. * We ignore DRQ here and stop the HSM by
  3584. * changing hsm_task_state to HSM_ST_ERR and
  3585. * let the EH abort the command or reset the device.
  3586. */
  3587. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  3588. printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
  3589. ap->id, status);
  3590. qc->err_mask |= AC_ERR_HSM;
  3591. ap->hsm_task_state = HSM_ST_ERR;
  3592. goto fsm_start;
  3593. }
  3594. /* Send the CDB (atapi) or the first data block (ata pio out).
  3595. * During the state transition, interrupt handler shouldn't
  3596. * be invoked before the data transfer is complete and
  3597. * hsm_task_state is changed. Hence, the following locking.
  3598. */
  3599. if (in_wq)
  3600. spin_lock_irqsave(ap->lock, flags);
  3601. if (qc->tf.protocol == ATA_PROT_PIO) {
  3602. /* PIO data out protocol.
  3603. * send first data block.
  3604. */
  3605. /* ata_pio_sectors() might change the state
  3606. * to HSM_ST_LAST. so, the state is changed here
  3607. * before ata_pio_sectors().
  3608. */
  3609. ap->hsm_task_state = HSM_ST;
  3610. ata_pio_sectors(qc);
  3611. ata_altstatus(ap); /* flush */
  3612. } else
  3613. /* send CDB */
  3614. atapi_send_cdb(ap, qc);
  3615. if (in_wq)
  3616. spin_unlock_irqrestore(ap->lock, flags);
  3617. /* if polling, ata_pio_task() handles the rest.
  3618. * otherwise, interrupt handler takes over from here.
  3619. */
  3620. break;
  3621. case HSM_ST:
  3622. /* complete command or read/write the data register */
  3623. if (qc->tf.protocol == ATA_PROT_ATAPI) {
  3624. /* ATAPI PIO protocol */
  3625. if ((status & ATA_DRQ) == 0) {
  3626. /* No more data to transfer or device error.
  3627. * Device error will be tagged in HSM_ST_LAST.
  3628. */
  3629. ap->hsm_task_state = HSM_ST_LAST;
  3630. goto fsm_start;
  3631. }
  3632. /* Device should not ask for data transfer (DRQ=1)
  3633. * when it finds something wrong.
  3634. * We ignore DRQ here and stop the HSM by
  3635. * changing hsm_task_state to HSM_ST_ERR and
  3636. * let the EH abort the command or reset the device.
  3637. */
  3638. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  3639. printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
  3640. ap->id, status);
  3641. qc->err_mask |= AC_ERR_HSM;
  3642. ap->hsm_task_state = HSM_ST_ERR;
  3643. goto fsm_start;
  3644. }
  3645. atapi_pio_bytes(qc);
  3646. if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
  3647. /* bad ireason reported by device */
  3648. goto fsm_start;
  3649. } else {
  3650. /* ATA PIO protocol */
  3651. if (unlikely((status & ATA_DRQ) == 0)) {
  3652. /* handle BSY=0, DRQ=0 as error */
  3653. if (likely(status & (ATA_ERR | ATA_DF)))
  3654. /* device stops HSM for abort/error */
  3655. qc->err_mask |= AC_ERR_DEV;
  3656. else
  3657. /* HSM violation. Let EH handle this */
  3658. qc->err_mask |= AC_ERR_HSM;
  3659. ap->hsm_task_state = HSM_ST_ERR;
  3660. goto fsm_start;
  3661. }
  3662. /* For PIO reads, some devices may ask for
  3663. * data transfer (DRQ=1) alone with ERR=1.
  3664. * We respect DRQ here and transfer one
  3665. * block of junk data before changing the
  3666. * hsm_task_state to HSM_ST_ERR.
  3667. *
  3668. * For PIO writes, ERR=1 DRQ=1 doesn't make
  3669. * sense since the data block has been
  3670. * transferred to the device.
  3671. */
  3672. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  3673. /* data might be corrputed */
  3674. qc->err_mask |= AC_ERR_DEV;
  3675. if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
  3676. ata_pio_sectors(qc);
  3677. ata_altstatus(ap);
  3678. status = ata_wait_idle(ap);
  3679. }
  3680. if (status & (ATA_BUSY | ATA_DRQ))
  3681. qc->err_mask |= AC_ERR_HSM;
  3682. /* ata_pio_sectors() might change the
  3683. * state to HSM_ST_LAST. so, the state
  3684. * is changed after ata_pio_sectors().
  3685. */
  3686. ap->hsm_task_state = HSM_ST_ERR;
  3687. goto fsm_start;
  3688. }
  3689. ata_pio_sectors(qc);
  3690. if (ap->hsm_task_state == HSM_ST_LAST &&
  3691. (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
  3692. /* all data read */
  3693. ata_altstatus(ap);
  3694. status = ata_wait_idle(ap);
  3695. goto fsm_start;
  3696. }
  3697. }
  3698. ata_altstatus(ap); /* flush */
  3699. poll_next = 1;
  3700. break;
  3701. case HSM_ST_LAST:
  3702. if (unlikely(!ata_ok(status))) {
  3703. qc->err_mask |= __ac_err_mask(status);
  3704. ap->hsm_task_state = HSM_ST_ERR;
  3705. goto fsm_start;
  3706. }
  3707. /* no more data to transfer */
  3708. DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
  3709. ap->id, qc->dev->devno, status);
  3710. WARN_ON(qc->err_mask);
  3711. ap->hsm_task_state = HSM_ST_IDLE;
  3712. /* complete taskfile transaction */
  3713. ata_hsm_qc_complete(qc, in_wq);
  3714. poll_next = 0;
  3715. break;
  3716. case HSM_ST_ERR:
  3717. /* make sure qc->err_mask is available to
  3718. * know what's wrong and recover
  3719. */
  3720. WARN_ON(qc->err_mask == 0);
  3721. ap->hsm_task_state = HSM_ST_IDLE;
  3722. /* complete taskfile transaction */
  3723. ata_hsm_qc_complete(qc, in_wq);
  3724. poll_next = 0;
  3725. break;
  3726. default:
  3727. poll_next = 0;
  3728. BUG();
  3729. }
  3730. return poll_next;
  3731. }
  3732. static void ata_pio_task(void *_data)
  3733. {
  3734. struct ata_queued_cmd *qc = _data;
  3735. struct ata_port *ap = qc->ap;
  3736. u8 status;
  3737. int poll_next;
  3738. fsm_start:
  3739. WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
  3740. /*
  3741. * This is purely heuristic. This is a fast path.
  3742. * Sometimes when we enter, BSY will be cleared in
  3743. * a chk-status or two. If not, the drive is probably seeking
  3744. * or something. Snooze for a couple msecs, then
  3745. * chk-status again. If still busy, queue delayed work.
  3746. */
  3747. status = ata_busy_wait(ap, ATA_BUSY, 5);
  3748. if (status & ATA_BUSY) {
  3749. msleep(2);
  3750. status = ata_busy_wait(ap, ATA_BUSY, 10);
  3751. if (status & ATA_BUSY) {
  3752. ata_port_queue_task(ap, ata_pio_task, qc, ATA_SHORT_PAUSE);
  3753. return;
  3754. }
  3755. }
  3756. /* move the HSM */
  3757. poll_next = ata_hsm_move(ap, qc, status, 1);
  3758. /* another command or interrupt handler
  3759. * may be running at this point.
  3760. */
  3761. if (poll_next)
  3762. goto fsm_start;
  3763. }
  3764. /**
  3765. * ata_qc_new - Request an available ATA command, for queueing
  3766. * @ap: Port associated with device @dev
  3767. * @dev: Device from whom we request an available command structure
  3768. *
  3769. * LOCKING:
  3770. * None.
  3771. */
  3772. static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
  3773. {
  3774. struct ata_queued_cmd *qc = NULL;
  3775. unsigned int i;
  3776. /* no command while frozen */
  3777. if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
  3778. return NULL;
  3779. /* the last tag is reserved for internal command. */
  3780. for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
  3781. if (!test_and_set_bit(i, &ap->qc_allocated)) {
  3782. qc = __ata_qc_from_tag(ap, i);
  3783. break;
  3784. }
  3785. if (qc)
  3786. qc->tag = i;
  3787. return qc;
  3788. }
  3789. /**
  3790. * ata_qc_new_init - Request an available ATA command, and initialize it
  3791. * @dev: Device from whom we request an available command structure
  3792. *
  3793. * LOCKING:
  3794. * None.
  3795. */
  3796. struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
  3797. {
  3798. struct ata_port *ap = dev->ap;
  3799. struct ata_queued_cmd *qc;
  3800. qc = ata_qc_new(ap);
  3801. if (qc) {
  3802. qc->scsicmd = NULL;
  3803. qc->ap = ap;
  3804. qc->dev = dev;
  3805. ata_qc_reinit(qc);
  3806. }
  3807. return qc;
  3808. }
  3809. /**
  3810. * ata_qc_free - free unused ata_queued_cmd
  3811. * @qc: Command to complete
  3812. *
  3813. * Designed to free unused ata_queued_cmd object
  3814. * in case something prevents using it.
  3815. *
  3816. * LOCKING:
  3817. * spin_lock_irqsave(host lock)
  3818. */
  3819. void ata_qc_free(struct ata_queued_cmd *qc)
  3820. {
  3821. struct ata_port *ap = qc->ap;
  3822. unsigned int tag;
  3823. WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  3824. qc->flags = 0;
  3825. tag = qc->tag;
  3826. if (likely(ata_tag_valid(tag))) {
  3827. qc->tag = ATA_TAG_POISON;
  3828. clear_bit(tag, &ap->qc_allocated);
  3829. }
  3830. }
  3831. void __ata_qc_complete(struct ata_queued_cmd *qc)
  3832. {
  3833. struct ata_port *ap = qc->ap;
  3834. WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  3835. WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
  3836. if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
  3837. ata_sg_clean(qc);
  3838. /* command should be marked inactive atomically with qc completion */
  3839. if (qc->tf.protocol == ATA_PROT_NCQ)
  3840. ap->sactive &= ~(1 << qc->tag);
  3841. else
  3842. ap->active_tag = ATA_TAG_POISON;
  3843. /* atapi: mark qc as inactive to prevent the interrupt handler
  3844. * from completing the command twice later, before the error handler
  3845. * is called. (when rc != 0 and atapi request sense is needed)
  3846. */
  3847. qc->flags &= ~ATA_QCFLAG_ACTIVE;
  3848. ap->qc_active &= ~(1 << qc->tag);
  3849. /* call completion callback */
  3850. qc->complete_fn(qc);
  3851. }
  3852. /**
  3853. * ata_qc_complete - Complete an active ATA command
  3854. * @qc: Command to complete
  3855. * @err_mask: ATA Status register contents
  3856. *
  3857. * Indicate to the mid and upper layers that an ATA
  3858. * command has completed, with either an ok or not-ok status.
  3859. *
  3860. * LOCKING:
  3861. * spin_lock_irqsave(host lock)
  3862. */
  3863. void ata_qc_complete(struct ata_queued_cmd *qc)
  3864. {
  3865. struct ata_port *ap = qc->ap;
  3866. /* XXX: New EH and old EH use different mechanisms to
  3867. * synchronize EH with regular execution path.
  3868. *
  3869. * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
  3870. * Normal execution path is responsible for not accessing a
  3871. * failed qc. libata core enforces the rule by returning NULL
  3872. * from ata_qc_from_tag() for failed qcs.
  3873. *
  3874. * Old EH depends on ata_qc_complete() nullifying completion
  3875. * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
  3876. * not synchronize with interrupt handler. Only PIO task is
  3877. * taken care of.
  3878. */
  3879. if (ap->ops->error_handler) {
  3880. WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
  3881. if (unlikely(qc->err_mask))
  3882. qc->flags |= ATA_QCFLAG_FAILED;
  3883. if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
  3884. if (!ata_tag_internal(qc->tag)) {
  3885. /* always fill result TF for failed qc */
  3886. ap->ops->tf_read(ap, &qc->result_tf);
  3887. ata_qc_schedule_eh(qc);
  3888. return;
  3889. }
  3890. }
  3891. /* read result TF if requested */
  3892. if (qc->flags & ATA_QCFLAG_RESULT_TF)
  3893. ap->ops->tf_read(ap, &qc->result_tf);
  3894. __ata_qc_complete(qc);
  3895. } else {
  3896. if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
  3897. return;
  3898. /* read result TF if failed or requested */
  3899. if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
  3900. ap->ops->tf_read(ap, &qc->result_tf);
  3901. __ata_qc_complete(qc);
  3902. }
  3903. }
  3904. /**
  3905. * ata_qc_complete_multiple - Complete multiple qcs successfully
  3906. * @ap: port in question
  3907. * @qc_active: new qc_active mask
  3908. * @finish_qc: LLDD callback invoked before completing a qc
  3909. *
  3910. * Complete in-flight commands. This functions is meant to be
  3911. * called from low-level driver's interrupt routine to complete
  3912. * requests normally. ap->qc_active and @qc_active is compared
  3913. * and commands are completed accordingly.
  3914. *
  3915. * LOCKING:
  3916. * spin_lock_irqsave(host lock)
  3917. *
  3918. * RETURNS:
  3919. * Number of completed commands on success, -errno otherwise.
  3920. */
  3921. int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
  3922. void (*finish_qc)(struct ata_queued_cmd *))
  3923. {
  3924. int nr_done = 0;
  3925. u32 done_mask;
  3926. int i;
  3927. done_mask = ap->qc_active ^ qc_active;
  3928. if (unlikely(done_mask & qc_active)) {
  3929. ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
  3930. "(%08x->%08x)\n", ap->qc_active, qc_active);
  3931. return -EINVAL;
  3932. }
  3933. for (i = 0; i < ATA_MAX_QUEUE; i++) {
  3934. struct ata_queued_cmd *qc;
  3935. if (!(done_mask & (1 << i)))
  3936. continue;
  3937. if ((qc = ata_qc_from_tag(ap, i))) {
  3938. if (finish_qc)
  3939. finish_qc(qc);
  3940. ata_qc_complete(qc);
  3941. nr_done++;
  3942. }
  3943. }
  3944. return nr_done;
  3945. }
  3946. static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
  3947. {
  3948. struct ata_port *ap = qc->ap;
  3949. switch (qc->tf.protocol) {
  3950. case ATA_PROT_NCQ:
  3951. case ATA_PROT_DMA:
  3952. case ATA_PROT_ATAPI_DMA:
  3953. return 1;
  3954. case ATA_PROT_ATAPI:
  3955. case ATA_PROT_PIO:
  3956. if (ap->flags & ATA_FLAG_PIO_DMA)
  3957. return 1;
  3958. /* fall through */
  3959. default:
  3960. return 0;
  3961. }
  3962. /* never reached */
  3963. }
  3964. /**
  3965. * ata_qc_issue - issue taskfile to device
  3966. * @qc: command to issue to device
  3967. *
  3968. * Prepare an ATA command to submission to device.
  3969. * This includes mapping the data into a DMA-able
  3970. * area, filling in the S/G table, and finally
  3971. * writing the taskfile to hardware, starting the command.
  3972. *
  3973. * LOCKING:
  3974. * spin_lock_irqsave(host lock)
  3975. */
  3976. void ata_qc_issue(struct ata_queued_cmd *qc)
  3977. {
  3978. struct ata_port *ap = qc->ap;
  3979. /* Make sure only one non-NCQ command is outstanding. The
  3980. * check is skipped for old EH because it reuses active qc to
  3981. * request ATAPI sense.
  3982. */
  3983. WARN_ON(ap->ops->error_handler && ata_tag_valid(ap->active_tag));
  3984. if (qc->tf.protocol == ATA_PROT_NCQ) {
  3985. WARN_ON(ap->sactive & (1 << qc->tag));
  3986. ap->sactive |= 1 << qc->tag;
  3987. } else {
  3988. WARN_ON(ap->sactive);
  3989. ap->active_tag = qc->tag;
  3990. }
  3991. qc->flags |= ATA_QCFLAG_ACTIVE;
  3992. ap->qc_active |= 1 << qc->tag;
  3993. if (ata_should_dma_map(qc)) {
  3994. if (qc->flags & ATA_QCFLAG_SG) {
  3995. if (ata_sg_setup(qc))
  3996. goto sg_err;
  3997. } else if (qc->flags & ATA_QCFLAG_SINGLE) {
  3998. if (ata_sg_setup_one(qc))
  3999. goto sg_err;
  4000. }
  4001. } else {
  4002. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  4003. }
  4004. ap->ops->qc_prep(qc);
  4005. qc->err_mask |= ap->ops->qc_issue(qc);
  4006. if (unlikely(qc->err_mask))
  4007. goto err;
  4008. return;
  4009. sg_err:
  4010. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  4011. qc->err_mask |= AC_ERR_SYSTEM;
  4012. err:
  4013. ata_qc_complete(qc);
  4014. }
  4015. /**
  4016. * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
  4017. * @qc: command to issue to device
  4018. *
  4019. * Using various libata functions and hooks, this function
  4020. * starts an ATA command. ATA commands are grouped into
  4021. * classes called "protocols", and issuing each type of protocol
  4022. * is slightly different.
  4023. *
  4024. * May be used as the qc_issue() entry in ata_port_operations.
  4025. *
  4026. * LOCKING:
  4027. * spin_lock_irqsave(host lock)
  4028. *
  4029. * RETURNS:
  4030. * Zero on success, AC_ERR_* mask on failure
  4031. */
  4032. unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
  4033. {
  4034. struct ata_port *ap = qc->ap;
  4035. /* Use polling pio if the LLD doesn't handle
  4036. * interrupt driven pio and atapi CDB interrupt.
  4037. */
  4038. if (ap->flags & ATA_FLAG_PIO_POLLING) {
  4039. switch (qc->tf.protocol) {
  4040. case ATA_PROT_PIO:
  4041. case ATA_PROT_ATAPI:
  4042. case ATA_PROT_ATAPI_NODATA:
  4043. qc->tf.flags |= ATA_TFLAG_POLLING;
  4044. break;
  4045. case ATA_PROT_ATAPI_DMA:
  4046. if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
  4047. /* see ata_dma_blacklisted() */
  4048. BUG();
  4049. break;
  4050. default:
  4051. break;
  4052. }
  4053. }
  4054. /* select the device */
  4055. ata_dev_select(ap, qc->dev->devno, 1, 0);
  4056. /* start the command */
  4057. switch (qc->tf.protocol) {
  4058. case ATA_PROT_NODATA:
  4059. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4060. ata_qc_set_polling(qc);
  4061. ata_tf_to_host(ap, &qc->tf);
  4062. ap->hsm_task_state = HSM_ST_LAST;
  4063. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4064. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4065. break;
  4066. case ATA_PROT_DMA:
  4067. WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
  4068. ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
  4069. ap->ops->bmdma_setup(qc); /* set up bmdma */
  4070. ap->ops->bmdma_start(qc); /* initiate bmdma */
  4071. ap->hsm_task_state = HSM_ST_LAST;
  4072. break;
  4073. case ATA_PROT_PIO:
  4074. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4075. ata_qc_set_polling(qc);
  4076. ata_tf_to_host(ap, &qc->tf);
  4077. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  4078. /* PIO data out protocol */
  4079. ap->hsm_task_state = HSM_ST_FIRST;
  4080. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4081. /* always send first data block using
  4082. * the ata_pio_task() codepath.
  4083. */
  4084. } else {
  4085. /* PIO data in protocol */
  4086. ap->hsm_task_state = HSM_ST;
  4087. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4088. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4089. /* if polling, ata_pio_task() handles the rest.
  4090. * otherwise, interrupt handler takes over from here.
  4091. */
  4092. }
  4093. break;
  4094. case ATA_PROT_ATAPI:
  4095. case ATA_PROT_ATAPI_NODATA:
  4096. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4097. ata_qc_set_polling(qc);
  4098. ata_tf_to_host(ap, &qc->tf);
  4099. ap->hsm_task_state = HSM_ST_FIRST;
  4100. /* send cdb by polling if no cdb interrupt */
  4101. if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
  4102. (qc->tf.flags & ATA_TFLAG_POLLING))
  4103. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4104. break;
  4105. case ATA_PROT_ATAPI_DMA:
  4106. WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
  4107. ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
  4108. ap->ops->bmdma_setup(qc); /* set up bmdma */
  4109. ap->hsm_task_state = HSM_ST_FIRST;
  4110. /* send cdb by polling if no cdb interrupt */
  4111. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  4112. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4113. break;
  4114. default:
  4115. WARN_ON(1);
  4116. return AC_ERR_SYSTEM;
  4117. }
  4118. return 0;
  4119. }
  4120. /**
  4121. * ata_host_intr - Handle host interrupt for given (port, task)
  4122. * @ap: Port on which interrupt arrived (possibly...)
  4123. * @qc: Taskfile currently active in engine
  4124. *
  4125. * Handle host interrupt for given queued command. Currently,
  4126. * only DMA interrupts are handled. All other commands are
  4127. * handled via polling with interrupts disabled (nIEN bit).
  4128. *
  4129. * LOCKING:
  4130. * spin_lock_irqsave(host lock)
  4131. *
  4132. * RETURNS:
  4133. * One if interrupt was handled, zero if not (shared irq).
  4134. */
  4135. inline unsigned int ata_host_intr (struct ata_port *ap,
  4136. struct ata_queued_cmd *qc)
  4137. {
  4138. u8 status, host_stat = 0;
  4139. VPRINTK("ata%u: protocol %d task_state %d\n",
  4140. ap->id, qc->tf.protocol, ap->hsm_task_state);
  4141. /* Check whether we are expecting interrupt in this state */
  4142. switch (ap->hsm_task_state) {
  4143. case HSM_ST_FIRST:
  4144. /* Some pre-ATAPI-4 devices assert INTRQ
  4145. * at this state when ready to receive CDB.
  4146. */
  4147. /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
  4148. * The flag was turned on only for atapi devices.
  4149. * No need to check is_atapi_taskfile(&qc->tf) again.
  4150. */
  4151. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  4152. goto idle_irq;
  4153. break;
  4154. case HSM_ST_LAST:
  4155. if (qc->tf.protocol == ATA_PROT_DMA ||
  4156. qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
  4157. /* check status of DMA engine */
  4158. host_stat = ap->ops->bmdma_status(ap);
  4159. VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
  4160. /* if it's not our irq... */
  4161. if (!(host_stat & ATA_DMA_INTR))
  4162. goto idle_irq;
  4163. /* before we do anything else, clear DMA-Start bit */
  4164. ap->ops->bmdma_stop(qc);
  4165. if (unlikely(host_stat & ATA_DMA_ERR)) {
  4166. /* error when transfering data to/from memory */
  4167. qc->err_mask |= AC_ERR_HOST_BUS;
  4168. ap->hsm_task_state = HSM_ST_ERR;
  4169. }
  4170. }
  4171. break;
  4172. case HSM_ST:
  4173. break;
  4174. default:
  4175. goto idle_irq;
  4176. }
  4177. /* check altstatus */
  4178. status = ata_altstatus(ap);
  4179. if (status & ATA_BUSY)
  4180. goto idle_irq;
  4181. /* check main status, clearing INTRQ */
  4182. status = ata_chk_status(ap);
  4183. if (unlikely(status & ATA_BUSY))
  4184. goto idle_irq;
  4185. /* ack bmdma irq events */
  4186. ap->ops->irq_clear(ap);
  4187. ata_hsm_move(ap, qc, status, 0);
  4188. return 1; /* irq handled */
  4189. idle_irq:
  4190. ap->stats.idle_irq++;
  4191. #ifdef ATA_IRQ_TRAP
  4192. if ((ap->stats.idle_irq % 1000) == 0) {
  4193. ata_irq_ack(ap, 0); /* debug trap */
  4194. ata_port_printk(ap, KERN_WARNING, "irq trap\n");
  4195. return 1;
  4196. }
  4197. #endif
  4198. return 0; /* irq not handled */
  4199. }
  4200. /**
  4201. * ata_interrupt - Default ATA host interrupt handler
  4202. * @irq: irq line (unused)
  4203. * @dev_instance: pointer to our ata_host information structure
  4204. *
  4205. * Default interrupt handler for PCI IDE devices. Calls
  4206. * ata_host_intr() for each port that is not disabled.
  4207. *
  4208. * LOCKING:
  4209. * Obtains host lock during operation.
  4210. *
  4211. * RETURNS:
  4212. * IRQ_NONE or IRQ_HANDLED.
  4213. */
  4214. irqreturn_t ata_interrupt (int irq, void *dev_instance)
  4215. {
  4216. struct ata_host *host = dev_instance;
  4217. unsigned int i;
  4218. unsigned int handled = 0;
  4219. unsigned long flags;
  4220. /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
  4221. spin_lock_irqsave(&host->lock, flags);
  4222. for (i = 0; i < host->n_ports; i++) {
  4223. struct ata_port *ap;
  4224. ap = host->ports[i];
  4225. if (ap &&
  4226. !(ap->flags & ATA_FLAG_DISABLED)) {
  4227. struct ata_queued_cmd *qc;
  4228. qc = ata_qc_from_tag(ap, ap->active_tag);
  4229. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
  4230. (qc->flags & ATA_QCFLAG_ACTIVE))
  4231. handled |= ata_host_intr(ap, qc);
  4232. }
  4233. }
  4234. spin_unlock_irqrestore(&host->lock, flags);
  4235. return IRQ_RETVAL(handled);
  4236. }
  4237. /**
  4238. * sata_scr_valid - test whether SCRs are accessible
  4239. * @ap: ATA port to test SCR accessibility for
  4240. *
  4241. * Test whether SCRs are accessible for @ap.
  4242. *
  4243. * LOCKING:
  4244. * None.
  4245. *
  4246. * RETURNS:
  4247. * 1 if SCRs are accessible, 0 otherwise.
  4248. */
  4249. int sata_scr_valid(struct ata_port *ap)
  4250. {
  4251. return ap->cbl == ATA_CBL_SATA && ap->ops->scr_read;
  4252. }
  4253. /**
  4254. * sata_scr_read - read SCR register of the specified port
  4255. * @ap: ATA port to read SCR for
  4256. * @reg: SCR to read
  4257. * @val: Place to store read value
  4258. *
  4259. * Read SCR register @reg of @ap into *@val. This function is
  4260. * guaranteed to succeed if the cable type of the port is SATA
  4261. * and the port implements ->scr_read.
  4262. *
  4263. * LOCKING:
  4264. * None.
  4265. *
  4266. * RETURNS:
  4267. * 0 on success, negative errno on failure.
  4268. */
  4269. int sata_scr_read(struct ata_port *ap, int reg, u32 *val)
  4270. {
  4271. if (sata_scr_valid(ap)) {
  4272. *val = ap->ops->scr_read(ap, reg);
  4273. return 0;
  4274. }
  4275. return -EOPNOTSUPP;
  4276. }
  4277. /**
  4278. * sata_scr_write - write SCR register of the specified port
  4279. * @ap: ATA port to write SCR for
  4280. * @reg: SCR to write
  4281. * @val: value to write
  4282. *
  4283. * Write @val to SCR register @reg of @ap. This function is
  4284. * guaranteed to succeed if the cable type of the port is SATA
  4285. * and the port implements ->scr_read.
  4286. *
  4287. * LOCKING:
  4288. * None.
  4289. *
  4290. * RETURNS:
  4291. * 0 on success, negative errno on failure.
  4292. */
  4293. int sata_scr_write(struct ata_port *ap, int reg, u32 val)
  4294. {
  4295. if (sata_scr_valid(ap)) {
  4296. ap->ops->scr_write(ap, reg, val);
  4297. return 0;
  4298. }
  4299. return -EOPNOTSUPP;
  4300. }
  4301. /**
  4302. * sata_scr_write_flush - write SCR register of the specified port and flush
  4303. * @ap: ATA port to write SCR for
  4304. * @reg: SCR to write
  4305. * @val: value to write
  4306. *
  4307. * This function is identical to sata_scr_write() except that this
  4308. * function performs flush after writing to the register.
  4309. *
  4310. * LOCKING:
  4311. * None.
  4312. *
  4313. * RETURNS:
  4314. * 0 on success, negative errno on failure.
  4315. */
  4316. int sata_scr_write_flush(struct ata_port *ap, int reg, u32 val)
  4317. {
  4318. if (sata_scr_valid(ap)) {
  4319. ap->ops->scr_write(ap, reg, val);
  4320. ap->ops->scr_read(ap, reg);
  4321. return 0;
  4322. }
  4323. return -EOPNOTSUPP;
  4324. }
  4325. /**
  4326. * ata_port_online - test whether the given port is online
  4327. * @ap: ATA port to test
  4328. *
  4329. * Test whether @ap is online. Note that this function returns 0
  4330. * if online status of @ap cannot be obtained, so
  4331. * ata_port_online(ap) != !ata_port_offline(ap).
  4332. *
  4333. * LOCKING:
  4334. * None.
  4335. *
  4336. * RETURNS:
  4337. * 1 if the port online status is available and online.
  4338. */
  4339. int ata_port_online(struct ata_port *ap)
  4340. {
  4341. u32 sstatus;
  4342. if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) == 0x3)
  4343. return 1;
  4344. return 0;
  4345. }
  4346. /**
  4347. * ata_port_offline - test whether the given port is offline
  4348. * @ap: ATA port to test
  4349. *
  4350. * Test whether @ap is offline. Note that this function returns
  4351. * 0 if offline status of @ap cannot be obtained, so
  4352. * ata_port_online(ap) != !ata_port_offline(ap).
  4353. *
  4354. * LOCKING:
  4355. * None.
  4356. *
  4357. * RETURNS:
  4358. * 1 if the port offline status is available and offline.
  4359. */
  4360. int ata_port_offline(struct ata_port *ap)
  4361. {
  4362. u32 sstatus;
  4363. if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) != 0x3)
  4364. return 1;
  4365. return 0;
  4366. }
  4367. int ata_flush_cache(struct ata_device *dev)
  4368. {
  4369. unsigned int err_mask;
  4370. u8 cmd;
  4371. if (!ata_try_flush_cache(dev))
  4372. return 0;
  4373. if (dev->flags & ATA_DFLAG_FLUSH_EXT)
  4374. cmd = ATA_CMD_FLUSH_EXT;
  4375. else
  4376. cmd = ATA_CMD_FLUSH;
  4377. err_mask = ata_do_simple_cmd(dev, cmd);
  4378. if (err_mask) {
  4379. ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
  4380. return -EIO;
  4381. }
  4382. return 0;
  4383. }
  4384. static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
  4385. unsigned int action, unsigned int ehi_flags,
  4386. int wait)
  4387. {
  4388. unsigned long flags;
  4389. int i, rc;
  4390. for (i = 0; i < host->n_ports; i++) {
  4391. struct ata_port *ap = host->ports[i];
  4392. /* Previous resume operation might still be in
  4393. * progress. Wait for PM_PENDING to clear.
  4394. */
  4395. if (ap->pflags & ATA_PFLAG_PM_PENDING) {
  4396. ata_port_wait_eh(ap);
  4397. WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
  4398. }
  4399. /* request PM ops to EH */
  4400. spin_lock_irqsave(ap->lock, flags);
  4401. ap->pm_mesg = mesg;
  4402. if (wait) {
  4403. rc = 0;
  4404. ap->pm_result = &rc;
  4405. }
  4406. ap->pflags |= ATA_PFLAG_PM_PENDING;
  4407. ap->eh_info.action |= action;
  4408. ap->eh_info.flags |= ehi_flags;
  4409. ata_port_schedule_eh(ap);
  4410. spin_unlock_irqrestore(ap->lock, flags);
  4411. /* wait and check result */
  4412. if (wait) {
  4413. ata_port_wait_eh(ap);
  4414. WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
  4415. if (rc)
  4416. return rc;
  4417. }
  4418. }
  4419. return 0;
  4420. }
  4421. /**
  4422. * ata_host_suspend - suspend host
  4423. * @host: host to suspend
  4424. * @mesg: PM message
  4425. *
  4426. * Suspend @host. Actual operation is performed by EH. This
  4427. * function requests EH to perform PM operations and waits for EH
  4428. * to finish.
  4429. *
  4430. * LOCKING:
  4431. * Kernel thread context (may sleep).
  4432. *
  4433. * RETURNS:
  4434. * 0 on success, -errno on failure.
  4435. */
  4436. int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
  4437. {
  4438. int i, j, rc;
  4439. rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
  4440. if (rc)
  4441. goto fail;
  4442. /* EH is quiescent now. Fail if we have any ready device.
  4443. * This happens if hotplug occurs between completion of device
  4444. * suspension and here.
  4445. */
  4446. for (i = 0; i < host->n_ports; i++) {
  4447. struct ata_port *ap = host->ports[i];
  4448. for (j = 0; j < ATA_MAX_DEVICES; j++) {
  4449. struct ata_device *dev = &ap->device[j];
  4450. if (ata_dev_ready(dev)) {
  4451. ata_port_printk(ap, KERN_WARNING,
  4452. "suspend failed, device %d "
  4453. "still active\n", dev->devno);
  4454. rc = -EBUSY;
  4455. goto fail;
  4456. }
  4457. }
  4458. }
  4459. host->dev->power.power_state = mesg;
  4460. return 0;
  4461. fail:
  4462. ata_host_resume(host);
  4463. return rc;
  4464. }
  4465. /**
  4466. * ata_host_resume - resume host
  4467. * @host: host to resume
  4468. *
  4469. * Resume @host. Actual operation is performed by EH. This
  4470. * function requests EH to perform PM operations and returns.
  4471. * Note that all resume operations are performed parallely.
  4472. *
  4473. * LOCKING:
  4474. * Kernel thread context (may sleep).
  4475. */
  4476. void ata_host_resume(struct ata_host *host)
  4477. {
  4478. ata_host_request_pm(host, PMSG_ON, ATA_EH_SOFTRESET,
  4479. ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
  4480. host->dev->power.power_state = PMSG_ON;
  4481. }
  4482. /**
  4483. * ata_port_start - Set port up for dma.
  4484. * @ap: Port to initialize
  4485. *
  4486. * Called just after data structures for each port are
  4487. * initialized. Allocates space for PRD table.
  4488. *
  4489. * May be used as the port_start() entry in ata_port_operations.
  4490. *
  4491. * LOCKING:
  4492. * Inherited from caller.
  4493. */
  4494. int ata_port_start (struct ata_port *ap)
  4495. {
  4496. struct device *dev = ap->dev;
  4497. int rc;
  4498. ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
  4499. if (!ap->prd)
  4500. return -ENOMEM;
  4501. rc = ata_pad_alloc(ap, dev);
  4502. if (rc) {
  4503. dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
  4504. return rc;
  4505. }
  4506. DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
  4507. return 0;
  4508. }
  4509. /**
  4510. * ata_port_stop - Undo ata_port_start()
  4511. * @ap: Port to shut down
  4512. *
  4513. * Frees the PRD table.
  4514. *
  4515. * May be used as the port_stop() entry in ata_port_operations.
  4516. *
  4517. * LOCKING:
  4518. * Inherited from caller.
  4519. */
  4520. void ata_port_stop (struct ata_port *ap)
  4521. {
  4522. struct device *dev = ap->dev;
  4523. dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
  4524. ata_pad_free(ap, dev);
  4525. }
  4526. void ata_host_stop (struct ata_host *host)
  4527. {
  4528. if (host->mmio_base)
  4529. iounmap(host->mmio_base);
  4530. }
  4531. /**
  4532. * ata_dev_init - Initialize an ata_device structure
  4533. * @dev: Device structure to initialize
  4534. *
  4535. * Initialize @dev in preparation for probing.
  4536. *
  4537. * LOCKING:
  4538. * Inherited from caller.
  4539. */
  4540. void ata_dev_init(struct ata_device *dev)
  4541. {
  4542. struct ata_port *ap = dev->ap;
  4543. unsigned long flags;
  4544. /* SATA spd limit is bound to the first device */
  4545. ap->sata_spd_limit = ap->hw_sata_spd_limit;
  4546. /* High bits of dev->flags are used to record warm plug
  4547. * requests which occur asynchronously. Synchronize using
  4548. * host lock.
  4549. */
  4550. spin_lock_irqsave(ap->lock, flags);
  4551. dev->flags &= ~ATA_DFLAG_INIT_MASK;
  4552. spin_unlock_irqrestore(ap->lock, flags);
  4553. memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
  4554. sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
  4555. dev->pio_mask = UINT_MAX;
  4556. dev->mwdma_mask = UINT_MAX;
  4557. dev->udma_mask = UINT_MAX;
  4558. }
  4559. /**
  4560. * ata_port_init - Initialize an ata_port structure
  4561. * @ap: Structure to initialize
  4562. * @host: Collection of hosts to which @ap belongs
  4563. * @ent: Probe information provided by low-level driver
  4564. * @port_no: Port number associated with this ata_port
  4565. *
  4566. * Initialize a new ata_port structure.
  4567. *
  4568. * LOCKING:
  4569. * Inherited from caller.
  4570. */
  4571. void ata_port_init(struct ata_port *ap, struct ata_host *host,
  4572. const struct ata_probe_ent *ent, unsigned int port_no)
  4573. {
  4574. unsigned int i;
  4575. ap->lock = &host->lock;
  4576. ap->flags = ATA_FLAG_DISABLED;
  4577. ap->id = ata_unique_id++;
  4578. ap->ctl = ATA_DEVCTL_OBS;
  4579. ap->host = host;
  4580. ap->dev = ent->dev;
  4581. ap->port_no = port_no;
  4582. if (port_no == 1 && ent->pinfo2) {
  4583. ap->pio_mask = ent->pinfo2->pio_mask;
  4584. ap->mwdma_mask = ent->pinfo2->mwdma_mask;
  4585. ap->udma_mask = ent->pinfo2->udma_mask;
  4586. ap->flags |= ent->pinfo2->flags;
  4587. ap->ops = ent->pinfo2->port_ops;
  4588. } else {
  4589. ap->pio_mask = ent->pio_mask;
  4590. ap->mwdma_mask = ent->mwdma_mask;
  4591. ap->udma_mask = ent->udma_mask;
  4592. ap->flags |= ent->port_flags;
  4593. ap->ops = ent->port_ops;
  4594. }
  4595. ap->hw_sata_spd_limit = UINT_MAX;
  4596. ap->active_tag = ATA_TAG_POISON;
  4597. ap->last_ctl = 0xFF;
  4598. #if defined(ATA_VERBOSE_DEBUG)
  4599. /* turn on all debugging levels */
  4600. ap->msg_enable = 0x00FF;
  4601. #elif defined(ATA_DEBUG)
  4602. ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
  4603. #else
  4604. ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
  4605. #endif
  4606. INIT_WORK(&ap->port_task, NULL, NULL);
  4607. INIT_WORK(&ap->hotplug_task, ata_scsi_hotplug, ap);
  4608. INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan, ap);
  4609. INIT_LIST_HEAD(&ap->eh_done_q);
  4610. init_waitqueue_head(&ap->eh_wait_q);
  4611. /* set cable type */
  4612. ap->cbl = ATA_CBL_NONE;
  4613. if (ap->flags & ATA_FLAG_SATA)
  4614. ap->cbl = ATA_CBL_SATA;
  4615. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  4616. struct ata_device *dev = &ap->device[i];
  4617. dev->ap = ap;
  4618. dev->devno = i;
  4619. ata_dev_init(dev);
  4620. }
  4621. #ifdef ATA_IRQ_TRAP
  4622. ap->stats.unhandled_irq = 1;
  4623. ap->stats.idle_irq = 1;
  4624. #endif
  4625. memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
  4626. }
  4627. /**
  4628. * ata_port_init_shost - Initialize SCSI host associated with ATA port
  4629. * @ap: ATA port to initialize SCSI host for
  4630. * @shost: SCSI host associated with @ap
  4631. *
  4632. * Initialize SCSI host @shost associated with ATA port @ap.
  4633. *
  4634. * LOCKING:
  4635. * Inherited from caller.
  4636. */
  4637. static void ata_port_init_shost(struct ata_port *ap, struct Scsi_Host *shost)
  4638. {
  4639. ap->scsi_host = shost;
  4640. shost->unique_id = ap->id;
  4641. shost->max_id = 16;
  4642. shost->max_lun = 1;
  4643. shost->max_channel = 1;
  4644. shost->max_cmd_len = 12;
  4645. }
  4646. /**
  4647. * ata_port_add - Attach low-level ATA driver to system
  4648. * @ent: Information provided by low-level driver
  4649. * @host: Collections of ports to which we add
  4650. * @port_no: Port number associated with this host
  4651. *
  4652. * Attach low-level ATA driver to system.
  4653. *
  4654. * LOCKING:
  4655. * PCI/etc. bus probe sem.
  4656. *
  4657. * RETURNS:
  4658. * New ata_port on success, for NULL on error.
  4659. */
  4660. static struct ata_port * ata_port_add(const struct ata_probe_ent *ent,
  4661. struct ata_host *host,
  4662. unsigned int port_no)
  4663. {
  4664. struct Scsi_Host *shost;
  4665. struct ata_port *ap;
  4666. DPRINTK("ENTER\n");
  4667. if (!ent->port_ops->error_handler &&
  4668. !(ent->port_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) {
  4669. printk(KERN_ERR "ata%u: no reset mechanism available\n",
  4670. port_no);
  4671. return NULL;
  4672. }
  4673. shost = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
  4674. if (!shost)
  4675. return NULL;
  4676. shost->transportt = &ata_scsi_transport_template;
  4677. ap = ata_shost_to_port(shost);
  4678. ata_port_init(ap, host, ent, port_no);
  4679. ata_port_init_shost(ap, shost);
  4680. return ap;
  4681. }
  4682. /**
  4683. * ata_sas_host_init - Initialize a host struct
  4684. * @host: host to initialize
  4685. * @dev: device host is attached to
  4686. * @flags: host flags
  4687. * @ops: port_ops
  4688. *
  4689. * LOCKING:
  4690. * PCI/etc. bus probe sem.
  4691. *
  4692. */
  4693. void ata_host_init(struct ata_host *host, struct device *dev,
  4694. unsigned long flags, const struct ata_port_operations *ops)
  4695. {
  4696. spin_lock_init(&host->lock);
  4697. host->dev = dev;
  4698. host->flags = flags;
  4699. host->ops = ops;
  4700. }
  4701. /**
  4702. * ata_device_add - Register hardware device with ATA and SCSI layers
  4703. * @ent: Probe information describing hardware device to be registered
  4704. *
  4705. * This function processes the information provided in the probe
  4706. * information struct @ent, allocates the necessary ATA and SCSI
  4707. * host information structures, initializes them, and registers
  4708. * everything with requisite kernel subsystems.
  4709. *
  4710. * This function requests irqs, probes the ATA bus, and probes
  4711. * the SCSI bus.
  4712. *
  4713. * LOCKING:
  4714. * PCI/etc. bus probe sem.
  4715. *
  4716. * RETURNS:
  4717. * Number of ports registered. Zero on error (no ports registered).
  4718. */
  4719. int ata_device_add(const struct ata_probe_ent *ent)
  4720. {
  4721. unsigned int i;
  4722. struct device *dev = ent->dev;
  4723. struct ata_host *host;
  4724. int rc;
  4725. DPRINTK("ENTER\n");
  4726. if (ent->irq == 0) {
  4727. dev_printk(KERN_ERR, dev, "is not available: No interrupt assigned.\n");
  4728. return 0;
  4729. }
  4730. /* alloc a container for our list of ATA ports (buses) */
  4731. host = kzalloc(sizeof(struct ata_host) +
  4732. (ent->n_ports * sizeof(void *)), GFP_KERNEL);
  4733. if (!host)
  4734. return 0;
  4735. ata_host_init(host, dev, ent->_host_flags, ent->port_ops);
  4736. host->n_ports = ent->n_ports;
  4737. host->irq = ent->irq;
  4738. host->irq2 = ent->irq2;
  4739. host->mmio_base = ent->mmio_base;
  4740. host->private_data = ent->private_data;
  4741. /* register each port bound to this device */
  4742. for (i = 0; i < host->n_ports; i++) {
  4743. struct ata_port *ap;
  4744. unsigned long xfer_mode_mask;
  4745. int irq_line = ent->irq;
  4746. ap = ata_port_add(ent, host, i);
  4747. host->ports[i] = ap;
  4748. if (!ap)
  4749. goto err_out;
  4750. /* dummy? */
  4751. if (ent->dummy_port_mask & (1 << i)) {
  4752. ata_port_printk(ap, KERN_INFO, "DUMMY\n");
  4753. ap->ops = &ata_dummy_port_ops;
  4754. continue;
  4755. }
  4756. /* start port */
  4757. rc = ap->ops->port_start(ap);
  4758. if (rc) {
  4759. host->ports[i] = NULL;
  4760. scsi_host_put(ap->scsi_host);
  4761. goto err_out;
  4762. }
  4763. /* Report the secondary IRQ for second channel legacy */
  4764. if (i == 1 && ent->irq2)
  4765. irq_line = ent->irq2;
  4766. xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
  4767. (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
  4768. (ap->pio_mask << ATA_SHIFT_PIO);
  4769. /* print per-port info to dmesg */
  4770. ata_port_printk(ap, KERN_INFO, "%cATA max %s cmd 0x%lX "
  4771. "ctl 0x%lX bmdma 0x%lX irq %d\n",
  4772. ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
  4773. ata_mode_string(xfer_mode_mask),
  4774. ap->ioaddr.cmd_addr,
  4775. ap->ioaddr.ctl_addr,
  4776. ap->ioaddr.bmdma_addr,
  4777. irq_line);
  4778. ata_chk_status(ap);
  4779. host->ops->irq_clear(ap);
  4780. ata_eh_freeze_port(ap); /* freeze port before requesting IRQ */
  4781. }
  4782. /* obtain irq, that may be shared between channels */
  4783. rc = request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
  4784. DRV_NAME, host);
  4785. if (rc) {
  4786. dev_printk(KERN_ERR, dev, "irq %lu request failed: %d\n",
  4787. ent->irq, rc);
  4788. goto err_out;
  4789. }
  4790. /* do we have a second IRQ for the other channel, eg legacy mode */
  4791. if (ent->irq2) {
  4792. /* We will get weird core code crashes later if this is true
  4793. so trap it now */
  4794. BUG_ON(ent->irq == ent->irq2);
  4795. rc = request_irq(ent->irq2, ent->port_ops->irq_handler, ent->irq_flags,
  4796. DRV_NAME, host);
  4797. if (rc) {
  4798. dev_printk(KERN_ERR, dev, "irq %lu request failed: %d\n",
  4799. ent->irq2, rc);
  4800. goto err_out_free_irq;
  4801. }
  4802. }
  4803. /* perform each probe synchronously */
  4804. DPRINTK("probe begin\n");
  4805. for (i = 0; i < host->n_ports; i++) {
  4806. struct ata_port *ap = host->ports[i];
  4807. u32 scontrol;
  4808. int rc;
  4809. /* init sata_spd_limit to the current value */
  4810. if (sata_scr_read(ap, SCR_CONTROL, &scontrol) == 0) {
  4811. int spd = (scontrol >> 4) & 0xf;
  4812. ap->hw_sata_spd_limit &= (1 << spd) - 1;
  4813. }
  4814. ap->sata_spd_limit = ap->hw_sata_spd_limit;
  4815. rc = scsi_add_host(ap->scsi_host, dev);
  4816. if (rc) {
  4817. ata_port_printk(ap, KERN_ERR, "scsi_add_host failed\n");
  4818. /* FIXME: do something useful here */
  4819. /* FIXME: handle unconditional calls to
  4820. * scsi_scan_host and ata_host_remove, below,
  4821. * at the very least
  4822. */
  4823. }
  4824. if (ap->ops->error_handler) {
  4825. struct ata_eh_info *ehi = &ap->eh_info;
  4826. unsigned long flags;
  4827. ata_port_probe(ap);
  4828. /* kick EH for boot probing */
  4829. spin_lock_irqsave(ap->lock, flags);
  4830. ehi->probe_mask = (1 << ATA_MAX_DEVICES) - 1;
  4831. ehi->action |= ATA_EH_SOFTRESET;
  4832. ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
  4833. ap->pflags |= ATA_PFLAG_LOADING;
  4834. ata_port_schedule_eh(ap);
  4835. spin_unlock_irqrestore(ap->lock, flags);
  4836. /* wait for EH to finish */
  4837. ata_port_wait_eh(ap);
  4838. } else {
  4839. DPRINTK("ata%u: bus probe begin\n", ap->id);
  4840. rc = ata_bus_probe(ap);
  4841. DPRINTK("ata%u: bus probe end\n", ap->id);
  4842. if (rc) {
  4843. /* FIXME: do something useful here?
  4844. * Current libata behavior will
  4845. * tear down everything when
  4846. * the module is removed
  4847. * or the h/w is unplugged.
  4848. */
  4849. }
  4850. }
  4851. }
  4852. /* probes are done, now scan each port's disk(s) */
  4853. DPRINTK("host probe begin\n");
  4854. for (i = 0; i < host->n_ports; i++) {
  4855. struct ata_port *ap = host->ports[i];
  4856. ata_scsi_scan_host(ap);
  4857. }
  4858. dev_set_drvdata(dev, host);
  4859. VPRINTK("EXIT, returning %u\n", ent->n_ports);
  4860. return ent->n_ports; /* success */
  4861. err_out_free_irq:
  4862. free_irq(ent->irq, host);
  4863. err_out:
  4864. for (i = 0; i < host->n_ports; i++) {
  4865. struct ata_port *ap = host->ports[i];
  4866. if (ap) {
  4867. ap->ops->port_stop(ap);
  4868. scsi_host_put(ap->scsi_host);
  4869. }
  4870. }
  4871. kfree(host);
  4872. VPRINTK("EXIT, returning 0\n");
  4873. return 0;
  4874. }
  4875. /**
  4876. * ata_port_detach - Detach ATA port in prepration of device removal
  4877. * @ap: ATA port to be detached
  4878. *
  4879. * Detach all ATA devices and the associated SCSI devices of @ap;
  4880. * then, remove the associated SCSI host. @ap is guaranteed to
  4881. * be quiescent on return from this function.
  4882. *
  4883. * LOCKING:
  4884. * Kernel thread context (may sleep).
  4885. */
  4886. void ata_port_detach(struct ata_port *ap)
  4887. {
  4888. unsigned long flags;
  4889. int i;
  4890. if (!ap->ops->error_handler)
  4891. goto skip_eh;
  4892. /* tell EH we're leaving & flush EH */
  4893. spin_lock_irqsave(ap->lock, flags);
  4894. ap->pflags |= ATA_PFLAG_UNLOADING;
  4895. spin_unlock_irqrestore(ap->lock, flags);
  4896. ata_port_wait_eh(ap);
  4897. /* EH is now guaranteed to see UNLOADING, so no new device
  4898. * will be attached. Disable all existing devices.
  4899. */
  4900. spin_lock_irqsave(ap->lock, flags);
  4901. for (i = 0; i < ATA_MAX_DEVICES; i++)
  4902. ata_dev_disable(&ap->device[i]);
  4903. spin_unlock_irqrestore(ap->lock, flags);
  4904. /* Final freeze & EH. All in-flight commands are aborted. EH
  4905. * will be skipped and retrials will be terminated with bad
  4906. * target.
  4907. */
  4908. spin_lock_irqsave(ap->lock, flags);
  4909. ata_port_freeze(ap); /* won't be thawed */
  4910. spin_unlock_irqrestore(ap->lock, flags);
  4911. ata_port_wait_eh(ap);
  4912. /* Flush hotplug task. The sequence is similar to
  4913. * ata_port_flush_task().
  4914. */
  4915. flush_workqueue(ata_aux_wq);
  4916. cancel_delayed_work(&ap->hotplug_task);
  4917. flush_workqueue(ata_aux_wq);
  4918. skip_eh:
  4919. /* remove the associated SCSI host */
  4920. scsi_remove_host(ap->scsi_host);
  4921. }
  4922. /**
  4923. * ata_host_remove - PCI layer callback for device removal
  4924. * @host: ATA host set that was removed
  4925. *
  4926. * Unregister all objects associated with this host set. Free those
  4927. * objects.
  4928. *
  4929. * LOCKING:
  4930. * Inherited from calling layer (may sleep).
  4931. */
  4932. void ata_host_remove(struct ata_host *host)
  4933. {
  4934. unsigned int i;
  4935. for (i = 0; i < host->n_ports; i++)
  4936. ata_port_detach(host->ports[i]);
  4937. free_irq(host->irq, host);
  4938. if (host->irq2)
  4939. free_irq(host->irq2, host);
  4940. for (i = 0; i < host->n_ports; i++) {
  4941. struct ata_port *ap = host->ports[i];
  4942. ata_scsi_release(ap->scsi_host);
  4943. if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
  4944. struct ata_ioports *ioaddr = &ap->ioaddr;
  4945. /* FIXME: Add -ac IDE pci mods to remove these special cases */
  4946. if (ioaddr->cmd_addr == ATA_PRIMARY_CMD)
  4947. release_region(ATA_PRIMARY_CMD, 8);
  4948. else if (ioaddr->cmd_addr == ATA_SECONDARY_CMD)
  4949. release_region(ATA_SECONDARY_CMD, 8);
  4950. }
  4951. scsi_host_put(ap->scsi_host);
  4952. }
  4953. if (host->ops->host_stop)
  4954. host->ops->host_stop(host);
  4955. kfree(host);
  4956. }
  4957. /**
  4958. * ata_scsi_release - SCSI layer callback hook for host unload
  4959. * @shost: libata host to be unloaded
  4960. *
  4961. * Performs all duties necessary to shut down a libata port...
  4962. * Kill port kthread, disable port, and release resources.
  4963. *
  4964. * LOCKING:
  4965. * Inherited from SCSI layer.
  4966. *
  4967. * RETURNS:
  4968. * One.
  4969. */
  4970. int ata_scsi_release(struct Scsi_Host *shost)
  4971. {
  4972. struct ata_port *ap = ata_shost_to_port(shost);
  4973. DPRINTK("ENTER\n");
  4974. ap->ops->port_disable(ap);
  4975. ap->ops->port_stop(ap);
  4976. DPRINTK("EXIT\n");
  4977. return 1;
  4978. }
  4979. struct ata_probe_ent *
  4980. ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
  4981. {
  4982. struct ata_probe_ent *probe_ent;
  4983. probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
  4984. if (!probe_ent) {
  4985. printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
  4986. kobject_name(&(dev->kobj)));
  4987. return NULL;
  4988. }
  4989. INIT_LIST_HEAD(&probe_ent->node);
  4990. probe_ent->dev = dev;
  4991. probe_ent->sht = port->sht;
  4992. probe_ent->port_flags = port->flags;
  4993. probe_ent->pio_mask = port->pio_mask;
  4994. probe_ent->mwdma_mask = port->mwdma_mask;
  4995. probe_ent->udma_mask = port->udma_mask;
  4996. probe_ent->port_ops = port->port_ops;
  4997. probe_ent->private_data = port->private_data;
  4998. return probe_ent;
  4999. }
  5000. /**
  5001. * ata_std_ports - initialize ioaddr with standard port offsets.
  5002. * @ioaddr: IO address structure to be initialized
  5003. *
  5004. * Utility function which initializes data_addr, error_addr,
  5005. * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
  5006. * device_addr, status_addr, and command_addr to standard offsets
  5007. * relative to cmd_addr.
  5008. *
  5009. * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
  5010. */
  5011. void ata_std_ports(struct ata_ioports *ioaddr)
  5012. {
  5013. ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
  5014. ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
  5015. ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
  5016. ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
  5017. ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
  5018. ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
  5019. ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
  5020. ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
  5021. ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
  5022. ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
  5023. }
  5024. #ifdef CONFIG_PCI
  5025. void ata_pci_host_stop (struct ata_host *host)
  5026. {
  5027. struct pci_dev *pdev = to_pci_dev(host->dev);
  5028. pci_iounmap(pdev, host->mmio_base);
  5029. }
  5030. /**
  5031. * ata_pci_remove_one - PCI layer callback for device removal
  5032. * @pdev: PCI device that was removed
  5033. *
  5034. * PCI layer indicates to libata via this hook that
  5035. * hot-unplug or module unload event has occurred.
  5036. * Handle this by unregistering all objects associated
  5037. * with this PCI device. Free those objects. Then finally
  5038. * release PCI resources and disable device.
  5039. *
  5040. * LOCKING:
  5041. * Inherited from PCI layer (may sleep).
  5042. */
  5043. void ata_pci_remove_one (struct pci_dev *pdev)
  5044. {
  5045. struct device *dev = pci_dev_to_dev(pdev);
  5046. struct ata_host *host = dev_get_drvdata(dev);
  5047. ata_host_remove(host);
  5048. pci_release_regions(pdev);
  5049. pci_disable_device(pdev);
  5050. dev_set_drvdata(dev, NULL);
  5051. }
  5052. /* move to PCI subsystem */
  5053. int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
  5054. {
  5055. unsigned long tmp = 0;
  5056. switch (bits->width) {
  5057. case 1: {
  5058. u8 tmp8 = 0;
  5059. pci_read_config_byte(pdev, bits->reg, &tmp8);
  5060. tmp = tmp8;
  5061. break;
  5062. }
  5063. case 2: {
  5064. u16 tmp16 = 0;
  5065. pci_read_config_word(pdev, bits->reg, &tmp16);
  5066. tmp = tmp16;
  5067. break;
  5068. }
  5069. case 4: {
  5070. u32 tmp32 = 0;
  5071. pci_read_config_dword(pdev, bits->reg, &tmp32);
  5072. tmp = tmp32;
  5073. break;
  5074. }
  5075. default:
  5076. return -EINVAL;
  5077. }
  5078. tmp &= bits->mask;
  5079. return (tmp == bits->val) ? 1 : 0;
  5080. }
  5081. void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
  5082. {
  5083. pci_save_state(pdev);
  5084. if (mesg.event == PM_EVENT_SUSPEND) {
  5085. pci_disable_device(pdev);
  5086. pci_set_power_state(pdev, PCI_D3hot);
  5087. }
  5088. }
  5089. void ata_pci_device_do_resume(struct pci_dev *pdev)
  5090. {
  5091. pci_set_power_state(pdev, PCI_D0);
  5092. pci_restore_state(pdev);
  5093. pci_enable_device(pdev);
  5094. pci_set_master(pdev);
  5095. }
  5096. int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
  5097. {
  5098. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  5099. int rc = 0;
  5100. rc = ata_host_suspend(host, mesg);
  5101. if (rc)
  5102. return rc;
  5103. ata_pci_device_do_suspend(pdev, mesg);
  5104. return 0;
  5105. }
  5106. int ata_pci_device_resume(struct pci_dev *pdev)
  5107. {
  5108. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  5109. ata_pci_device_do_resume(pdev);
  5110. ata_host_resume(host);
  5111. return 0;
  5112. }
  5113. #endif /* CONFIG_PCI */
  5114. static int __init ata_init(void)
  5115. {
  5116. ata_probe_timeout *= HZ;
  5117. ata_wq = create_workqueue("ata");
  5118. if (!ata_wq)
  5119. return -ENOMEM;
  5120. ata_aux_wq = create_singlethread_workqueue("ata_aux");
  5121. if (!ata_aux_wq) {
  5122. destroy_workqueue(ata_wq);
  5123. return -ENOMEM;
  5124. }
  5125. printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
  5126. return 0;
  5127. }
  5128. static void __exit ata_exit(void)
  5129. {
  5130. destroy_workqueue(ata_wq);
  5131. destroy_workqueue(ata_aux_wq);
  5132. }
  5133. subsys_initcall(ata_init);
  5134. module_exit(ata_exit);
  5135. static unsigned long ratelimit_time;
  5136. static DEFINE_SPINLOCK(ata_ratelimit_lock);
  5137. int ata_ratelimit(void)
  5138. {
  5139. int rc;
  5140. unsigned long flags;
  5141. spin_lock_irqsave(&ata_ratelimit_lock, flags);
  5142. if (time_after(jiffies, ratelimit_time)) {
  5143. rc = 1;
  5144. ratelimit_time = jiffies + (HZ/5);
  5145. } else
  5146. rc = 0;
  5147. spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
  5148. return rc;
  5149. }
  5150. /**
  5151. * ata_wait_register - wait until register value changes
  5152. * @reg: IO-mapped register
  5153. * @mask: Mask to apply to read register value
  5154. * @val: Wait condition
  5155. * @interval_msec: polling interval in milliseconds
  5156. * @timeout_msec: timeout in milliseconds
  5157. *
  5158. * Waiting for some bits of register to change is a common
  5159. * operation for ATA controllers. This function reads 32bit LE
  5160. * IO-mapped register @reg and tests for the following condition.
  5161. *
  5162. * (*@reg & mask) != val
  5163. *
  5164. * If the condition is met, it returns; otherwise, the process is
  5165. * repeated after @interval_msec until timeout.
  5166. *
  5167. * LOCKING:
  5168. * Kernel thread context (may sleep)
  5169. *
  5170. * RETURNS:
  5171. * The final register value.
  5172. */
  5173. u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
  5174. unsigned long interval_msec,
  5175. unsigned long timeout_msec)
  5176. {
  5177. unsigned long timeout;
  5178. u32 tmp;
  5179. tmp = ioread32(reg);
  5180. /* Calculate timeout _after_ the first read to make sure
  5181. * preceding writes reach the controller before starting to
  5182. * eat away the timeout.
  5183. */
  5184. timeout = jiffies + (timeout_msec * HZ) / 1000;
  5185. while ((tmp & mask) == val && time_before(jiffies, timeout)) {
  5186. msleep(interval_msec);
  5187. tmp = ioread32(reg);
  5188. }
  5189. return tmp;
  5190. }
  5191. /*
  5192. * Dummy port_ops
  5193. */
  5194. static void ata_dummy_noret(struct ata_port *ap) { }
  5195. static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
  5196. static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
  5197. static u8 ata_dummy_check_status(struct ata_port *ap)
  5198. {
  5199. return ATA_DRDY;
  5200. }
  5201. static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
  5202. {
  5203. return AC_ERR_SYSTEM;
  5204. }
  5205. const struct ata_port_operations ata_dummy_port_ops = {
  5206. .port_disable = ata_port_disable,
  5207. .check_status = ata_dummy_check_status,
  5208. .check_altstatus = ata_dummy_check_status,
  5209. .dev_select = ata_noop_dev_select,
  5210. .qc_prep = ata_noop_qc_prep,
  5211. .qc_issue = ata_dummy_qc_issue,
  5212. .freeze = ata_dummy_noret,
  5213. .thaw = ata_dummy_noret,
  5214. .error_handler = ata_dummy_noret,
  5215. .post_internal_cmd = ata_dummy_qc_noret,
  5216. .irq_clear = ata_dummy_noret,
  5217. .port_start = ata_dummy_ret0,
  5218. .port_stop = ata_dummy_noret,
  5219. };
  5220. /*
  5221. * libata is essentially a library of internal helper functions for
  5222. * low-level ATA host controller drivers. As such, the API/ABI is
  5223. * likely to change as new drivers are added and updated.
  5224. * Do not depend on ABI/API stability.
  5225. */
  5226. EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
  5227. EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
  5228. EXPORT_SYMBOL_GPL(sata_deb_timing_long);
  5229. EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
  5230. EXPORT_SYMBOL_GPL(ata_std_bios_param);
  5231. EXPORT_SYMBOL_GPL(ata_std_ports);
  5232. EXPORT_SYMBOL_GPL(ata_host_init);
  5233. EXPORT_SYMBOL_GPL(ata_device_add);
  5234. EXPORT_SYMBOL_GPL(ata_port_detach);
  5235. EXPORT_SYMBOL_GPL(ata_host_remove);
  5236. EXPORT_SYMBOL_GPL(ata_sg_init);
  5237. EXPORT_SYMBOL_GPL(ata_sg_init_one);
  5238. EXPORT_SYMBOL_GPL(ata_hsm_move);
  5239. EXPORT_SYMBOL_GPL(ata_qc_complete);
  5240. EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
  5241. EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
  5242. EXPORT_SYMBOL_GPL(ata_tf_load);
  5243. EXPORT_SYMBOL_GPL(ata_tf_read);
  5244. EXPORT_SYMBOL_GPL(ata_noop_dev_select);
  5245. EXPORT_SYMBOL_GPL(ata_std_dev_select);
  5246. EXPORT_SYMBOL_GPL(ata_tf_to_fis);
  5247. EXPORT_SYMBOL_GPL(ata_tf_from_fis);
  5248. EXPORT_SYMBOL_GPL(ata_check_status);
  5249. EXPORT_SYMBOL_GPL(ata_altstatus);
  5250. EXPORT_SYMBOL_GPL(ata_exec_command);
  5251. EXPORT_SYMBOL_GPL(ata_port_start);
  5252. EXPORT_SYMBOL_GPL(ata_port_stop);
  5253. EXPORT_SYMBOL_GPL(ata_host_stop);
  5254. EXPORT_SYMBOL_GPL(ata_interrupt);
  5255. EXPORT_SYMBOL_GPL(ata_mmio_data_xfer);
  5256. EXPORT_SYMBOL_GPL(ata_pio_data_xfer);
  5257. EXPORT_SYMBOL_GPL(ata_pio_data_xfer_noirq);
  5258. EXPORT_SYMBOL_GPL(ata_qc_prep);
  5259. EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
  5260. EXPORT_SYMBOL_GPL(ata_bmdma_setup);
  5261. EXPORT_SYMBOL_GPL(ata_bmdma_start);
  5262. EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
  5263. EXPORT_SYMBOL_GPL(ata_bmdma_status);
  5264. EXPORT_SYMBOL_GPL(ata_bmdma_stop);
  5265. EXPORT_SYMBOL_GPL(ata_bmdma_freeze);
  5266. EXPORT_SYMBOL_GPL(ata_bmdma_thaw);
  5267. EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh);
  5268. EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
  5269. EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
  5270. EXPORT_SYMBOL_GPL(ata_port_probe);
  5271. EXPORT_SYMBOL_GPL(sata_set_spd);
  5272. EXPORT_SYMBOL_GPL(sata_phy_debounce);
  5273. EXPORT_SYMBOL_GPL(sata_phy_resume);
  5274. EXPORT_SYMBOL_GPL(sata_phy_reset);
  5275. EXPORT_SYMBOL_GPL(__sata_phy_reset);
  5276. EXPORT_SYMBOL_GPL(ata_bus_reset);
  5277. EXPORT_SYMBOL_GPL(ata_std_prereset);
  5278. EXPORT_SYMBOL_GPL(ata_std_softreset);
  5279. EXPORT_SYMBOL_GPL(sata_port_hardreset);
  5280. EXPORT_SYMBOL_GPL(sata_std_hardreset);
  5281. EXPORT_SYMBOL_GPL(ata_std_postreset);
  5282. EXPORT_SYMBOL_GPL(ata_dev_classify);
  5283. EXPORT_SYMBOL_GPL(ata_dev_pair);
  5284. EXPORT_SYMBOL_GPL(ata_port_disable);
  5285. EXPORT_SYMBOL_GPL(ata_ratelimit);
  5286. EXPORT_SYMBOL_GPL(ata_wait_register);
  5287. EXPORT_SYMBOL_GPL(ata_busy_sleep);
  5288. EXPORT_SYMBOL_GPL(ata_port_queue_task);
  5289. EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
  5290. EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
  5291. EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
  5292. EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
  5293. EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
  5294. EXPORT_SYMBOL_GPL(ata_scsi_release);
  5295. EXPORT_SYMBOL_GPL(ata_host_intr);
  5296. EXPORT_SYMBOL_GPL(sata_scr_valid);
  5297. EXPORT_SYMBOL_GPL(sata_scr_read);
  5298. EXPORT_SYMBOL_GPL(sata_scr_write);
  5299. EXPORT_SYMBOL_GPL(sata_scr_write_flush);
  5300. EXPORT_SYMBOL_GPL(ata_port_online);
  5301. EXPORT_SYMBOL_GPL(ata_port_offline);
  5302. EXPORT_SYMBOL_GPL(ata_host_suspend);
  5303. EXPORT_SYMBOL_GPL(ata_host_resume);
  5304. EXPORT_SYMBOL_GPL(ata_id_string);
  5305. EXPORT_SYMBOL_GPL(ata_id_c_string);
  5306. EXPORT_SYMBOL_GPL(ata_device_blacklisted);
  5307. EXPORT_SYMBOL_GPL(ata_scsi_simulate);
  5308. EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
  5309. EXPORT_SYMBOL_GPL(ata_timing_compute);
  5310. EXPORT_SYMBOL_GPL(ata_timing_merge);
  5311. #ifdef CONFIG_PCI
  5312. EXPORT_SYMBOL_GPL(pci_test_config_bits);
  5313. EXPORT_SYMBOL_GPL(ata_pci_host_stop);
  5314. EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
  5315. EXPORT_SYMBOL_GPL(ata_pci_init_one);
  5316. EXPORT_SYMBOL_GPL(ata_pci_remove_one);
  5317. EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
  5318. EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
  5319. EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
  5320. EXPORT_SYMBOL_GPL(ata_pci_device_resume);
  5321. EXPORT_SYMBOL_GPL(ata_pci_default_filter);
  5322. EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
  5323. #endif /* CONFIG_PCI */
  5324. EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
  5325. EXPORT_SYMBOL_GPL(ata_scsi_device_resume);
  5326. EXPORT_SYMBOL_GPL(ata_eng_timeout);
  5327. EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
  5328. EXPORT_SYMBOL_GPL(ata_port_abort);
  5329. EXPORT_SYMBOL_GPL(ata_port_freeze);
  5330. EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
  5331. EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
  5332. EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
  5333. EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
  5334. EXPORT_SYMBOL_GPL(ata_do_eh);