libata-core.c 169 KB

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