libata-core.c 142 KB

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