libata-core.c 168 KB

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