libata-core.c 152 KB

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