libata-core.c 152 KB

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