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. * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
  990. * without filling any other registers
  991. */
  992. static int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
  993. {
  994. struct ata_taskfile tf;
  995. int err;
  996. ata_tf_init(dev, &tf);
  997. tf.command = cmd;
  998. tf.flags |= ATA_TFLAG_DEVICE;
  999. tf.protocol = ATA_PROT_NODATA;
  1000. err = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
  1001. if (err)
  1002. ata_dev_printk(dev, KERN_ERR, "%s: ata command failed: %d\n",
  1003. __FUNCTION__, err);
  1004. return err;
  1005. }
  1006. /**
  1007. * ata_pio_need_iordy - check if iordy needed
  1008. * @adev: ATA device
  1009. *
  1010. * Check if the current speed of the device requires IORDY. Used
  1011. * by various controllers for chip configuration.
  1012. */
  1013. unsigned int ata_pio_need_iordy(const struct ata_device *adev)
  1014. {
  1015. int pio;
  1016. int speed = adev->pio_mode - XFER_PIO_0;
  1017. if (speed < 2)
  1018. return 0;
  1019. if (speed > 2)
  1020. return 1;
  1021. /* If we have no drive specific rule, then PIO 2 is non IORDY */
  1022. if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
  1023. pio = adev->id[ATA_ID_EIDE_PIO];
  1024. /* Is the speed faster than the drive allows non IORDY ? */
  1025. if (pio) {
  1026. /* This is cycle times not frequency - watch the logic! */
  1027. if (pio > 240) /* PIO2 is 240nS per cycle */
  1028. return 1;
  1029. return 0;
  1030. }
  1031. }
  1032. return 0;
  1033. }
  1034. /**
  1035. * ata_dev_read_id - Read ID data from the specified device
  1036. * @dev: target device
  1037. * @p_class: pointer to class of the target device (may be changed)
  1038. * @post_reset: is this read ID post-reset?
  1039. * @id: buffer to read IDENTIFY data into
  1040. *
  1041. * Read ID data from the specified device. ATA_CMD_ID_ATA is
  1042. * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
  1043. * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
  1044. * for pre-ATA4 drives.
  1045. *
  1046. * LOCKING:
  1047. * Kernel thread context (may sleep)
  1048. *
  1049. * RETURNS:
  1050. * 0 on success, -errno otherwise.
  1051. */
  1052. int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
  1053. int post_reset, u16 *id)
  1054. {
  1055. struct ata_port *ap = dev->ap;
  1056. unsigned int class = *p_class;
  1057. struct ata_taskfile tf;
  1058. unsigned int err_mask = 0;
  1059. const char *reason;
  1060. int rc;
  1061. if (ata_msg_ctl(ap))
  1062. ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER, host %u, dev %u\n",
  1063. __FUNCTION__, ap->id, dev->devno);
  1064. ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
  1065. retry:
  1066. ata_tf_init(dev, &tf);
  1067. switch (class) {
  1068. case ATA_DEV_ATA:
  1069. tf.command = ATA_CMD_ID_ATA;
  1070. break;
  1071. case ATA_DEV_ATAPI:
  1072. tf.command = ATA_CMD_ID_ATAPI;
  1073. break;
  1074. default:
  1075. rc = -ENODEV;
  1076. reason = "unsupported class";
  1077. goto err_out;
  1078. }
  1079. tf.protocol = ATA_PROT_PIO;
  1080. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
  1081. id, sizeof(id[0]) * ATA_ID_WORDS);
  1082. if (err_mask) {
  1083. rc = -EIO;
  1084. reason = "I/O error";
  1085. goto err_out;
  1086. }
  1087. swap_buf_le16(id, ATA_ID_WORDS);
  1088. /* sanity check */
  1089. if ((class == ATA_DEV_ATA) != (ata_id_is_ata(id) | ata_id_is_cfa(id))) {
  1090. rc = -EINVAL;
  1091. reason = "device reports illegal type";
  1092. goto err_out;
  1093. }
  1094. if (post_reset && class == ATA_DEV_ATA) {
  1095. /*
  1096. * The exact sequence expected by certain pre-ATA4 drives is:
  1097. * SRST RESET
  1098. * IDENTIFY
  1099. * INITIALIZE DEVICE PARAMETERS
  1100. * anything else..
  1101. * Some drives were very specific about that exact sequence.
  1102. */
  1103. if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
  1104. err_mask = ata_dev_init_params(dev, id[3], id[6]);
  1105. if (err_mask) {
  1106. rc = -EIO;
  1107. reason = "INIT_DEV_PARAMS failed";
  1108. goto err_out;
  1109. }
  1110. /* current CHS translation info (id[53-58]) might be
  1111. * changed. reread the identify device info.
  1112. */
  1113. post_reset = 0;
  1114. goto retry;
  1115. }
  1116. }
  1117. *p_class = class;
  1118. return 0;
  1119. err_out:
  1120. if (ata_msg_warn(ap))
  1121. ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
  1122. "(%s, err_mask=0x%x)\n", reason, err_mask);
  1123. return rc;
  1124. }
  1125. static inline u8 ata_dev_knobble(struct ata_device *dev)
  1126. {
  1127. return ((dev->ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
  1128. }
  1129. static void ata_dev_config_ncq(struct ata_device *dev,
  1130. char *desc, size_t desc_sz)
  1131. {
  1132. struct ata_port *ap = dev->ap;
  1133. int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
  1134. if (!ata_id_has_ncq(dev->id)) {
  1135. desc[0] = '\0';
  1136. return;
  1137. }
  1138. if (ap->flags & ATA_FLAG_NCQ) {
  1139. hdepth = min(ap->host->can_queue, ATA_MAX_QUEUE - 1);
  1140. dev->flags |= ATA_DFLAG_NCQ;
  1141. }
  1142. if (hdepth >= ddepth)
  1143. snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
  1144. else
  1145. snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
  1146. }
  1147. /**
  1148. * ata_dev_configure - Configure the specified ATA/ATAPI device
  1149. * @dev: Target device to configure
  1150. * @print_info: Enable device info printout
  1151. *
  1152. * Configure @dev according to @dev->id. Generic and low-level
  1153. * driver specific fixups are also applied.
  1154. *
  1155. * LOCKING:
  1156. * Kernel thread context (may sleep)
  1157. *
  1158. * RETURNS:
  1159. * 0 on success, -errno otherwise
  1160. */
  1161. int ata_dev_configure(struct ata_device *dev, int print_info)
  1162. {
  1163. struct ata_port *ap = dev->ap;
  1164. const u16 *id = dev->id;
  1165. unsigned int xfer_mask;
  1166. int i, rc;
  1167. if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
  1168. ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT (host %u, dev %u) -- nodev\n",
  1169. __FUNCTION__, ap->id, dev->devno);
  1170. return 0;
  1171. }
  1172. if (ata_msg_probe(ap))
  1173. ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER, host %u, dev %u\n",
  1174. __FUNCTION__, ap->id, dev->devno);
  1175. /* print device capabilities */
  1176. if (ata_msg_probe(ap))
  1177. ata_dev_printk(dev, KERN_DEBUG, "%s: cfg 49:%04x 82:%04x 83:%04x "
  1178. "84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
  1179. __FUNCTION__,
  1180. id[49], id[82], id[83], id[84],
  1181. id[85], id[86], id[87], id[88]);
  1182. /* initialize to-be-configured parameters */
  1183. dev->flags &= ~ATA_DFLAG_CFG_MASK;
  1184. dev->max_sectors = 0;
  1185. dev->cdb_len = 0;
  1186. dev->n_sectors = 0;
  1187. dev->cylinders = 0;
  1188. dev->heads = 0;
  1189. dev->sectors = 0;
  1190. /*
  1191. * common ATA, ATAPI feature tests
  1192. */
  1193. /* find max transfer mode; for printk only */
  1194. xfer_mask = ata_id_xfermask(id);
  1195. if (ata_msg_probe(ap))
  1196. ata_dump_id(id);
  1197. /* ATA-specific feature tests */
  1198. if (dev->class == ATA_DEV_ATA) {
  1199. dev->n_sectors = ata_id_n_sectors(id);
  1200. if (ata_id_has_lba(id)) {
  1201. const char *lba_desc;
  1202. char ncq_desc[20];
  1203. lba_desc = "LBA";
  1204. dev->flags |= ATA_DFLAG_LBA;
  1205. if (ata_id_has_lba48(id)) {
  1206. dev->flags |= ATA_DFLAG_LBA48;
  1207. lba_desc = "LBA48";
  1208. }
  1209. /* config NCQ */
  1210. ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
  1211. /* print device info to dmesg */
  1212. if (ata_msg_info(ap))
  1213. ata_dev_printk(dev, KERN_INFO, "ATA-%d, "
  1214. "max %s, %Lu sectors: %s %s\n",
  1215. ata_id_major_version(id),
  1216. ata_mode_string(xfer_mask),
  1217. (unsigned long long)dev->n_sectors,
  1218. lba_desc, ncq_desc);
  1219. } else {
  1220. /* CHS */
  1221. /* Default translation */
  1222. dev->cylinders = id[1];
  1223. dev->heads = id[3];
  1224. dev->sectors = id[6];
  1225. if (ata_id_current_chs_valid(id)) {
  1226. /* Current CHS translation is valid. */
  1227. dev->cylinders = id[54];
  1228. dev->heads = id[55];
  1229. dev->sectors = id[56];
  1230. }
  1231. /* print device info to dmesg */
  1232. if (ata_msg_info(ap))
  1233. ata_dev_printk(dev, KERN_INFO, "ATA-%d, "
  1234. "max %s, %Lu sectors: CHS %u/%u/%u\n",
  1235. ata_id_major_version(id),
  1236. ata_mode_string(xfer_mask),
  1237. (unsigned long long)dev->n_sectors,
  1238. dev->cylinders, dev->heads, dev->sectors);
  1239. }
  1240. if (dev->id[59] & 0x100) {
  1241. dev->multi_count = dev->id[59] & 0xff;
  1242. if (ata_msg_info(ap))
  1243. ata_dev_printk(dev, KERN_INFO, "ata%u: dev %u multi count %u\n",
  1244. ap->id, dev->devno, dev->multi_count);
  1245. }
  1246. dev->cdb_len = 16;
  1247. }
  1248. /* ATAPI-specific feature tests */
  1249. else if (dev->class == ATA_DEV_ATAPI) {
  1250. char *cdb_intr_string = "";
  1251. rc = atapi_cdb_len(id);
  1252. if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
  1253. if (ata_msg_warn(ap))
  1254. ata_dev_printk(dev, KERN_WARNING,
  1255. "unsupported CDB len\n");
  1256. rc = -EINVAL;
  1257. goto err_out_nosup;
  1258. }
  1259. dev->cdb_len = (unsigned int) rc;
  1260. if (ata_id_cdb_intr(dev->id)) {
  1261. dev->flags |= ATA_DFLAG_CDB_INTR;
  1262. cdb_intr_string = ", CDB intr";
  1263. }
  1264. /* print device info to dmesg */
  1265. if (ata_msg_info(ap))
  1266. ata_dev_printk(dev, KERN_INFO, "ATAPI, max %s%s\n",
  1267. ata_mode_string(xfer_mask),
  1268. cdb_intr_string);
  1269. }
  1270. ap->host->max_cmd_len = 0;
  1271. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1272. ap->host->max_cmd_len = max_t(unsigned int,
  1273. ap->host->max_cmd_len,
  1274. ap->device[i].cdb_len);
  1275. /* limit bridge transfers to udma5, 200 sectors */
  1276. if (ata_dev_knobble(dev)) {
  1277. if (ata_msg_info(ap))
  1278. ata_dev_printk(dev, KERN_INFO,
  1279. "applying bridge limits\n");
  1280. dev->udma_mask &= ATA_UDMA5;
  1281. dev->max_sectors = ATA_MAX_SECTORS;
  1282. }
  1283. if (ap->ops->dev_config)
  1284. ap->ops->dev_config(ap, dev);
  1285. if (ata_msg_probe(ap))
  1286. ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n",
  1287. __FUNCTION__, ata_chk_status(ap));
  1288. return 0;
  1289. err_out_nosup:
  1290. if (ata_msg_probe(ap))
  1291. ata_dev_printk(dev, KERN_DEBUG,
  1292. "%s: EXIT, err\n", __FUNCTION__);
  1293. return rc;
  1294. }
  1295. /**
  1296. * ata_bus_probe - Reset and probe ATA bus
  1297. * @ap: Bus to probe
  1298. *
  1299. * Master ATA bus probing function. Initiates a hardware-dependent
  1300. * bus reset, then attempts to identify any devices found on
  1301. * the bus.
  1302. *
  1303. * LOCKING:
  1304. * PCI/etc. bus probe sem.
  1305. *
  1306. * RETURNS:
  1307. * Zero on success, negative errno otherwise.
  1308. */
  1309. static int ata_bus_probe(struct ata_port *ap)
  1310. {
  1311. unsigned int classes[ATA_MAX_DEVICES];
  1312. int tries[ATA_MAX_DEVICES];
  1313. int i, rc, down_xfermask;
  1314. struct ata_device *dev;
  1315. ata_port_probe(ap);
  1316. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1317. tries[i] = ATA_PROBE_MAX_TRIES;
  1318. retry:
  1319. down_xfermask = 0;
  1320. /* reset and determine device classes */
  1321. ap->ops->phy_reset(ap);
  1322. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1323. dev = &ap->device[i];
  1324. if (!(ap->flags & ATA_FLAG_DISABLED) &&
  1325. dev->class != ATA_DEV_UNKNOWN)
  1326. classes[dev->devno] = dev->class;
  1327. else
  1328. classes[dev->devno] = ATA_DEV_NONE;
  1329. dev->class = ATA_DEV_UNKNOWN;
  1330. }
  1331. ata_port_probe(ap);
  1332. /* after the reset the device state is PIO 0 and the controller
  1333. state is undefined. Record the mode */
  1334. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1335. ap->device[i].pio_mode = XFER_PIO_0;
  1336. /* read IDENTIFY page and configure devices */
  1337. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1338. dev = &ap->device[i];
  1339. if (tries[i])
  1340. dev->class = classes[i];
  1341. if (!ata_dev_enabled(dev))
  1342. continue;
  1343. rc = ata_dev_read_id(dev, &dev->class, 1, dev->id);
  1344. if (rc)
  1345. goto fail;
  1346. rc = ata_dev_configure(dev, 1);
  1347. if (rc)
  1348. goto fail;
  1349. }
  1350. /* configure transfer mode */
  1351. rc = ata_set_mode(ap, &dev);
  1352. if (rc) {
  1353. down_xfermask = 1;
  1354. goto fail;
  1355. }
  1356. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1357. if (ata_dev_enabled(&ap->device[i]))
  1358. return 0;
  1359. /* no device present, disable port */
  1360. ata_port_disable(ap);
  1361. ap->ops->port_disable(ap);
  1362. return -ENODEV;
  1363. fail:
  1364. switch (rc) {
  1365. case -EINVAL:
  1366. case -ENODEV:
  1367. tries[dev->devno] = 0;
  1368. break;
  1369. case -EIO:
  1370. sata_down_spd_limit(ap);
  1371. /* fall through */
  1372. default:
  1373. tries[dev->devno]--;
  1374. if (down_xfermask &&
  1375. ata_down_xfermask_limit(dev, tries[dev->devno] == 1))
  1376. tries[dev->devno] = 0;
  1377. }
  1378. if (!tries[dev->devno]) {
  1379. ata_down_xfermask_limit(dev, 1);
  1380. ata_dev_disable(dev);
  1381. }
  1382. goto retry;
  1383. }
  1384. /**
  1385. * ata_port_probe - Mark port as enabled
  1386. * @ap: Port for which we indicate enablement
  1387. *
  1388. * Modify @ap data structure such that the system
  1389. * thinks that the entire port is enabled.
  1390. *
  1391. * LOCKING: host_set lock, or some other form of
  1392. * serialization.
  1393. */
  1394. void ata_port_probe(struct ata_port *ap)
  1395. {
  1396. ap->flags &= ~ATA_FLAG_DISABLED;
  1397. }
  1398. /**
  1399. * sata_print_link_status - Print SATA link status
  1400. * @ap: SATA port to printk link status about
  1401. *
  1402. * This function prints link speed and status of a SATA link.
  1403. *
  1404. * LOCKING:
  1405. * None.
  1406. */
  1407. static void sata_print_link_status(struct ata_port *ap)
  1408. {
  1409. u32 sstatus, scontrol, tmp;
  1410. if (sata_scr_read(ap, SCR_STATUS, &sstatus))
  1411. return;
  1412. sata_scr_read(ap, SCR_CONTROL, &scontrol);
  1413. if (ata_port_online(ap)) {
  1414. tmp = (sstatus >> 4) & 0xf;
  1415. ata_port_printk(ap, KERN_INFO,
  1416. "SATA link up %s (SStatus %X SControl %X)\n",
  1417. sata_spd_string(tmp), sstatus, scontrol);
  1418. } else {
  1419. ata_port_printk(ap, KERN_INFO,
  1420. "SATA link down (SStatus %X SControl %X)\n",
  1421. sstatus, scontrol);
  1422. }
  1423. }
  1424. /**
  1425. * __sata_phy_reset - Wake/reset a low-level SATA PHY
  1426. * @ap: SATA port associated with target SATA PHY.
  1427. *
  1428. * This function issues commands to standard SATA Sxxx
  1429. * PHY registers, to wake up the phy (and device), and
  1430. * clear any reset condition.
  1431. *
  1432. * LOCKING:
  1433. * PCI/etc. bus probe sem.
  1434. *
  1435. */
  1436. void __sata_phy_reset(struct ata_port *ap)
  1437. {
  1438. u32 sstatus;
  1439. unsigned long timeout = jiffies + (HZ * 5);
  1440. if (ap->flags & ATA_FLAG_SATA_RESET) {
  1441. /* issue phy wake/reset */
  1442. sata_scr_write_flush(ap, SCR_CONTROL, 0x301);
  1443. /* Couldn't find anything in SATA I/II specs, but
  1444. * AHCI-1.1 10.4.2 says at least 1 ms. */
  1445. mdelay(1);
  1446. }
  1447. /* phy wake/clear reset */
  1448. sata_scr_write_flush(ap, SCR_CONTROL, 0x300);
  1449. /* wait for phy to become ready, if necessary */
  1450. do {
  1451. msleep(200);
  1452. sata_scr_read(ap, SCR_STATUS, &sstatus);
  1453. if ((sstatus & 0xf) != 1)
  1454. break;
  1455. } while (time_before(jiffies, timeout));
  1456. /* print link status */
  1457. sata_print_link_status(ap);
  1458. /* TODO: phy layer with polling, timeouts, etc. */
  1459. if (!ata_port_offline(ap))
  1460. ata_port_probe(ap);
  1461. else
  1462. ata_port_disable(ap);
  1463. if (ap->flags & ATA_FLAG_DISABLED)
  1464. return;
  1465. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  1466. ata_port_disable(ap);
  1467. return;
  1468. }
  1469. ap->cbl = ATA_CBL_SATA;
  1470. }
  1471. /**
  1472. * sata_phy_reset - Reset SATA bus.
  1473. * @ap: SATA port associated with target SATA PHY.
  1474. *
  1475. * This function resets the SATA bus, and then probes
  1476. * the bus for devices.
  1477. *
  1478. * LOCKING:
  1479. * PCI/etc. bus probe sem.
  1480. *
  1481. */
  1482. void sata_phy_reset(struct ata_port *ap)
  1483. {
  1484. __sata_phy_reset(ap);
  1485. if (ap->flags & ATA_FLAG_DISABLED)
  1486. return;
  1487. ata_bus_reset(ap);
  1488. }
  1489. /**
  1490. * ata_dev_pair - return other device on cable
  1491. * @adev: device
  1492. *
  1493. * Obtain the other device on the same cable, or if none is
  1494. * present NULL is returned
  1495. */
  1496. struct ata_device *ata_dev_pair(struct ata_device *adev)
  1497. {
  1498. struct ata_port *ap = adev->ap;
  1499. struct ata_device *pair = &ap->device[1 - adev->devno];
  1500. if (!ata_dev_enabled(pair))
  1501. return NULL;
  1502. return pair;
  1503. }
  1504. /**
  1505. * ata_port_disable - Disable port.
  1506. * @ap: Port to be disabled.
  1507. *
  1508. * Modify @ap data structure such that the system
  1509. * thinks that the entire port is disabled, and should
  1510. * never attempt to probe or communicate with devices
  1511. * on this port.
  1512. *
  1513. * LOCKING: host_set lock, or some other form of
  1514. * serialization.
  1515. */
  1516. void ata_port_disable(struct ata_port *ap)
  1517. {
  1518. ap->device[0].class = ATA_DEV_NONE;
  1519. ap->device[1].class = ATA_DEV_NONE;
  1520. ap->flags |= ATA_FLAG_DISABLED;
  1521. }
  1522. /**
  1523. * sata_down_spd_limit - adjust SATA spd limit downward
  1524. * @ap: Port to adjust SATA spd limit for
  1525. *
  1526. * Adjust SATA spd limit of @ap downward. Note that this
  1527. * function only adjusts the limit. The change must be applied
  1528. * using sata_set_spd().
  1529. *
  1530. * LOCKING:
  1531. * Inherited from caller.
  1532. *
  1533. * RETURNS:
  1534. * 0 on success, negative errno on failure
  1535. */
  1536. int sata_down_spd_limit(struct ata_port *ap)
  1537. {
  1538. u32 sstatus, spd, mask;
  1539. int rc, highbit;
  1540. rc = sata_scr_read(ap, SCR_STATUS, &sstatus);
  1541. if (rc)
  1542. return rc;
  1543. mask = ap->sata_spd_limit;
  1544. if (mask <= 1)
  1545. return -EINVAL;
  1546. highbit = fls(mask) - 1;
  1547. mask &= ~(1 << highbit);
  1548. spd = (sstatus >> 4) & 0xf;
  1549. if (spd <= 1)
  1550. return -EINVAL;
  1551. spd--;
  1552. mask &= (1 << spd) - 1;
  1553. if (!mask)
  1554. return -EINVAL;
  1555. ap->sata_spd_limit = mask;
  1556. ata_port_printk(ap, KERN_WARNING, "limiting SATA link speed to %s\n",
  1557. sata_spd_string(fls(mask)));
  1558. return 0;
  1559. }
  1560. static int __sata_set_spd_needed(struct ata_port *ap, u32 *scontrol)
  1561. {
  1562. u32 spd, limit;
  1563. if (ap->sata_spd_limit == UINT_MAX)
  1564. limit = 0;
  1565. else
  1566. limit = fls(ap->sata_spd_limit);
  1567. spd = (*scontrol >> 4) & 0xf;
  1568. *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
  1569. return spd != limit;
  1570. }
  1571. /**
  1572. * sata_set_spd_needed - is SATA spd configuration needed
  1573. * @ap: Port in question
  1574. *
  1575. * Test whether the spd limit in SControl matches
  1576. * @ap->sata_spd_limit. This function is used to determine
  1577. * whether hardreset is necessary to apply SATA spd
  1578. * configuration.
  1579. *
  1580. * LOCKING:
  1581. * Inherited from caller.
  1582. *
  1583. * RETURNS:
  1584. * 1 if SATA spd configuration is needed, 0 otherwise.
  1585. */
  1586. int sata_set_spd_needed(struct ata_port *ap)
  1587. {
  1588. u32 scontrol;
  1589. if (sata_scr_read(ap, SCR_CONTROL, &scontrol))
  1590. return 0;
  1591. return __sata_set_spd_needed(ap, &scontrol);
  1592. }
  1593. /**
  1594. * sata_set_spd - set SATA spd according to spd limit
  1595. * @ap: Port to set SATA spd for
  1596. *
  1597. * Set SATA spd of @ap according to sata_spd_limit.
  1598. *
  1599. * LOCKING:
  1600. * Inherited from caller.
  1601. *
  1602. * RETURNS:
  1603. * 0 if spd doesn't need to be changed, 1 if spd has been
  1604. * changed. Negative errno if SCR registers are inaccessible.
  1605. */
  1606. int sata_set_spd(struct ata_port *ap)
  1607. {
  1608. u32 scontrol;
  1609. int rc;
  1610. if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
  1611. return rc;
  1612. if (!__sata_set_spd_needed(ap, &scontrol))
  1613. return 0;
  1614. if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
  1615. return rc;
  1616. return 1;
  1617. }
  1618. /*
  1619. * This mode timing computation functionality is ported over from
  1620. * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
  1621. */
  1622. /*
  1623. * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
  1624. * These were taken from ATA/ATAPI-6 standard, rev 0a, except
  1625. * for PIO 5, which is a nonstandard extension and UDMA6, which
  1626. * is currently supported only by Maxtor drives.
  1627. */
  1628. static const struct ata_timing ata_timing[] = {
  1629. { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
  1630. { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
  1631. { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
  1632. { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
  1633. { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
  1634. { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
  1635. { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
  1636. /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
  1637. { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
  1638. { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
  1639. { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
  1640. { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
  1641. { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
  1642. { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
  1643. /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
  1644. { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
  1645. { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
  1646. { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
  1647. { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
  1648. { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
  1649. /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
  1650. { 0xFF }
  1651. };
  1652. #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
  1653. #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
  1654. static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
  1655. {
  1656. q->setup = EZ(t->setup * 1000, T);
  1657. q->act8b = EZ(t->act8b * 1000, T);
  1658. q->rec8b = EZ(t->rec8b * 1000, T);
  1659. q->cyc8b = EZ(t->cyc8b * 1000, T);
  1660. q->active = EZ(t->active * 1000, T);
  1661. q->recover = EZ(t->recover * 1000, T);
  1662. q->cycle = EZ(t->cycle * 1000, T);
  1663. q->udma = EZ(t->udma * 1000, UT);
  1664. }
  1665. void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
  1666. struct ata_timing *m, unsigned int what)
  1667. {
  1668. if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
  1669. if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
  1670. if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
  1671. if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
  1672. if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
  1673. if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
  1674. if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
  1675. if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
  1676. }
  1677. static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
  1678. {
  1679. const struct ata_timing *t;
  1680. for (t = ata_timing; t->mode != speed; t++)
  1681. if (t->mode == 0xFF)
  1682. return NULL;
  1683. return t;
  1684. }
  1685. int ata_timing_compute(struct ata_device *adev, unsigned short speed,
  1686. struct ata_timing *t, int T, int UT)
  1687. {
  1688. const struct ata_timing *s;
  1689. struct ata_timing p;
  1690. /*
  1691. * Find the mode.
  1692. */
  1693. if (!(s = ata_timing_find_mode(speed)))
  1694. return -EINVAL;
  1695. memcpy(t, s, sizeof(*s));
  1696. /*
  1697. * If the drive is an EIDE drive, it can tell us it needs extended
  1698. * PIO/MW_DMA cycle timing.
  1699. */
  1700. if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
  1701. memset(&p, 0, sizeof(p));
  1702. if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
  1703. if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
  1704. else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
  1705. } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
  1706. p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
  1707. }
  1708. ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
  1709. }
  1710. /*
  1711. * Convert the timing to bus clock counts.
  1712. */
  1713. ata_timing_quantize(t, t, T, UT);
  1714. /*
  1715. * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
  1716. * S.M.A.R.T * and some other commands. We have to ensure that the
  1717. * DMA cycle timing is slower/equal than the fastest PIO timing.
  1718. */
  1719. if (speed > XFER_PIO_4) {
  1720. ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
  1721. ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
  1722. }
  1723. /*
  1724. * Lengthen active & recovery time so that cycle time is correct.
  1725. */
  1726. if (t->act8b + t->rec8b < t->cyc8b) {
  1727. t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
  1728. t->rec8b = t->cyc8b - t->act8b;
  1729. }
  1730. if (t->active + t->recover < t->cycle) {
  1731. t->active += (t->cycle - (t->active + t->recover)) / 2;
  1732. t->recover = t->cycle - t->active;
  1733. }
  1734. return 0;
  1735. }
  1736. /**
  1737. * ata_down_xfermask_limit - adjust dev xfer masks downward
  1738. * @dev: Device to adjust xfer masks
  1739. * @force_pio0: Force PIO0
  1740. *
  1741. * Adjust xfer masks of @dev downward. Note that this function
  1742. * does not apply the change. Invoking ata_set_mode() afterwards
  1743. * will apply the limit.
  1744. *
  1745. * LOCKING:
  1746. * Inherited from caller.
  1747. *
  1748. * RETURNS:
  1749. * 0 on success, negative errno on failure
  1750. */
  1751. int ata_down_xfermask_limit(struct ata_device *dev, int force_pio0)
  1752. {
  1753. unsigned long xfer_mask;
  1754. int highbit;
  1755. xfer_mask = ata_pack_xfermask(dev->pio_mask, dev->mwdma_mask,
  1756. dev->udma_mask);
  1757. if (!xfer_mask)
  1758. goto fail;
  1759. /* don't gear down to MWDMA from UDMA, go directly to PIO */
  1760. if (xfer_mask & ATA_MASK_UDMA)
  1761. xfer_mask &= ~ATA_MASK_MWDMA;
  1762. highbit = fls(xfer_mask) - 1;
  1763. xfer_mask &= ~(1 << highbit);
  1764. if (force_pio0)
  1765. xfer_mask &= 1 << ATA_SHIFT_PIO;
  1766. if (!xfer_mask)
  1767. goto fail;
  1768. ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
  1769. &dev->udma_mask);
  1770. ata_dev_printk(dev, KERN_WARNING, "limiting speed to %s\n",
  1771. ata_mode_string(xfer_mask));
  1772. return 0;
  1773. fail:
  1774. return -EINVAL;
  1775. }
  1776. static int ata_dev_set_mode(struct ata_device *dev)
  1777. {
  1778. unsigned int err_mask;
  1779. int rc;
  1780. dev->flags &= ~ATA_DFLAG_PIO;
  1781. if (dev->xfer_shift == ATA_SHIFT_PIO)
  1782. dev->flags |= ATA_DFLAG_PIO;
  1783. err_mask = ata_dev_set_xfermode(dev);
  1784. if (err_mask) {
  1785. ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
  1786. "(err_mask=0x%x)\n", err_mask);
  1787. return -EIO;
  1788. }
  1789. rc = ata_dev_revalidate(dev, 0);
  1790. if (rc)
  1791. return rc;
  1792. DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
  1793. dev->xfer_shift, (int)dev->xfer_mode);
  1794. ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
  1795. ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
  1796. return 0;
  1797. }
  1798. /**
  1799. * ata_set_mode - Program timings and issue SET FEATURES - XFER
  1800. * @ap: port on which timings will be programmed
  1801. * @r_failed_dev: out paramter for failed device
  1802. *
  1803. * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
  1804. * ata_set_mode() fails, pointer to the failing device is
  1805. * returned in @r_failed_dev.
  1806. *
  1807. * LOCKING:
  1808. * PCI/etc. bus probe sem.
  1809. *
  1810. * RETURNS:
  1811. * 0 on success, negative errno otherwise
  1812. */
  1813. int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
  1814. {
  1815. struct ata_device *dev;
  1816. int i, rc = 0, used_dma = 0, found = 0;
  1817. /* has private set_mode? */
  1818. if (ap->ops->set_mode) {
  1819. /* FIXME: make ->set_mode handle no device case and
  1820. * return error code and failing device on failure.
  1821. */
  1822. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1823. if (ata_dev_enabled(&ap->device[i])) {
  1824. ap->ops->set_mode(ap);
  1825. break;
  1826. }
  1827. }
  1828. return 0;
  1829. }
  1830. /* step 1: calculate xfer_mask */
  1831. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1832. unsigned int pio_mask, dma_mask;
  1833. dev = &ap->device[i];
  1834. if (!ata_dev_enabled(dev))
  1835. continue;
  1836. ata_dev_xfermask(dev);
  1837. pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
  1838. dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
  1839. dev->pio_mode = ata_xfer_mask2mode(pio_mask);
  1840. dev->dma_mode = ata_xfer_mask2mode(dma_mask);
  1841. found = 1;
  1842. if (dev->dma_mode)
  1843. used_dma = 1;
  1844. }
  1845. if (!found)
  1846. goto out;
  1847. /* step 2: always set host PIO timings */
  1848. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1849. dev = &ap->device[i];
  1850. if (!ata_dev_enabled(dev))
  1851. continue;
  1852. if (!dev->pio_mode) {
  1853. ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
  1854. rc = -EINVAL;
  1855. goto out;
  1856. }
  1857. dev->xfer_mode = dev->pio_mode;
  1858. dev->xfer_shift = ATA_SHIFT_PIO;
  1859. if (ap->ops->set_piomode)
  1860. ap->ops->set_piomode(ap, dev);
  1861. }
  1862. /* step 3: set host DMA timings */
  1863. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1864. dev = &ap->device[i];
  1865. if (!ata_dev_enabled(dev) || !dev->dma_mode)
  1866. continue;
  1867. dev->xfer_mode = dev->dma_mode;
  1868. dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
  1869. if (ap->ops->set_dmamode)
  1870. ap->ops->set_dmamode(ap, dev);
  1871. }
  1872. /* step 4: update devices' xfer mode */
  1873. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1874. dev = &ap->device[i];
  1875. if (!ata_dev_enabled(dev))
  1876. continue;
  1877. rc = ata_dev_set_mode(dev);
  1878. if (rc)
  1879. goto out;
  1880. }
  1881. /* Record simplex status. If we selected DMA then the other
  1882. * host channels are not permitted to do so.
  1883. */
  1884. if (used_dma && (ap->host_set->flags & ATA_HOST_SIMPLEX))
  1885. ap->host_set->simplex_claimed = 1;
  1886. /* step5: chip specific finalisation */
  1887. if (ap->ops->post_set_mode)
  1888. ap->ops->post_set_mode(ap);
  1889. out:
  1890. if (rc)
  1891. *r_failed_dev = dev;
  1892. return rc;
  1893. }
  1894. /**
  1895. * ata_tf_to_host - issue ATA taskfile to host controller
  1896. * @ap: port to which command is being issued
  1897. * @tf: ATA taskfile register set
  1898. *
  1899. * Issues ATA taskfile register set to ATA host controller,
  1900. * with proper synchronization with interrupt handler and
  1901. * other threads.
  1902. *
  1903. * LOCKING:
  1904. * spin_lock_irqsave(host_set lock)
  1905. */
  1906. static inline void ata_tf_to_host(struct ata_port *ap,
  1907. const struct ata_taskfile *tf)
  1908. {
  1909. ap->ops->tf_load(ap, tf);
  1910. ap->ops->exec_command(ap, tf);
  1911. }
  1912. /**
  1913. * ata_busy_sleep - sleep until BSY clears, or timeout
  1914. * @ap: port containing status register to be polled
  1915. * @tmout_pat: impatience timeout
  1916. * @tmout: overall timeout
  1917. *
  1918. * Sleep until ATA Status register bit BSY clears,
  1919. * or a timeout occurs.
  1920. *
  1921. * LOCKING: None.
  1922. */
  1923. unsigned int ata_busy_sleep (struct ata_port *ap,
  1924. unsigned long tmout_pat, unsigned long tmout)
  1925. {
  1926. unsigned long timer_start, timeout;
  1927. u8 status;
  1928. status = ata_busy_wait(ap, ATA_BUSY, 300);
  1929. timer_start = jiffies;
  1930. timeout = timer_start + tmout_pat;
  1931. while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
  1932. msleep(50);
  1933. status = ata_busy_wait(ap, ATA_BUSY, 3);
  1934. }
  1935. if (status & ATA_BUSY)
  1936. ata_port_printk(ap, KERN_WARNING,
  1937. "port is slow to respond, please be patient\n");
  1938. timeout = timer_start + tmout;
  1939. while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
  1940. msleep(50);
  1941. status = ata_chk_status(ap);
  1942. }
  1943. if (status & ATA_BUSY) {
  1944. ata_port_printk(ap, KERN_ERR, "port failed to respond "
  1945. "(%lu secs)\n", tmout / HZ);
  1946. return 1;
  1947. }
  1948. return 0;
  1949. }
  1950. static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
  1951. {
  1952. struct ata_ioports *ioaddr = &ap->ioaddr;
  1953. unsigned int dev0 = devmask & (1 << 0);
  1954. unsigned int dev1 = devmask & (1 << 1);
  1955. unsigned long timeout;
  1956. /* if device 0 was found in ata_devchk, wait for its
  1957. * BSY bit to clear
  1958. */
  1959. if (dev0)
  1960. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  1961. /* if device 1 was found in ata_devchk, wait for
  1962. * register access, then wait for BSY to clear
  1963. */
  1964. timeout = jiffies + ATA_TMOUT_BOOT;
  1965. while (dev1) {
  1966. u8 nsect, lbal;
  1967. ap->ops->dev_select(ap, 1);
  1968. if (ap->flags & ATA_FLAG_MMIO) {
  1969. nsect = readb((void __iomem *) ioaddr->nsect_addr);
  1970. lbal = readb((void __iomem *) ioaddr->lbal_addr);
  1971. } else {
  1972. nsect = inb(ioaddr->nsect_addr);
  1973. lbal = inb(ioaddr->lbal_addr);
  1974. }
  1975. if ((nsect == 1) && (lbal == 1))
  1976. break;
  1977. if (time_after(jiffies, timeout)) {
  1978. dev1 = 0;
  1979. break;
  1980. }
  1981. msleep(50); /* give drive a breather */
  1982. }
  1983. if (dev1)
  1984. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  1985. /* is all this really necessary? */
  1986. ap->ops->dev_select(ap, 0);
  1987. if (dev1)
  1988. ap->ops->dev_select(ap, 1);
  1989. if (dev0)
  1990. ap->ops->dev_select(ap, 0);
  1991. }
  1992. static unsigned int ata_bus_softreset(struct ata_port *ap,
  1993. unsigned int devmask)
  1994. {
  1995. struct ata_ioports *ioaddr = &ap->ioaddr;
  1996. DPRINTK("ata%u: bus reset via SRST\n", ap->id);
  1997. /* software reset. causes dev0 to be selected */
  1998. if (ap->flags & ATA_FLAG_MMIO) {
  1999. writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
  2000. udelay(20); /* FIXME: flush */
  2001. writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
  2002. udelay(20); /* FIXME: flush */
  2003. writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
  2004. } else {
  2005. outb(ap->ctl, ioaddr->ctl_addr);
  2006. udelay(10);
  2007. outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
  2008. udelay(10);
  2009. outb(ap->ctl, ioaddr->ctl_addr);
  2010. }
  2011. /* spec mandates ">= 2ms" before checking status.
  2012. * We wait 150ms, because that was the magic delay used for
  2013. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  2014. * between when the ATA command register is written, and then
  2015. * status is checked. Because waiting for "a while" before
  2016. * checking status is fine, post SRST, we perform this magic
  2017. * delay here as well.
  2018. *
  2019. * Old drivers/ide uses the 2mS rule and then waits for ready
  2020. */
  2021. msleep(150);
  2022. /* Before we perform post reset processing we want to see if
  2023. * the bus shows 0xFF because the odd clown forgets the D7
  2024. * pulldown resistor.
  2025. */
  2026. if (ata_check_status(ap) == 0xFF) {
  2027. ata_port_printk(ap, KERN_ERR, "SRST failed (status 0xFF)\n");
  2028. return AC_ERR_OTHER;
  2029. }
  2030. ata_bus_post_reset(ap, devmask);
  2031. return 0;
  2032. }
  2033. /**
  2034. * ata_bus_reset - reset host port and associated ATA channel
  2035. * @ap: port to reset
  2036. *
  2037. * This is typically the first time we actually start issuing
  2038. * commands to the ATA channel. We wait for BSY to clear, then
  2039. * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
  2040. * result. Determine what devices, if any, are on the channel
  2041. * by looking at the device 0/1 error register. Look at the signature
  2042. * stored in each device's taskfile registers, to determine if
  2043. * the device is ATA or ATAPI.
  2044. *
  2045. * LOCKING:
  2046. * PCI/etc. bus probe sem.
  2047. * Obtains host_set lock.
  2048. *
  2049. * SIDE EFFECTS:
  2050. * Sets ATA_FLAG_DISABLED if bus reset fails.
  2051. */
  2052. void ata_bus_reset(struct ata_port *ap)
  2053. {
  2054. struct ata_ioports *ioaddr = &ap->ioaddr;
  2055. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  2056. u8 err;
  2057. unsigned int dev0, dev1 = 0, devmask = 0;
  2058. DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
  2059. /* determine if device 0/1 are present */
  2060. if (ap->flags & ATA_FLAG_SATA_RESET)
  2061. dev0 = 1;
  2062. else {
  2063. dev0 = ata_devchk(ap, 0);
  2064. if (slave_possible)
  2065. dev1 = ata_devchk(ap, 1);
  2066. }
  2067. if (dev0)
  2068. devmask |= (1 << 0);
  2069. if (dev1)
  2070. devmask |= (1 << 1);
  2071. /* select device 0 again */
  2072. ap->ops->dev_select(ap, 0);
  2073. /* issue bus reset */
  2074. if (ap->flags & ATA_FLAG_SRST)
  2075. if (ata_bus_softreset(ap, devmask))
  2076. goto err_out;
  2077. /*
  2078. * determine by signature whether we have ATA or ATAPI devices
  2079. */
  2080. ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
  2081. if ((slave_possible) && (err != 0x81))
  2082. ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
  2083. /* re-enable interrupts */
  2084. if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
  2085. ata_irq_on(ap);
  2086. /* is double-select really necessary? */
  2087. if (ap->device[1].class != ATA_DEV_NONE)
  2088. ap->ops->dev_select(ap, 1);
  2089. if (ap->device[0].class != ATA_DEV_NONE)
  2090. ap->ops->dev_select(ap, 0);
  2091. /* if no devices were detected, disable this port */
  2092. if ((ap->device[0].class == ATA_DEV_NONE) &&
  2093. (ap->device[1].class == ATA_DEV_NONE))
  2094. goto err_out;
  2095. if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
  2096. /* set up device control for ATA_FLAG_SATA_RESET */
  2097. if (ap->flags & ATA_FLAG_MMIO)
  2098. writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
  2099. else
  2100. outb(ap->ctl, ioaddr->ctl_addr);
  2101. }
  2102. DPRINTK("EXIT\n");
  2103. return;
  2104. err_out:
  2105. ata_port_printk(ap, KERN_ERR, "disabling port\n");
  2106. ap->ops->port_disable(ap);
  2107. DPRINTK("EXIT\n");
  2108. }
  2109. /**
  2110. * sata_phy_debounce - debounce SATA phy status
  2111. * @ap: ATA port to debounce SATA phy status for
  2112. * @params: timing parameters { interval, duratinon, timeout } in msec
  2113. *
  2114. * Make sure SStatus of @ap reaches stable state, determined by
  2115. * holding the same value where DET is not 1 for @duration polled
  2116. * every @interval, before @timeout. Timeout constraints the
  2117. * beginning of the stable state. Because, after hot unplugging,
  2118. * DET gets stuck at 1 on some controllers, this functions waits
  2119. * until timeout then returns 0 if DET is stable at 1.
  2120. *
  2121. * LOCKING:
  2122. * Kernel thread context (may sleep)
  2123. *
  2124. * RETURNS:
  2125. * 0 on success, -errno on failure.
  2126. */
  2127. int sata_phy_debounce(struct ata_port *ap, const unsigned long *params)
  2128. {
  2129. unsigned long interval_msec = params[0];
  2130. unsigned long duration = params[1] * HZ / 1000;
  2131. unsigned long timeout = jiffies + params[2] * HZ / 1000;
  2132. unsigned long last_jiffies;
  2133. u32 last, cur;
  2134. int rc;
  2135. if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
  2136. return rc;
  2137. cur &= 0xf;
  2138. last = cur;
  2139. last_jiffies = jiffies;
  2140. while (1) {
  2141. msleep(interval_msec);
  2142. if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
  2143. return rc;
  2144. cur &= 0xf;
  2145. /* DET stable? */
  2146. if (cur == last) {
  2147. if (cur == 1 && time_before(jiffies, timeout))
  2148. continue;
  2149. if (time_after(jiffies, last_jiffies + duration))
  2150. return 0;
  2151. continue;
  2152. }
  2153. /* unstable, start over */
  2154. last = cur;
  2155. last_jiffies = jiffies;
  2156. /* check timeout */
  2157. if (time_after(jiffies, timeout))
  2158. return -EBUSY;
  2159. }
  2160. }
  2161. /**
  2162. * sata_phy_resume - resume SATA phy
  2163. * @ap: ATA port to resume SATA phy for
  2164. * @params: timing parameters { interval, duratinon, timeout } in msec
  2165. *
  2166. * Resume SATA phy of @ap and debounce it.
  2167. *
  2168. * LOCKING:
  2169. * Kernel thread context (may sleep)
  2170. *
  2171. * RETURNS:
  2172. * 0 on success, -errno on failure.
  2173. */
  2174. int sata_phy_resume(struct ata_port *ap, const unsigned long *params)
  2175. {
  2176. u32 scontrol;
  2177. int rc;
  2178. if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
  2179. return rc;
  2180. scontrol = (scontrol & 0x0f0) | 0x300;
  2181. if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
  2182. return rc;
  2183. /* Some PHYs react badly if SStatus is pounded immediately
  2184. * after resuming. Delay 200ms before debouncing.
  2185. */
  2186. msleep(200);
  2187. return sata_phy_debounce(ap, params);
  2188. }
  2189. static void ata_wait_spinup(struct ata_port *ap)
  2190. {
  2191. struct ata_eh_context *ehc = &ap->eh_context;
  2192. unsigned long end, secs;
  2193. int rc;
  2194. /* first, debounce phy if SATA */
  2195. if (ap->cbl == ATA_CBL_SATA) {
  2196. rc = sata_phy_debounce(ap, sata_deb_timing_eh);
  2197. /* if debounced successfully and offline, no need to wait */
  2198. if ((rc == 0 || rc == -EOPNOTSUPP) && ata_port_offline(ap))
  2199. return;
  2200. }
  2201. /* okay, let's give the drive time to spin up */
  2202. end = ehc->i.hotplug_timestamp + ATA_SPINUP_WAIT * HZ / 1000;
  2203. secs = ((end - jiffies) + HZ - 1) / HZ;
  2204. if (time_after(jiffies, end))
  2205. return;
  2206. if (secs > 5)
  2207. ata_port_printk(ap, KERN_INFO, "waiting for device to spin up "
  2208. "(%lu secs)\n", secs);
  2209. schedule_timeout_uninterruptible(end - jiffies);
  2210. }
  2211. /**
  2212. * ata_std_prereset - prepare for reset
  2213. * @ap: ATA port to be reset
  2214. *
  2215. * @ap is about to be reset. Initialize it.
  2216. *
  2217. * LOCKING:
  2218. * Kernel thread context (may sleep)
  2219. *
  2220. * RETURNS:
  2221. * 0 on success, -errno otherwise.
  2222. */
  2223. int ata_std_prereset(struct ata_port *ap)
  2224. {
  2225. struct ata_eh_context *ehc = &ap->eh_context;
  2226. const unsigned long *timing;
  2227. int rc;
  2228. /* hotplug? */
  2229. if (ehc->i.flags & ATA_EHI_HOTPLUGGED) {
  2230. if (ap->flags & ATA_FLAG_HRST_TO_RESUME)
  2231. ehc->i.action |= ATA_EH_HARDRESET;
  2232. if (ap->flags & ATA_FLAG_SKIP_D2H_BSY)
  2233. ata_wait_spinup(ap);
  2234. }
  2235. /* if we're about to do hardreset, nothing more to do */
  2236. if (ehc->i.action & ATA_EH_HARDRESET)
  2237. return 0;
  2238. /* if SATA, resume phy */
  2239. if (ap->cbl == ATA_CBL_SATA) {
  2240. if (ap->flags & ATA_FLAG_LOADING)
  2241. timing = sata_deb_timing_boot;
  2242. else
  2243. timing = sata_deb_timing_eh;
  2244. rc = sata_phy_resume(ap, timing);
  2245. if (rc && rc != -EOPNOTSUPP) {
  2246. /* phy resume failed */
  2247. ata_port_printk(ap, KERN_WARNING, "failed to resume "
  2248. "link for reset (errno=%d)\n", rc);
  2249. return rc;
  2250. }
  2251. }
  2252. /* Wait for !BSY if the controller can wait for the first D2H
  2253. * Reg FIS and we don't know that no device is attached.
  2254. */
  2255. if (!(ap->flags & ATA_FLAG_SKIP_D2H_BSY) && !ata_port_offline(ap))
  2256. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  2257. return 0;
  2258. }
  2259. /**
  2260. * ata_std_softreset - reset host port via ATA SRST
  2261. * @ap: port to reset
  2262. * @classes: resulting classes of attached devices
  2263. *
  2264. * Reset host port using ATA SRST.
  2265. *
  2266. * LOCKING:
  2267. * Kernel thread context (may sleep)
  2268. *
  2269. * RETURNS:
  2270. * 0 on success, -errno otherwise.
  2271. */
  2272. int ata_std_softreset(struct ata_port *ap, unsigned int *classes)
  2273. {
  2274. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  2275. unsigned int devmask = 0, err_mask;
  2276. u8 err;
  2277. DPRINTK("ENTER\n");
  2278. if (ata_port_offline(ap)) {
  2279. classes[0] = ATA_DEV_NONE;
  2280. goto out;
  2281. }
  2282. /* determine if device 0/1 are present */
  2283. if (ata_devchk(ap, 0))
  2284. devmask |= (1 << 0);
  2285. if (slave_possible && ata_devchk(ap, 1))
  2286. devmask |= (1 << 1);
  2287. /* select device 0 again */
  2288. ap->ops->dev_select(ap, 0);
  2289. /* issue bus reset */
  2290. DPRINTK("about to softreset, devmask=%x\n", devmask);
  2291. err_mask = ata_bus_softreset(ap, devmask);
  2292. if (err_mask) {
  2293. ata_port_printk(ap, KERN_ERR, "SRST failed (err_mask=0x%x)\n",
  2294. err_mask);
  2295. return -EIO;
  2296. }
  2297. /* determine by signature whether we have ATA or ATAPI devices */
  2298. classes[0] = ata_dev_try_classify(ap, 0, &err);
  2299. if (slave_possible && err != 0x81)
  2300. classes[1] = ata_dev_try_classify(ap, 1, &err);
  2301. out:
  2302. DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
  2303. return 0;
  2304. }
  2305. /**
  2306. * sata_std_hardreset - reset host port via SATA phy reset
  2307. * @ap: port to reset
  2308. * @class: resulting class of attached device
  2309. *
  2310. * SATA phy-reset host port using DET bits of SControl register.
  2311. *
  2312. * LOCKING:
  2313. * Kernel thread context (may sleep)
  2314. *
  2315. * RETURNS:
  2316. * 0 on success, -errno otherwise.
  2317. */
  2318. int sata_std_hardreset(struct ata_port *ap, unsigned int *class)
  2319. {
  2320. u32 scontrol;
  2321. int rc;
  2322. DPRINTK("ENTER\n");
  2323. if (sata_set_spd_needed(ap)) {
  2324. /* SATA spec says nothing about how to reconfigure
  2325. * spd. To be on the safe side, turn off phy during
  2326. * reconfiguration. This works for at least ICH7 AHCI
  2327. * and Sil3124.
  2328. */
  2329. if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
  2330. return rc;
  2331. scontrol = (scontrol & 0x0f0) | 0x302;
  2332. if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
  2333. return rc;
  2334. sata_set_spd(ap);
  2335. }
  2336. /* issue phy wake/reset */
  2337. if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
  2338. return rc;
  2339. scontrol = (scontrol & 0x0f0) | 0x301;
  2340. if ((rc = sata_scr_write_flush(ap, SCR_CONTROL, scontrol)))
  2341. return rc;
  2342. /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
  2343. * 10.4.2 says at least 1 ms.
  2344. */
  2345. msleep(1);
  2346. /* bring phy back */
  2347. sata_phy_resume(ap, sata_deb_timing_eh);
  2348. /* TODO: phy layer with polling, timeouts, etc. */
  2349. if (ata_port_offline(ap)) {
  2350. *class = ATA_DEV_NONE;
  2351. DPRINTK("EXIT, link offline\n");
  2352. return 0;
  2353. }
  2354. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  2355. ata_port_printk(ap, KERN_ERR,
  2356. "COMRESET failed (device not ready)\n");
  2357. return -EIO;
  2358. }
  2359. ap->ops->dev_select(ap, 0); /* probably unnecessary */
  2360. *class = ata_dev_try_classify(ap, 0, NULL);
  2361. DPRINTK("EXIT, class=%u\n", *class);
  2362. return 0;
  2363. }
  2364. /**
  2365. * ata_std_postreset - standard postreset callback
  2366. * @ap: the target ata_port
  2367. * @classes: classes of attached devices
  2368. *
  2369. * This function is invoked after a successful reset. Note that
  2370. * the device might have been reset more than once using
  2371. * different reset methods before postreset is invoked.
  2372. *
  2373. * LOCKING:
  2374. * Kernel thread context (may sleep)
  2375. */
  2376. void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
  2377. {
  2378. u32 serror;
  2379. DPRINTK("ENTER\n");
  2380. /* print link status */
  2381. sata_print_link_status(ap);
  2382. /* clear SError */
  2383. if (sata_scr_read(ap, SCR_ERROR, &serror) == 0)
  2384. sata_scr_write(ap, SCR_ERROR, serror);
  2385. /* re-enable interrupts */
  2386. if (!ap->ops->error_handler) {
  2387. /* FIXME: hack. create a hook instead */
  2388. if (ap->ioaddr.ctl_addr)
  2389. ata_irq_on(ap);
  2390. }
  2391. /* is double-select really necessary? */
  2392. if (classes[0] != ATA_DEV_NONE)
  2393. ap->ops->dev_select(ap, 1);
  2394. if (classes[1] != ATA_DEV_NONE)
  2395. ap->ops->dev_select(ap, 0);
  2396. /* bail out if no device is present */
  2397. if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
  2398. DPRINTK("EXIT, no device\n");
  2399. return;
  2400. }
  2401. /* set up device control */
  2402. if (ap->ioaddr.ctl_addr) {
  2403. if (ap->flags & ATA_FLAG_MMIO)
  2404. writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
  2405. else
  2406. outb(ap->ctl, ap->ioaddr.ctl_addr);
  2407. }
  2408. DPRINTK("EXIT\n");
  2409. }
  2410. /**
  2411. * ata_dev_same_device - Determine whether new ID matches configured device
  2412. * @dev: device to compare against
  2413. * @new_class: class of the new device
  2414. * @new_id: IDENTIFY page of the new device
  2415. *
  2416. * Compare @new_class and @new_id against @dev and determine
  2417. * whether @dev is the device indicated by @new_class and
  2418. * @new_id.
  2419. *
  2420. * LOCKING:
  2421. * None.
  2422. *
  2423. * RETURNS:
  2424. * 1 if @dev matches @new_class and @new_id, 0 otherwise.
  2425. */
  2426. static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
  2427. const u16 *new_id)
  2428. {
  2429. const u16 *old_id = dev->id;
  2430. unsigned char model[2][41], serial[2][21];
  2431. u64 new_n_sectors;
  2432. if (dev->class != new_class) {
  2433. ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
  2434. dev->class, new_class);
  2435. return 0;
  2436. }
  2437. ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
  2438. ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
  2439. ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
  2440. ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
  2441. new_n_sectors = ata_id_n_sectors(new_id);
  2442. if (strcmp(model[0], model[1])) {
  2443. ata_dev_printk(dev, KERN_INFO, "model number mismatch "
  2444. "'%s' != '%s'\n", model[0], model[1]);
  2445. return 0;
  2446. }
  2447. if (strcmp(serial[0], serial[1])) {
  2448. ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
  2449. "'%s' != '%s'\n", serial[0], serial[1]);
  2450. return 0;
  2451. }
  2452. if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
  2453. ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
  2454. "%llu != %llu\n",
  2455. (unsigned long long)dev->n_sectors,
  2456. (unsigned long long)new_n_sectors);
  2457. return 0;
  2458. }
  2459. return 1;
  2460. }
  2461. /**
  2462. * ata_dev_revalidate - Revalidate ATA device
  2463. * @dev: device to revalidate
  2464. * @post_reset: is this revalidation after reset?
  2465. *
  2466. * Re-read IDENTIFY page and make sure @dev is still attached to
  2467. * the port.
  2468. *
  2469. * LOCKING:
  2470. * Kernel thread context (may sleep)
  2471. *
  2472. * RETURNS:
  2473. * 0 on success, negative errno otherwise
  2474. */
  2475. int ata_dev_revalidate(struct ata_device *dev, int post_reset)
  2476. {
  2477. unsigned int class = dev->class;
  2478. u16 *id = (void *)dev->ap->sector_buf;
  2479. int rc;
  2480. if (!ata_dev_enabled(dev)) {
  2481. rc = -ENODEV;
  2482. goto fail;
  2483. }
  2484. /* read ID data */
  2485. rc = ata_dev_read_id(dev, &class, post_reset, id);
  2486. if (rc)
  2487. goto fail;
  2488. /* is the device still there? */
  2489. if (!ata_dev_same_device(dev, class, id)) {
  2490. rc = -ENODEV;
  2491. goto fail;
  2492. }
  2493. memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
  2494. /* configure device according to the new ID */
  2495. rc = ata_dev_configure(dev, 0);
  2496. if (rc == 0)
  2497. return 0;
  2498. fail:
  2499. ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
  2500. return rc;
  2501. }
  2502. static const char * const ata_dma_blacklist [] = {
  2503. "WDC AC11000H", NULL,
  2504. "WDC AC22100H", NULL,
  2505. "WDC AC32500H", NULL,
  2506. "WDC AC33100H", NULL,
  2507. "WDC AC31600H", NULL,
  2508. "WDC AC32100H", "24.09P07",
  2509. "WDC AC23200L", "21.10N21",
  2510. "Compaq CRD-8241B", NULL,
  2511. "CRD-8400B", NULL,
  2512. "CRD-8480B", NULL,
  2513. "CRD-8482B", NULL,
  2514. "CRD-84", NULL,
  2515. "SanDisk SDP3B", NULL,
  2516. "SanDisk SDP3B-64", NULL,
  2517. "SANYO CD-ROM CRD", NULL,
  2518. "HITACHI CDR-8", NULL,
  2519. "HITACHI CDR-8335", NULL,
  2520. "HITACHI CDR-8435", NULL,
  2521. "Toshiba CD-ROM XM-6202B", NULL,
  2522. "TOSHIBA CD-ROM XM-1702BC", NULL,
  2523. "CD-532E-A", NULL,
  2524. "E-IDE CD-ROM CR-840", NULL,
  2525. "CD-ROM Drive/F5A", NULL,
  2526. "WPI CDD-820", NULL,
  2527. "SAMSUNG CD-ROM SC-148C", NULL,
  2528. "SAMSUNG CD-ROM SC", NULL,
  2529. "SanDisk SDP3B-64", NULL,
  2530. "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,
  2531. "_NEC DV5800A", NULL,
  2532. "SAMSUNG CD-ROM SN-124", "N001"
  2533. };
  2534. static int ata_strim(char *s, size_t len)
  2535. {
  2536. len = strnlen(s, len);
  2537. /* ATAPI specifies that empty space is blank-filled; remove blanks */
  2538. while ((len > 0) && (s[len - 1] == ' ')) {
  2539. len--;
  2540. s[len] = 0;
  2541. }
  2542. return len;
  2543. }
  2544. static int ata_dma_blacklisted(const struct ata_device *dev)
  2545. {
  2546. unsigned char model_num[40];
  2547. unsigned char model_rev[16];
  2548. unsigned int nlen, rlen;
  2549. int i;
  2550. /* We don't support polling DMA.
  2551. * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
  2552. * if the LLDD handles only interrupts in the HSM_ST_LAST state.
  2553. */
  2554. if ((dev->ap->flags & ATA_FLAG_PIO_POLLING) &&
  2555. (dev->flags & ATA_DFLAG_CDB_INTR))
  2556. return 1;
  2557. ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
  2558. sizeof(model_num));
  2559. ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS,
  2560. sizeof(model_rev));
  2561. nlen = ata_strim(model_num, sizeof(model_num));
  2562. rlen = ata_strim(model_rev, sizeof(model_rev));
  2563. for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i += 2) {
  2564. if (!strncmp(ata_dma_blacklist[i], model_num, nlen)) {
  2565. if (ata_dma_blacklist[i+1] == NULL)
  2566. return 1;
  2567. if (!strncmp(ata_dma_blacklist[i], model_rev, rlen))
  2568. return 1;
  2569. }
  2570. }
  2571. return 0;
  2572. }
  2573. /**
  2574. * ata_dev_xfermask - Compute supported xfermask of the given device
  2575. * @dev: Device to compute xfermask for
  2576. *
  2577. * Compute supported xfermask of @dev and store it in
  2578. * dev->*_mask. This function is responsible for applying all
  2579. * known limits including host controller limits, device
  2580. * blacklist, etc...
  2581. *
  2582. * FIXME: The current implementation limits all transfer modes to
  2583. * the fastest of the lowested device on the port. This is not
  2584. * required on most controllers.
  2585. *
  2586. * LOCKING:
  2587. * None.
  2588. */
  2589. static void ata_dev_xfermask(struct ata_device *dev)
  2590. {
  2591. struct ata_port *ap = dev->ap;
  2592. struct ata_host_set *hs = ap->host_set;
  2593. unsigned long xfer_mask;
  2594. int i;
  2595. xfer_mask = ata_pack_xfermask(ap->pio_mask,
  2596. ap->mwdma_mask, ap->udma_mask);
  2597. /* Apply cable rule here. Don't apply it early because when
  2598. * we handle hot plug the cable type can itself change.
  2599. */
  2600. if (ap->cbl == ATA_CBL_PATA40)
  2601. xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
  2602. /* FIXME: Use port-wide xfermask for now */
  2603. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  2604. struct ata_device *d = &ap->device[i];
  2605. if (ata_dev_absent(d))
  2606. continue;
  2607. if (ata_dev_disabled(d)) {
  2608. /* to avoid violating device selection timing */
  2609. xfer_mask &= ata_pack_xfermask(d->pio_mask,
  2610. UINT_MAX, UINT_MAX);
  2611. continue;
  2612. }
  2613. xfer_mask &= ata_pack_xfermask(d->pio_mask,
  2614. d->mwdma_mask, d->udma_mask);
  2615. xfer_mask &= ata_id_xfermask(d->id);
  2616. if (ata_dma_blacklisted(d))
  2617. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  2618. }
  2619. if (ata_dma_blacklisted(dev))
  2620. ata_dev_printk(dev, KERN_WARNING,
  2621. "device is on DMA blacklist, disabling DMA\n");
  2622. if (hs->flags & ATA_HOST_SIMPLEX) {
  2623. if (hs->simplex_claimed)
  2624. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  2625. }
  2626. if (ap->ops->mode_filter)
  2627. xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask);
  2628. ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
  2629. &dev->mwdma_mask, &dev->udma_mask);
  2630. }
  2631. /**
  2632. * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
  2633. * @dev: Device to which command will be sent
  2634. *
  2635. * Issue SET FEATURES - XFER MODE command to device @dev
  2636. * on port @ap.
  2637. *
  2638. * LOCKING:
  2639. * PCI/etc. bus probe sem.
  2640. *
  2641. * RETURNS:
  2642. * 0 on success, AC_ERR_* mask otherwise.
  2643. */
  2644. static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
  2645. {
  2646. struct ata_taskfile tf;
  2647. unsigned int err_mask;
  2648. /* set up set-features taskfile */
  2649. DPRINTK("set features - xfer mode\n");
  2650. ata_tf_init(dev, &tf);
  2651. tf.command = ATA_CMD_SET_FEATURES;
  2652. tf.feature = SETFEATURES_XFER;
  2653. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2654. tf.protocol = ATA_PROT_NODATA;
  2655. tf.nsect = dev->xfer_mode;
  2656. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
  2657. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  2658. return err_mask;
  2659. }
  2660. /**
  2661. * ata_dev_init_params - Issue INIT DEV PARAMS command
  2662. * @dev: Device to which command will be sent
  2663. * @heads: Number of heads (taskfile parameter)
  2664. * @sectors: Number of sectors (taskfile parameter)
  2665. *
  2666. * LOCKING:
  2667. * Kernel thread context (may sleep)
  2668. *
  2669. * RETURNS:
  2670. * 0 on success, AC_ERR_* mask otherwise.
  2671. */
  2672. static unsigned int ata_dev_init_params(struct ata_device *dev,
  2673. u16 heads, u16 sectors)
  2674. {
  2675. struct ata_taskfile tf;
  2676. unsigned int err_mask;
  2677. /* Number of sectors per track 1-255. Number of heads 1-16 */
  2678. if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
  2679. return AC_ERR_INVALID;
  2680. /* set up init dev params taskfile */
  2681. DPRINTK("init dev params \n");
  2682. ata_tf_init(dev, &tf);
  2683. tf.command = ATA_CMD_INIT_DEV_PARAMS;
  2684. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2685. tf.protocol = ATA_PROT_NODATA;
  2686. tf.nsect = sectors;
  2687. tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
  2688. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
  2689. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  2690. return err_mask;
  2691. }
  2692. /**
  2693. * ata_sg_clean - Unmap DMA memory associated with command
  2694. * @qc: Command containing DMA memory to be released
  2695. *
  2696. * Unmap all mapped DMA memory associated with this command.
  2697. *
  2698. * LOCKING:
  2699. * spin_lock_irqsave(host_set lock)
  2700. */
  2701. static void ata_sg_clean(struct ata_queued_cmd *qc)
  2702. {
  2703. struct ata_port *ap = qc->ap;
  2704. struct scatterlist *sg = qc->__sg;
  2705. int dir = qc->dma_dir;
  2706. void *pad_buf = NULL;
  2707. WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
  2708. WARN_ON(sg == NULL);
  2709. if (qc->flags & ATA_QCFLAG_SINGLE)
  2710. WARN_ON(qc->n_elem > 1);
  2711. VPRINTK("unmapping %u sg elements\n", qc->n_elem);
  2712. /* if we padded the buffer out to 32-bit bound, and data
  2713. * xfer direction is from-device, we must copy from the
  2714. * pad buffer back into the supplied buffer
  2715. */
  2716. if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
  2717. pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  2718. if (qc->flags & ATA_QCFLAG_SG) {
  2719. if (qc->n_elem)
  2720. dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
  2721. /* restore last sg */
  2722. sg[qc->orig_n_elem - 1].length += qc->pad_len;
  2723. if (pad_buf) {
  2724. struct scatterlist *psg = &qc->pad_sgent;
  2725. void *addr = kmap_atomic(psg->page, KM_IRQ0);
  2726. memcpy(addr + psg->offset, pad_buf, qc->pad_len);
  2727. kunmap_atomic(addr, KM_IRQ0);
  2728. }
  2729. } else {
  2730. if (qc->n_elem)
  2731. dma_unmap_single(ap->dev,
  2732. sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
  2733. dir);
  2734. /* restore sg */
  2735. sg->length += qc->pad_len;
  2736. if (pad_buf)
  2737. memcpy(qc->buf_virt + sg->length - qc->pad_len,
  2738. pad_buf, qc->pad_len);
  2739. }
  2740. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  2741. qc->__sg = NULL;
  2742. }
  2743. /**
  2744. * ata_fill_sg - Fill PCI IDE PRD table
  2745. * @qc: Metadata associated with taskfile to be transferred
  2746. *
  2747. * Fill PCI IDE PRD (scatter-gather) table with segments
  2748. * associated with the current disk command.
  2749. *
  2750. * LOCKING:
  2751. * spin_lock_irqsave(host_set lock)
  2752. *
  2753. */
  2754. static void ata_fill_sg(struct ata_queued_cmd *qc)
  2755. {
  2756. struct ata_port *ap = qc->ap;
  2757. struct scatterlist *sg;
  2758. unsigned int idx;
  2759. WARN_ON(qc->__sg == NULL);
  2760. WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
  2761. idx = 0;
  2762. ata_for_each_sg(sg, qc) {
  2763. u32 addr, offset;
  2764. u32 sg_len, len;
  2765. /* determine if physical DMA addr spans 64K boundary.
  2766. * Note h/w doesn't support 64-bit, so we unconditionally
  2767. * truncate dma_addr_t to u32.
  2768. */
  2769. addr = (u32) sg_dma_address(sg);
  2770. sg_len = sg_dma_len(sg);
  2771. while (sg_len) {
  2772. offset = addr & 0xffff;
  2773. len = sg_len;
  2774. if ((offset + sg_len) > 0x10000)
  2775. len = 0x10000 - offset;
  2776. ap->prd[idx].addr = cpu_to_le32(addr);
  2777. ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
  2778. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
  2779. idx++;
  2780. sg_len -= len;
  2781. addr += len;
  2782. }
  2783. }
  2784. if (idx)
  2785. ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  2786. }
  2787. /**
  2788. * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
  2789. * @qc: Metadata associated with taskfile to check
  2790. *
  2791. * Allow low-level driver to filter ATA PACKET commands, returning
  2792. * a status indicating whether or not it is OK to use DMA for the
  2793. * supplied PACKET command.
  2794. *
  2795. * LOCKING:
  2796. * spin_lock_irqsave(host_set lock)
  2797. *
  2798. * RETURNS: 0 when ATAPI DMA can be used
  2799. * nonzero otherwise
  2800. */
  2801. int ata_check_atapi_dma(struct ata_queued_cmd *qc)
  2802. {
  2803. struct ata_port *ap = qc->ap;
  2804. int rc = 0; /* Assume ATAPI DMA is OK by default */
  2805. if (ap->ops->check_atapi_dma)
  2806. rc = ap->ops->check_atapi_dma(qc);
  2807. return rc;
  2808. }
  2809. /**
  2810. * ata_qc_prep - Prepare taskfile for submission
  2811. * @qc: Metadata associated with taskfile to be prepared
  2812. *
  2813. * Prepare ATA taskfile for submission.
  2814. *
  2815. * LOCKING:
  2816. * spin_lock_irqsave(host_set lock)
  2817. */
  2818. void ata_qc_prep(struct ata_queued_cmd *qc)
  2819. {
  2820. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  2821. return;
  2822. ata_fill_sg(qc);
  2823. }
  2824. void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
  2825. /**
  2826. * ata_sg_init_one - Associate command with memory buffer
  2827. * @qc: Command to be associated
  2828. * @buf: Memory buffer
  2829. * @buflen: Length of memory buffer, in bytes.
  2830. *
  2831. * Initialize the data-related elements of queued_cmd @qc
  2832. * to point to a single memory buffer, @buf of byte length @buflen.
  2833. *
  2834. * LOCKING:
  2835. * spin_lock_irqsave(host_set lock)
  2836. */
  2837. void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
  2838. {
  2839. struct scatterlist *sg;
  2840. qc->flags |= ATA_QCFLAG_SINGLE;
  2841. memset(&qc->sgent, 0, sizeof(qc->sgent));
  2842. qc->__sg = &qc->sgent;
  2843. qc->n_elem = 1;
  2844. qc->orig_n_elem = 1;
  2845. qc->buf_virt = buf;
  2846. qc->nbytes = buflen;
  2847. sg = qc->__sg;
  2848. sg_init_one(sg, buf, buflen);
  2849. }
  2850. /**
  2851. * ata_sg_init - Associate command with scatter-gather table.
  2852. * @qc: Command to be associated
  2853. * @sg: Scatter-gather table.
  2854. * @n_elem: Number of elements in s/g table.
  2855. *
  2856. * Initialize the data-related elements of queued_cmd @qc
  2857. * to point to a scatter-gather table @sg, containing @n_elem
  2858. * elements.
  2859. *
  2860. * LOCKING:
  2861. * spin_lock_irqsave(host_set lock)
  2862. */
  2863. void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
  2864. unsigned int n_elem)
  2865. {
  2866. qc->flags |= ATA_QCFLAG_SG;
  2867. qc->__sg = sg;
  2868. qc->n_elem = n_elem;
  2869. qc->orig_n_elem = n_elem;
  2870. }
  2871. /**
  2872. * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
  2873. * @qc: Command with memory buffer to be mapped.
  2874. *
  2875. * DMA-map the memory buffer associated with queued_cmd @qc.
  2876. *
  2877. * LOCKING:
  2878. * spin_lock_irqsave(host_set lock)
  2879. *
  2880. * RETURNS:
  2881. * Zero on success, negative on error.
  2882. */
  2883. static int ata_sg_setup_one(struct ata_queued_cmd *qc)
  2884. {
  2885. struct ata_port *ap = qc->ap;
  2886. int dir = qc->dma_dir;
  2887. struct scatterlist *sg = qc->__sg;
  2888. dma_addr_t dma_address;
  2889. int trim_sg = 0;
  2890. /* we must lengthen transfers to end on a 32-bit boundary */
  2891. qc->pad_len = sg->length & 3;
  2892. if (qc->pad_len) {
  2893. void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  2894. struct scatterlist *psg = &qc->pad_sgent;
  2895. WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
  2896. memset(pad_buf, 0, ATA_DMA_PAD_SZ);
  2897. if (qc->tf.flags & ATA_TFLAG_WRITE)
  2898. memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
  2899. qc->pad_len);
  2900. sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
  2901. sg_dma_len(psg) = ATA_DMA_PAD_SZ;
  2902. /* trim sg */
  2903. sg->length -= qc->pad_len;
  2904. if (sg->length == 0)
  2905. trim_sg = 1;
  2906. DPRINTK("padding done, sg->length=%u pad_len=%u\n",
  2907. sg->length, qc->pad_len);
  2908. }
  2909. if (trim_sg) {
  2910. qc->n_elem--;
  2911. goto skip_map;
  2912. }
  2913. dma_address = dma_map_single(ap->dev, qc->buf_virt,
  2914. sg->length, dir);
  2915. if (dma_mapping_error(dma_address)) {
  2916. /* restore sg */
  2917. sg->length += qc->pad_len;
  2918. return -1;
  2919. }
  2920. sg_dma_address(sg) = dma_address;
  2921. sg_dma_len(sg) = sg->length;
  2922. skip_map:
  2923. DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
  2924. qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  2925. return 0;
  2926. }
  2927. /**
  2928. * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
  2929. * @qc: Command with scatter-gather table to be mapped.
  2930. *
  2931. * DMA-map the scatter-gather table associated with queued_cmd @qc.
  2932. *
  2933. * LOCKING:
  2934. * spin_lock_irqsave(host_set lock)
  2935. *
  2936. * RETURNS:
  2937. * Zero on success, negative on error.
  2938. *
  2939. */
  2940. static int ata_sg_setup(struct ata_queued_cmd *qc)
  2941. {
  2942. struct ata_port *ap = qc->ap;
  2943. struct scatterlist *sg = qc->__sg;
  2944. struct scatterlist *lsg = &sg[qc->n_elem - 1];
  2945. int n_elem, pre_n_elem, dir, trim_sg = 0;
  2946. VPRINTK("ENTER, ata%u\n", ap->id);
  2947. WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
  2948. /* we must lengthen transfers to end on a 32-bit boundary */
  2949. qc->pad_len = lsg->length & 3;
  2950. if (qc->pad_len) {
  2951. void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  2952. struct scatterlist *psg = &qc->pad_sgent;
  2953. unsigned int offset;
  2954. WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
  2955. memset(pad_buf, 0, ATA_DMA_PAD_SZ);
  2956. /*
  2957. * psg->page/offset are used to copy to-be-written
  2958. * data in this function or read data in ata_sg_clean.
  2959. */
  2960. offset = lsg->offset + lsg->length - qc->pad_len;
  2961. psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
  2962. psg->offset = offset_in_page(offset);
  2963. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  2964. void *addr = kmap_atomic(psg->page, KM_IRQ0);
  2965. memcpy(pad_buf, addr + psg->offset, qc->pad_len);
  2966. kunmap_atomic(addr, KM_IRQ0);
  2967. }
  2968. sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
  2969. sg_dma_len(psg) = ATA_DMA_PAD_SZ;
  2970. /* trim last sg */
  2971. lsg->length -= qc->pad_len;
  2972. if (lsg->length == 0)
  2973. trim_sg = 1;
  2974. DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
  2975. qc->n_elem - 1, lsg->length, qc->pad_len);
  2976. }
  2977. pre_n_elem = qc->n_elem;
  2978. if (trim_sg && pre_n_elem)
  2979. pre_n_elem--;
  2980. if (!pre_n_elem) {
  2981. n_elem = 0;
  2982. goto skip_map;
  2983. }
  2984. dir = qc->dma_dir;
  2985. n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
  2986. if (n_elem < 1) {
  2987. /* restore last sg */
  2988. lsg->length += qc->pad_len;
  2989. return -1;
  2990. }
  2991. DPRINTK("%d sg elements mapped\n", n_elem);
  2992. skip_map:
  2993. qc->n_elem = n_elem;
  2994. return 0;
  2995. }
  2996. /**
  2997. * swap_buf_le16 - swap halves of 16-bit words in place
  2998. * @buf: Buffer to swap
  2999. * @buf_words: Number of 16-bit words in buffer.
  3000. *
  3001. * Swap halves of 16-bit words if needed to convert from
  3002. * little-endian byte order to native cpu byte order, or
  3003. * vice-versa.
  3004. *
  3005. * LOCKING:
  3006. * Inherited from caller.
  3007. */
  3008. void swap_buf_le16(u16 *buf, unsigned int buf_words)
  3009. {
  3010. #ifdef __BIG_ENDIAN
  3011. unsigned int i;
  3012. for (i = 0; i < buf_words; i++)
  3013. buf[i] = le16_to_cpu(buf[i]);
  3014. #endif /* __BIG_ENDIAN */
  3015. }
  3016. /**
  3017. * ata_mmio_data_xfer - Transfer data by MMIO
  3018. * @adev: device for this I/O
  3019. * @buf: data buffer
  3020. * @buflen: buffer length
  3021. * @write_data: read/write
  3022. *
  3023. * Transfer data from/to the device data register by MMIO.
  3024. *
  3025. * LOCKING:
  3026. * Inherited from caller.
  3027. */
  3028. void ata_mmio_data_xfer(struct ata_device *adev, unsigned char *buf,
  3029. unsigned int buflen, int write_data)
  3030. {
  3031. struct ata_port *ap = adev->ap;
  3032. unsigned int i;
  3033. unsigned int words = buflen >> 1;
  3034. u16 *buf16 = (u16 *) buf;
  3035. void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
  3036. /* Transfer multiple of 2 bytes */
  3037. if (write_data) {
  3038. for (i = 0; i < words; i++)
  3039. writew(le16_to_cpu(buf16[i]), mmio);
  3040. } else {
  3041. for (i = 0; i < words; i++)
  3042. buf16[i] = cpu_to_le16(readw(mmio));
  3043. }
  3044. /* Transfer trailing 1 byte, if any. */
  3045. if (unlikely(buflen & 0x01)) {
  3046. u16 align_buf[1] = { 0 };
  3047. unsigned char *trailing_buf = buf + buflen - 1;
  3048. if (write_data) {
  3049. memcpy(align_buf, trailing_buf, 1);
  3050. writew(le16_to_cpu(align_buf[0]), mmio);
  3051. } else {
  3052. align_buf[0] = cpu_to_le16(readw(mmio));
  3053. memcpy(trailing_buf, align_buf, 1);
  3054. }
  3055. }
  3056. }
  3057. /**
  3058. * ata_pio_data_xfer - Transfer data by PIO
  3059. * @adev: device to target
  3060. * @buf: data buffer
  3061. * @buflen: buffer length
  3062. * @write_data: read/write
  3063. *
  3064. * Transfer data from/to the device data register by PIO.
  3065. *
  3066. * LOCKING:
  3067. * Inherited from caller.
  3068. */
  3069. void ata_pio_data_xfer(struct ata_device *adev, unsigned char *buf,
  3070. unsigned int buflen, int write_data)
  3071. {
  3072. struct ata_port *ap = adev->ap;
  3073. unsigned int words = buflen >> 1;
  3074. /* Transfer multiple of 2 bytes */
  3075. if (write_data)
  3076. outsw(ap->ioaddr.data_addr, buf, words);
  3077. else
  3078. insw(ap->ioaddr.data_addr, buf, words);
  3079. /* Transfer trailing 1 byte, if any. */
  3080. if (unlikely(buflen & 0x01)) {
  3081. u16 align_buf[1] = { 0 };
  3082. unsigned char *trailing_buf = buf + buflen - 1;
  3083. if (write_data) {
  3084. memcpy(align_buf, trailing_buf, 1);
  3085. outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
  3086. } else {
  3087. align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
  3088. memcpy(trailing_buf, align_buf, 1);
  3089. }
  3090. }
  3091. }
  3092. /**
  3093. * ata_pio_data_xfer_noirq - Transfer data by PIO
  3094. * @adev: device to target
  3095. * @buf: data buffer
  3096. * @buflen: buffer length
  3097. * @write_data: read/write
  3098. *
  3099. * Transfer data from/to the device data register by PIO. Do the
  3100. * transfer with interrupts disabled.
  3101. *
  3102. * LOCKING:
  3103. * Inherited from caller.
  3104. */
  3105. void ata_pio_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
  3106. unsigned int buflen, int write_data)
  3107. {
  3108. unsigned long flags;
  3109. local_irq_save(flags);
  3110. ata_pio_data_xfer(adev, buf, buflen, write_data);
  3111. local_irq_restore(flags);
  3112. }
  3113. /**
  3114. * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
  3115. * @qc: Command on going
  3116. *
  3117. * Transfer ATA_SECT_SIZE of data from/to the ATA device.
  3118. *
  3119. * LOCKING:
  3120. * Inherited from caller.
  3121. */
  3122. static void ata_pio_sector(struct ata_queued_cmd *qc)
  3123. {
  3124. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  3125. struct scatterlist *sg = qc->__sg;
  3126. struct ata_port *ap = qc->ap;
  3127. struct page *page;
  3128. unsigned int offset;
  3129. unsigned char *buf;
  3130. if (qc->cursect == (qc->nsect - 1))
  3131. ap->hsm_task_state = HSM_ST_LAST;
  3132. page = sg[qc->cursg].page;
  3133. offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
  3134. /* get the current page and offset */
  3135. page = nth_page(page, (offset >> PAGE_SHIFT));
  3136. offset %= PAGE_SIZE;
  3137. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  3138. if (PageHighMem(page)) {
  3139. unsigned long flags;
  3140. /* FIXME: use a bounce buffer */
  3141. local_irq_save(flags);
  3142. buf = kmap_atomic(page, KM_IRQ0);
  3143. /* do the actual data transfer */
  3144. ap->ops->data_xfer(qc->dev, buf + offset, ATA_SECT_SIZE, do_write);
  3145. kunmap_atomic(buf, KM_IRQ0);
  3146. local_irq_restore(flags);
  3147. } else {
  3148. buf = page_address(page);
  3149. ap->ops->data_xfer(qc->dev, buf + offset, ATA_SECT_SIZE, do_write);
  3150. }
  3151. qc->cursect++;
  3152. qc->cursg_ofs++;
  3153. if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
  3154. qc->cursg++;
  3155. qc->cursg_ofs = 0;
  3156. }
  3157. }
  3158. /**
  3159. * ata_pio_sectors - Transfer one or many 512-byte sectors.
  3160. * @qc: Command on going
  3161. *
  3162. * Transfer one or many ATA_SECT_SIZE of data from/to the
  3163. * ATA device for the DRQ request.
  3164. *
  3165. * LOCKING:
  3166. * Inherited from caller.
  3167. */
  3168. static void ata_pio_sectors(struct ata_queued_cmd *qc)
  3169. {
  3170. if (is_multi_taskfile(&qc->tf)) {
  3171. /* READ/WRITE MULTIPLE */
  3172. unsigned int nsect;
  3173. WARN_ON(qc->dev->multi_count == 0);
  3174. nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
  3175. while (nsect--)
  3176. ata_pio_sector(qc);
  3177. } else
  3178. ata_pio_sector(qc);
  3179. }
  3180. /**
  3181. * atapi_send_cdb - Write CDB bytes to hardware
  3182. * @ap: Port to which ATAPI device is attached.
  3183. * @qc: Taskfile currently active
  3184. *
  3185. * When device has indicated its readiness to accept
  3186. * a CDB, this function is called. Send the CDB.
  3187. *
  3188. * LOCKING:
  3189. * caller.
  3190. */
  3191. static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
  3192. {
  3193. /* send SCSI cdb */
  3194. DPRINTK("send cdb\n");
  3195. WARN_ON(qc->dev->cdb_len < 12);
  3196. ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
  3197. ata_altstatus(ap); /* flush */
  3198. switch (qc->tf.protocol) {
  3199. case ATA_PROT_ATAPI:
  3200. ap->hsm_task_state = HSM_ST;
  3201. break;
  3202. case ATA_PROT_ATAPI_NODATA:
  3203. ap->hsm_task_state = HSM_ST_LAST;
  3204. break;
  3205. case ATA_PROT_ATAPI_DMA:
  3206. ap->hsm_task_state = HSM_ST_LAST;
  3207. /* initiate bmdma */
  3208. ap->ops->bmdma_start(qc);
  3209. break;
  3210. }
  3211. }
  3212. /**
  3213. * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
  3214. * @qc: Command on going
  3215. * @bytes: number of bytes
  3216. *
  3217. * Transfer Transfer data from/to the ATAPI device.
  3218. *
  3219. * LOCKING:
  3220. * Inherited from caller.
  3221. *
  3222. */
  3223. static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
  3224. {
  3225. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  3226. struct scatterlist *sg = qc->__sg;
  3227. struct ata_port *ap = qc->ap;
  3228. struct page *page;
  3229. unsigned char *buf;
  3230. unsigned int offset, count;
  3231. if (qc->curbytes + bytes >= qc->nbytes)
  3232. ap->hsm_task_state = HSM_ST_LAST;
  3233. next_sg:
  3234. if (unlikely(qc->cursg >= qc->n_elem)) {
  3235. /*
  3236. * The end of qc->sg is reached and the device expects
  3237. * more data to transfer. In order not to overrun qc->sg
  3238. * and fulfill length specified in the byte count register,
  3239. * - for read case, discard trailing data from the device
  3240. * - for write case, padding zero data to the device
  3241. */
  3242. u16 pad_buf[1] = { 0 };
  3243. unsigned int words = bytes >> 1;
  3244. unsigned int i;
  3245. if (words) /* warning if bytes > 1 */
  3246. ata_dev_printk(qc->dev, KERN_WARNING,
  3247. "%u bytes trailing data\n", bytes);
  3248. for (i = 0; i < words; i++)
  3249. ap->ops->data_xfer(qc->dev, (unsigned char*)pad_buf, 2, do_write);
  3250. ap->hsm_task_state = HSM_ST_LAST;
  3251. return;
  3252. }
  3253. sg = &qc->__sg[qc->cursg];
  3254. page = sg->page;
  3255. offset = sg->offset + qc->cursg_ofs;
  3256. /* get the current page and offset */
  3257. page = nth_page(page, (offset >> PAGE_SHIFT));
  3258. offset %= PAGE_SIZE;
  3259. /* don't overrun current sg */
  3260. count = min(sg->length - qc->cursg_ofs, bytes);
  3261. /* don't cross page boundaries */
  3262. count = min(count, (unsigned int)PAGE_SIZE - offset);
  3263. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  3264. if (PageHighMem(page)) {
  3265. unsigned long flags;
  3266. /* FIXME: use bounce buffer */
  3267. local_irq_save(flags);
  3268. buf = kmap_atomic(page, KM_IRQ0);
  3269. /* do the actual data transfer */
  3270. ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
  3271. kunmap_atomic(buf, KM_IRQ0);
  3272. local_irq_restore(flags);
  3273. } else {
  3274. buf = page_address(page);
  3275. ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
  3276. }
  3277. bytes -= count;
  3278. qc->curbytes += count;
  3279. qc->cursg_ofs += count;
  3280. if (qc->cursg_ofs == sg->length) {
  3281. qc->cursg++;
  3282. qc->cursg_ofs = 0;
  3283. }
  3284. if (bytes)
  3285. goto next_sg;
  3286. }
  3287. /**
  3288. * atapi_pio_bytes - Transfer data from/to the ATAPI device.
  3289. * @qc: Command on going
  3290. *
  3291. * Transfer Transfer data from/to the ATAPI device.
  3292. *
  3293. * LOCKING:
  3294. * Inherited from caller.
  3295. */
  3296. static void atapi_pio_bytes(struct ata_queued_cmd *qc)
  3297. {
  3298. struct ata_port *ap = qc->ap;
  3299. struct ata_device *dev = qc->dev;
  3300. unsigned int ireason, bc_lo, bc_hi, bytes;
  3301. int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
  3302. /* Abuse qc->result_tf for temp storage of intermediate TF
  3303. * here to save some kernel stack usage.
  3304. * For normal completion, qc->result_tf is not relevant. For
  3305. * error, qc->result_tf is later overwritten by ata_qc_complete().
  3306. * So, the correctness of qc->result_tf is not affected.
  3307. */
  3308. ap->ops->tf_read(ap, &qc->result_tf);
  3309. ireason = qc->result_tf.nsect;
  3310. bc_lo = qc->result_tf.lbam;
  3311. bc_hi = qc->result_tf.lbah;
  3312. bytes = (bc_hi << 8) | bc_lo;
  3313. /* shall be cleared to zero, indicating xfer of data */
  3314. if (ireason & (1 << 0))
  3315. goto err_out;
  3316. /* make sure transfer direction matches expected */
  3317. i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
  3318. if (do_write != i_write)
  3319. goto err_out;
  3320. VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
  3321. __atapi_pio_bytes(qc, bytes);
  3322. return;
  3323. err_out:
  3324. ata_dev_printk(dev, KERN_INFO, "ATAPI check failed\n");
  3325. qc->err_mask |= AC_ERR_HSM;
  3326. ap->hsm_task_state = HSM_ST_ERR;
  3327. }
  3328. /**
  3329. * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
  3330. * @ap: the target ata_port
  3331. * @qc: qc on going
  3332. *
  3333. * RETURNS:
  3334. * 1 if ok in workqueue, 0 otherwise.
  3335. */
  3336. static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
  3337. {
  3338. if (qc->tf.flags & ATA_TFLAG_POLLING)
  3339. return 1;
  3340. if (ap->hsm_task_state == HSM_ST_FIRST) {
  3341. if (qc->tf.protocol == ATA_PROT_PIO &&
  3342. (qc->tf.flags & ATA_TFLAG_WRITE))
  3343. return 1;
  3344. if (is_atapi_taskfile(&qc->tf) &&
  3345. !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  3346. return 1;
  3347. }
  3348. return 0;
  3349. }
  3350. /**
  3351. * ata_hsm_qc_complete - finish a qc running on standard HSM
  3352. * @qc: Command to complete
  3353. * @in_wq: 1 if called from workqueue, 0 otherwise
  3354. *
  3355. * Finish @qc which is running on standard HSM.
  3356. *
  3357. * LOCKING:
  3358. * If @in_wq is zero, spin_lock_irqsave(host_set lock).
  3359. * Otherwise, none on entry and grabs host lock.
  3360. */
  3361. static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
  3362. {
  3363. struct ata_port *ap = qc->ap;
  3364. unsigned long flags;
  3365. if (ap->ops->error_handler) {
  3366. if (in_wq) {
  3367. spin_lock_irqsave(ap->lock, flags);
  3368. /* EH might have kicked in while host_set lock
  3369. * is released.
  3370. */
  3371. qc = ata_qc_from_tag(ap, qc->tag);
  3372. if (qc) {
  3373. if (likely(!(qc->err_mask & AC_ERR_HSM))) {
  3374. ata_irq_on(ap);
  3375. ata_qc_complete(qc);
  3376. } else
  3377. ata_port_freeze(ap);
  3378. }
  3379. spin_unlock_irqrestore(ap->lock, flags);
  3380. } else {
  3381. if (likely(!(qc->err_mask & AC_ERR_HSM)))
  3382. ata_qc_complete(qc);
  3383. else
  3384. ata_port_freeze(ap);
  3385. }
  3386. } else {
  3387. if (in_wq) {
  3388. spin_lock_irqsave(ap->lock, flags);
  3389. ata_irq_on(ap);
  3390. ata_qc_complete(qc);
  3391. spin_unlock_irqrestore(ap->lock, flags);
  3392. } else
  3393. ata_qc_complete(qc);
  3394. }
  3395. ata_altstatus(ap); /* flush */
  3396. }
  3397. /**
  3398. * ata_hsm_move - move the HSM to the next state.
  3399. * @ap: the target ata_port
  3400. * @qc: qc on going
  3401. * @status: current device status
  3402. * @in_wq: 1 if called from workqueue, 0 otherwise
  3403. *
  3404. * RETURNS:
  3405. * 1 when poll next status needed, 0 otherwise.
  3406. */
  3407. int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
  3408. u8 status, int in_wq)
  3409. {
  3410. unsigned long flags = 0;
  3411. int poll_next;
  3412. WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
  3413. /* Make sure ata_qc_issue_prot() does not throw things
  3414. * like DMA polling into the workqueue. Notice that
  3415. * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
  3416. */
  3417. WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
  3418. fsm_start:
  3419. DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
  3420. ap->id, qc->tf.protocol, ap->hsm_task_state, status);
  3421. switch (ap->hsm_task_state) {
  3422. case HSM_ST_FIRST:
  3423. /* Send first data block or PACKET CDB */
  3424. /* If polling, we will stay in the work queue after
  3425. * sending the data. Otherwise, interrupt handler
  3426. * takes over after sending the data.
  3427. */
  3428. poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
  3429. /* check device status */
  3430. if (unlikely((status & ATA_DRQ) == 0)) {
  3431. /* handle BSY=0, DRQ=0 as error */
  3432. if (likely(status & (ATA_ERR | ATA_DF)))
  3433. /* device stops HSM for abort/error */
  3434. qc->err_mask |= AC_ERR_DEV;
  3435. else
  3436. /* HSM violation. Let EH handle this */
  3437. qc->err_mask |= AC_ERR_HSM;
  3438. ap->hsm_task_state = HSM_ST_ERR;
  3439. goto fsm_start;
  3440. }
  3441. /* Device should not ask for data transfer (DRQ=1)
  3442. * when it finds something wrong.
  3443. * We ignore DRQ here and stop the HSM by
  3444. * changing hsm_task_state to HSM_ST_ERR and
  3445. * let the EH abort the command or reset the device.
  3446. */
  3447. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  3448. printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
  3449. ap->id, status);
  3450. qc->err_mask |= AC_ERR_HSM;
  3451. ap->hsm_task_state = HSM_ST_ERR;
  3452. goto fsm_start;
  3453. }
  3454. /* Send the CDB (atapi) or the first data block (ata pio out).
  3455. * During the state transition, interrupt handler shouldn't
  3456. * be invoked before the data transfer is complete and
  3457. * hsm_task_state is changed. Hence, the following locking.
  3458. */
  3459. if (in_wq)
  3460. spin_lock_irqsave(ap->lock, flags);
  3461. if (qc->tf.protocol == ATA_PROT_PIO) {
  3462. /* PIO data out protocol.
  3463. * send first data block.
  3464. */
  3465. /* ata_pio_sectors() might change the state
  3466. * to HSM_ST_LAST. so, the state is changed here
  3467. * before ata_pio_sectors().
  3468. */
  3469. ap->hsm_task_state = HSM_ST;
  3470. ata_pio_sectors(qc);
  3471. ata_altstatus(ap); /* flush */
  3472. } else
  3473. /* send CDB */
  3474. atapi_send_cdb(ap, qc);
  3475. if (in_wq)
  3476. spin_unlock_irqrestore(ap->lock, flags);
  3477. /* if polling, ata_pio_task() handles the rest.
  3478. * otherwise, interrupt handler takes over from here.
  3479. */
  3480. break;
  3481. case HSM_ST:
  3482. /* complete command or read/write the data register */
  3483. if (qc->tf.protocol == ATA_PROT_ATAPI) {
  3484. /* ATAPI PIO protocol */
  3485. if ((status & ATA_DRQ) == 0) {
  3486. /* No more data to transfer or device error.
  3487. * Device error will be tagged in HSM_ST_LAST.
  3488. */
  3489. ap->hsm_task_state = HSM_ST_LAST;
  3490. goto fsm_start;
  3491. }
  3492. /* Device should not ask for data transfer (DRQ=1)
  3493. * when it finds something wrong.
  3494. * We ignore DRQ here and stop the HSM by
  3495. * changing hsm_task_state to HSM_ST_ERR and
  3496. * let the EH abort the command or reset the device.
  3497. */
  3498. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  3499. printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
  3500. ap->id, status);
  3501. qc->err_mask |= AC_ERR_HSM;
  3502. ap->hsm_task_state = HSM_ST_ERR;
  3503. goto fsm_start;
  3504. }
  3505. atapi_pio_bytes(qc);
  3506. if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
  3507. /* bad ireason reported by device */
  3508. goto fsm_start;
  3509. } else {
  3510. /* ATA PIO protocol */
  3511. if (unlikely((status & ATA_DRQ) == 0)) {
  3512. /* handle BSY=0, DRQ=0 as error */
  3513. if (likely(status & (ATA_ERR | ATA_DF)))
  3514. /* device stops HSM for abort/error */
  3515. qc->err_mask |= AC_ERR_DEV;
  3516. else
  3517. /* HSM violation. Let EH handle this */
  3518. qc->err_mask |= AC_ERR_HSM;
  3519. ap->hsm_task_state = HSM_ST_ERR;
  3520. goto fsm_start;
  3521. }
  3522. /* For PIO reads, some devices may ask for
  3523. * data transfer (DRQ=1) alone with ERR=1.
  3524. * We respect DRQ here and transfer one
  3525. * block of junk data before changing the
  3526. * hsm_task_state to HSM_ST_ERR.
  3527. *
  3528. * For PIO writes, ERR=1 DRQ=1 doesn't make
  3529. * sense since the data block has been
  3530. * transferred to the device.
  3531. */
  3532. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  3533. /* data might be corrputed */
  3534. qc->err_mask |= AC_ERR_DEV;
  3535. if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
  3536. ata_pio_sectors(qc);
  3537. ata_altstatus(ap);
  3538. status = ata_wait_idle(ap);
  3539. }
  3540. if (status & (ATA_BUSY | ATA_DRQ))
  3541. qc->err_mask |= AC_ERR_HSM;
  3542. /* ata_pio_sectors() might change the
  3543. * state to HSM_ST_LAST. so, the state
  3544. * is changed after ata_pio_sectors().
  3545. */
  3546. ap->hsm_task_state = HSM_ST_ERR;
  3547. goto fsm_start;
  3548. }
  3549. ata_pio_sectors(qc);
  3550. if (ap->hsm_task_state == HSM_ST_LAST &&
  3551. (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
  3552. /* all data read */
  3553. ata_altstatus(ap);
  3554. status = ata_wait_idle(ap);
  3555. goto fsm_start;
  3556. }
  3557. }
  3558. ata_altstatus(ap); /* flush */
  3559. poll_next = 1;
  3560. break;
  3561. case HSM_ST_LAST:
  3562. if (unlikely(!ata_ok(status))) {
  3563. qc->err_mask |= __ac_err_mask(status);
  3564. ap->hsm_task_state = HSM_ST_ERR;
  3565. goto fsm_start;
  3566. }
  3567. /* no more data to transfer */
  3568. DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
  3569. ap->id, qc->dev->devno, status);
  3570. WARN_ON(qc->err_mask);
  3571. ap->hsm_task_state = HSM_ST_IDLE;
  3572. /* complete taskfile transaction */
  3573. ata_hsm_qc_complete(qc, in_wq);
  3574. poll_next = 0;
  3575. break;
  3576. case HSM_ST_ERR:
  3577. /* make sure qc->err_mask is available to
  3578. * know what's wrong and recover
  3579. */
  3580. WARN_ON(qc->err_mask == 0);
  3581. ap->hsm_task_state = HSM_ST_IDLE;
  3582. /* complete taskfile transaction */
  3583. ata_hsm_qc_complete(qc, in_wq);
  3584. poll_next = 0;
  3585. break;
  3586. default:
  3587. poll_next = 0;
  3588. BUG();
  3589. }
  3590. return poll_next;
  3591. }
  3592. static void ata_pio_task(void *_data)
  3593. {
  3594. struct ata_queued_cmd *qc = _data;
  3595. struct ata_port *ap = qc->ap;
  3596. u8 status;
  3597. int poll_next;
  3598. fsm_start:
  3599. WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
  3600. /*
  3601. * This is purely heuristic. This is a fast path.
  3602. * Sometimes when we enter, BSY will be cleared in
  3603. * a chk-status or two. If not, the drive is probably seeking
  3604. * or something. Snooze for a couple msecs, then
  3605. * chk-status again. If still busy, queue delayed work.
  3606. */
  3607. status = ata_busy_wait(ap, ATA_BUSY, 5);
  3608. if (status & ATA_BUSY) {
  3609. msleep(2);
  3610. status = ata_busy_wait(ap, ATA_BUSY, 10);
  3611. if (status & ATA_BUSY) {
  3612. ata_port_queue_task(ap, ata_pio_task, qc, ATA_SHORT_PAUSE);
  3613. return;
  3614. }
  3615. }
  3616. /* move the HSM */
  3617. poll_next = ata_hsm_move(ap, qc, status, 1);
  3618. /* another command or interrupt handler
  3619. * may be running at this point.
  3620. */
  3621. if (poll_next)
  3622. goto fsm_start;
  3623. }
  3624. /**
  3625. * ata_qc_new - Request an available ATA command, for queueing
  3626. * @ap: Port associated with device @dev
  3627. * @dev: Device from whom we request an available command structure
  3628. *
  3629. * LOCKING:
  3630. * None.
  3631. */
  3632. static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
  3633. {
  3634. struct ata_queued_cmd *qc = NULL;
  3635. unsigned int i;
  3636. /* no command while frozen */
  3637. if (unlikely(ap->flags & ATA_FLAG_FROZEN))
  3638. return NULL;
  3639. /* the last tag is reserved for internal command. */
  3640. for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
  3641. if (!test_and_set_bit(i, &ap->qc_allocated)) {
  3642. qc = __ata_qc_from_tag(ap, i);
  3643. break;
  3644. }
  3645. if (qc)
  3646. qc->tag = i;
  3647. return qc;
  3648. }
  3649. /**
  3650. * ata_qc_new_init - Request an available ATA command, and initialize it
  3651. * @dev: Device from whom we request an available command structure
  3652. *
  3653. * LOCKING:
  3654. * None.
  3655. */
  3656. struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
  3657. {
  3658. struct ata_port *ap = dev->ap;
  3659. struct ata_queued_cmd *qc;
  3660. qc = ata_qc_new(ap);
  3661. if (qc) {
  3662. qc->scsicmd = NULL;
  3663. qc->ap = ap;
  3664. qc->dev = dev;
  3665. ata_qc_reinit(qc);
  3666. }
  3667. return qc;
  3668. }
  3669. /**
  3670. * ata_qc_free - free unused ata_queued_cmd
  3671. * @qc: Command to complete
  3672. *
  3673. * Designed to free unused ata_queued_cmd object
  3674. * in case something prevents using it.
  3675. *
  3676. * LOCKING:
  3677. * spin_lock_irqsave(host_set lock)
  3678. */
  3679. void ata_qc_free(struct ata_queued_cmd *qc)
  3680. {
  3681. struct ata_port *ap = qc->ap;
  3682. unsigned int tag;
  3683. WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  3684. qc->flags = 0;
  3685. tag = qc->tag;
  3686. if (likely(ata_tag_valid(tag))) {
  3687. qc->tag = ATA_TAG_POISON;
  3688. clear_bit(tag, &ap->qc_allocated);
  3689. }
  3690. }
  3691. void __ata_qc_complete(struct ata_queued_cmd *qc)
  3692. {
  3693. struct ata_port *ap = qc->ap;
  3694. WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  3695. WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
  3696. if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
  3697. ata_sg_clean(qc);
  3698. /* command should be marked inactive atomically with qc completion */
  3699. if (qc->tf.protocol == ATA_PROT_NCQ)
  3700. ap->sactive &= ~(1 << qc->tag);
  3701. else
  3702. ap->active_tag = ATA_TAG_POISON;
  3703. /* atapi: mark qc as inactive to prevent the interrupt handler
  3704. * from completing the command twice later, before the error handler
  3705. * is called. (when rc != 0 and atapi request sense is needed)
  3706. */
  3707. qc->flags &= ~ATA_QCFLAG_ACTIVE;
  3708. ap->qc_active &= ~(1 << qc->tag);
  3709. /* call completion callback */
  3710. qc->complete_fn(qc);
  3711. }
  3712. /**
  3713. * ata_qc_complete - Complete an active ATA command
  3714. * @qc: Command to complete
  3715. * @err_mask: ATA Status register contents
  3716. *
  3717. * Indicate to the mid and upper layers that an ATA
  3718. * command has completed, with either an ok or not-ok status.
  3719. *
  3720. * LOCKING:
  3721. * spin_lock_irqsave(host_set lock)
  3722. */
  3723. void ata_qc_complete(struct ata_queued_cmd *qc)
  3724. {
  3725. struct ata_port *ap = qc->ap;
  3726. /* XXX: New EH and old EH use different mechanisms to
  3727. * synchronize EH with regular execution path.
  3728. *
  3729. * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
  3730. * Normal execution path is responsible for not accessing a
  3731. * failed qc. libata core enforces the rule by returning NULL
  3732. * from ata_qc_from_tag() for failed qcs.
  3733. *
  3734. * Old EH depends on ata_qc_complete() nullifying completion
  3735. * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
  3736. * not synchronize with interrupt handler. Only PIO task is
  3737. * taken care of.
  3738. */
  3739. if (ap->ops->error_handler) {
  3740. WARN_ON(ap->flags & ATA_FLAG_FROZEN);
  3741. if (unlikely(qc->err_mask))
  3742. qc->flags |= ATA_QCFLAG_FAILED;
  3743. if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
  3744. if (!ata_tag_internal(qc->tag)) {
  3745. /* always fill result TF for failed qc */
  3746. ap->ops->tf_read(ap, &qc->result_tf);
  3747. ata_qc_schedule_eh(qc);
  3748. return;
  3749. }
  3750. }
  3751. /* read result TF if requested */
  3752. if (qc->flags & ATA_QCFLAG_RESULT_TF)
  3753. ap->ops->tf_read(ap, &qc->result_tf);
  3754. __ata_qc_complete(qc);
  3755. } else {
  3756. if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
  3757. return;
  3758. /* read result TF if failed or requested */
  3759. if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
  3760. ap->ops->tf_read(ap, &qc->result_tf);
  3761. __ata_qc_complete(qc);
  3762. }
  3763. }
  3764. /**
  3765. * ata_qc_complete_multiple - Complete multiple qcs successfully
  3766. * @ap: port in question
  3767. * @qc_active: new qc_active mask
  3768. * @finish_qc: LLDD callback invoked before completing a qc
  3769. *
  3770. * Complete in-flight commands. This functions is meant to be
  3771. * called from low-level driver's interrupt routine to complete
  3772. * requests normally. ap->qc_active and @qc_active is compared
  3773. * and commands are completed accordingly.
  3774. *
  3775. * LOCKING:
  3776. * spin_lock_irqsave(host_set lock)
  3777. *
  3778. * RETURNS:
  3779. * Number of completed commands on success, -errno otherwise.
  3780. */
  3781. int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
  3782. void (*finish_qc)(struct ata_queued_cmd *))
  3783. {
  3784. int nr_done = 0;
  3785. u32 done_mask;
  3786. int i;
  3787. done_mask = ap->qc_active ^ qc_active;
  3788. if (unlikely(done_mask & qc_active)) {
  3789. ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
  3790. "(%08x->%08x)\n", ap->qc_active, qc_active);
  3791. return -EINVAL;
  3792. }
  3793. for (i = 0; i < ATA_MAX_QUEUE; i++) {
  3794. struct ata_queued_cmd *qc;
  3795. if (!(done_mask & (1 << i)))
  3796. continue;
  3797. if ((qc = ata_qc_from_tag(ap, i))) {
  3798. if (finish_qc)
  3799. finish_qc(qc);
  3800. ata_qc_complete(qc);
  3801. nr_done++;
  3802. }
  3803. }
  3804. return nr_done;
  3805. }
  3806. static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
  3807. {
  3808. struct ata_port *ap = qc->ap;
  3809. switch (qc->tf.protocol) {
  3810. case ATA_PROT_NCQ:
  3811. case ATA_PROT_DMA:
  3812. case ATA_PROT_ATAPI_DMA:
  3813. return 1;
  3814. case ATA_PROT_ATAPI:
  3815. case ATA_PROT_PIO:
  3816. if (ap->flags & ATA_FLAG_PIO_DMA)
  3817. return 1;
  3818. /* fall through */
  3819. default:
  3820. return 0;
  3821. }
  3822. /* never reached */
  3823. }
  3824. /**
  3825. * ata_qc_issue - issue taskfile to device
  3826. * @qc: command to issue to device
  3827. *
  3828. * Prepare an ATA command to submission to device.
  3829. * This includes mapping the data into a DMA-able
  3830. * area, filling in the S/G table, and finally
  3831. * writing the taskfile to hardware, starting the command.
  3832. *
  3833. * LOCKING:
  3834. * spin_lock_irqsave(host_set lock)
  3835. */
  3836. void ata_qc_issue(struct ata_queued_cmd *qc)
  3837. {
  3838. struct ata_port *ap = qc->ap;
  3839. /* Make sure only one non-NCQ command is outstanding. The
  3840. * check is skipped for old EH because it reuses active qc to
  3841. * request ATAPI sense.
  3842. */
  3843. WARN_ON(ap->ops->error_handler && ata_tag_valid(ap->active_tag));
  3844. if (qc->tf.protocol == ATA_PROT_NCQ) {
  3845. WARN_ON(ap->sactive & (1 << qc->tag));
  3846. ap->sactive |= 1 << qc->tag;
  3847. } else {
  3848. WARN_ON(ap->sactive);
  3849. ap->active_tag = qc->tag;
  3850. }
  3851. qc->flags |= ATA_QCFLAG_ACTIVE;
  3852. ap->qc_active |= 1 << qc->tag;
  3853. if (ata_should_dma_map(qc)) {
  3854. if (qc->flags & ATA_QCFLAG_SG) {
  3855. if (ata_sg_setup(qc))
  3856. goto sg_err;
  3857. } else if (qc->flags & ATA_QCFLAG_SINGLE) {
  3858. if (ata_sg_setup_one(qc))
  3859. goto sg_err;
  3860. }
  3861. } else {
  3862. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  3863. }
  3864. ap->ops->qc_prep(qc);
  3865. qc->err_mask |= ap->ops->qc_issue(qc);
  3866. if (unlikely(qc->err_mask))
  3867. goto err;
  3868. return;
  3869. sg_err:
  3870. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  3871. qc->err_mask |= AC_ERR_SYSTEM;
  3872. err:
  3873. ata_qc_complete(qc);
  3874. }
  3875. /**
  3876. * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
  3877. * @qc: command to issue to device
  3878. *
  3879. * Using various libata functions and hooks, this function
  3880. * starts an ATA command. ATA commands are grouped into
  3881. * classes called "protocols", and issuing each type of protocol
  3882. * is slightly different.
  3883. *
  3884. * May be used as the qc_issue() entry in ata_port_operations.
  3885. *
  3886. * LOCKING:
  3887. * spin_lock_irqsave(host_set lock)
  3888. *
  3889. * RETURNS:
  3890. * Zero on success, AC_ERR_* mask on failure
  3891. */
  3892. unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
  3893. {
  3894. struct ata_port *ap = qc->ap;
  3895. /* Use polling pio if the LLD doesn't handle
  3896. * interrupt driven pio and atapi CDB interrupt.
  3897. */
  3898. if (ap->flags & ATA_FLAG_PIO_POLLING) {
  3899. switch (qc->tf.protocol) {
  3900. case ATA_PROT_PIO:
  3901. case ATA_PROT_ATAPI:
  3902. case ATA_PROT_ATAPI_NODATA:
  3903. qc->tf.flags |= ATA_TFLAG_POLLING;
  3904. break;
  3905. case ATA_PROT_ATAPI_DMA:
  3906. if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
  3907. /* see ata_dma_blacklisted() */
  3908. BUG();
  3909. break;
  3910. default:
  3911. break;
  3912. }
  3913. }
  3914. /* select the device */
  3915. ata_dev_select(ap, qc->dev->devno, 1, 0);
  3916. /* start the command */
  3917. switch (qc->tf.protocol) {
  3918. case ATA_PROT_NODATA:
  3919. if (qc->tf.flags & ATA_TFLAG_POLLING)
  3920. ata_qc_set_polling(qc);
  3921. ata_tf_to_host(ap, &qc->tf);
  3922. ap->hsm_task_state = HSM_ST_LAST;
  3923. if (qc->tf.flags & ATA_TFLAG_POLLING)
  3924. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  3925. break;
  3926. case ATA_PROT_DMA:
  3927. WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
  3928. ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
  3929. ap->ops->bmdma_setup(qc); /* set up bmdma */
  3930. ap->ops->bmdma_start(qc); /* initiate bmdma */
  3931. ap->hsm_task_state = HSM_ST_LAST;
  3932. break;
  3933. case ATA_PROT_PIO:
  3934. if (qc->tf.flags & ATA_TFLAG_POLLING)
  3935. ata_qc_set_polling(qc);
  3936. ata_tf_to_host(ap, &qc->tf);
  3937. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  3938. /* PIO data out protocol */
  3939. ap->hsm_task_state = HSM_ST_FIRST;
  3940. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  3941. /* always send first data block using
  3942. * the ata_pio_task() codepath.
  3943. */
  3944. } else {
  3945. /* PIO data in protocol */
  3946. ap->hsm_task_state = HSM_ST;
  3947. if (qc->tf.flags & ATA_TFLAG_POLLING)
  3948. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  3949. /* if polling, ata_pio_task() handles the rest.
  3950. * otherwise, interrupt handler takes over from here.
  3951. */
  3952. }
  3953. break;
  3954. case ATA_PROT_ATAPI:
  3955. case ATA_PROT_ATAPI_NODATA:
  3956. if (qc->tf.flags & ATA_TFLAG_POLLING)
  3957. ata_qc_set_polling(qc);
  3958. ata_tf_to_host(ap, &qc->tf);
  3959. ap->hsm_task_state = HSM_ST_FIRST;
  3960. /* send cdb by polling if no cdb interrupt */
  3961. if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
  3962. (qc->tf.flags & ATA_TFLAG_POLLING))
  3963. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  3964. break;
  3965. case ATA_PROT_ATAPI_DMA:
  3966. WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
  3967. ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
  3968. ap->ops->bmdma_setup(qc); /* set up bmdma */
  3969. ap->hsm_task_state = HSM_ST_FIRST;
  3970. /* send cdb by polling if no cdb interrupt */
  3971. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  3972. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  3973. break;
  3974. default:
  3975. WARN_ON(1);
  3976. return AC_ERR_SYSTEM;
  3977. }
  3978. return 0;
  3979. }
  3980. /**
  3981. * ata_host_intr - Handle host interrupt for given (port, task)
  3982. * @ap: Port on which interrupt arrived (possibly...)
  3983. * @qc: Taskfile currently active in engine
  3984. *
  3985. * Handle host interrupt for given queued command. Currently,
  3986. * only DMA interrupts are handled. All other commands are
  3987. * handled via polling with interrupts disabled (nIEN bit).
  3988. *
  3989. * LOCKING:
  3990. * spin_lock_irqsave(host_set lock)
  3991. *
  3992. * RETURNS:
  3993. * One if interrupt was handled, zero if not (shared irq).
  3994. */
  3995. inline unsigned int ata_host_intr (struct ata_port *ap,
  3996. struct ata_queued_cmd *qc)
  3997. {
  3998. u8 status, host_stat = 0;
  3999. VPRINTK("ata%u: protocol %d task_state %d\n",
  4000. ap->id, qc->tf.protocol, ap->hsm_task_state);
  4001. /* Check whether we are expecting interrupt in this state */
  4002. switch (ap->hsm_task_state) {
  4003. case HSM_ST_FIRST:
  4004. /* Some pre-ATAPI-4 devices assert INTRQ
  4005. * at this state when ready to receive CDB.
  4006. */
  4007. /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
  4008. * The flag was turned on only for atapi devices.
  4009. * No need to check is_atapi_taskfile(&qc->tf) again.
  4010. */
  4011. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  4012. goto idle_irq;
  4013. break;
  4014. case HSM_ST_LAST:
  4015. if (qc->tf.protocol == ATA_PROT_DMA ||
  4016. qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
  4017. /* check status of DMA engine */
  4018. host_stat = ap->ops->bmdma_status(ap);
  4019. VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
  4020. /* if it's not our irq... */
  4021. if (!(host_stat & ATA_DMA_INTR))
  4022. goto idle_irq;
  4023. /* before we do anything else, clear DMA-Start bit */
  4024. ap->ops->bmdma_stop(qc);
  4025. if (unlikely(host_stat & ATA_DMA_ERR)) {
  4026. /* error when transfering data to/from memory */
  4027. qc->err_mask |= AC_ERR_HOST_BUS;
  4028. ap->hsm_task_state = HSM_ST_ERR;
  4029. }
  4030. }
  4031. break;
  4032. case HSM_ST:
  4033. break;
  4034. default:
  4035. goto idle_irq;
  4036. }
  4037. /* check altstatus */
  4038. status = ata_altstatus(ap);
  4039. if (status & ATA_BUSY)
  4040. goto idle_irq;
  4041. /* check main status, clearing INTRQ */
  4042. status = ata_chk_status(ap);
  4043. if (unlikely(status & ATA_BUSY))
  4044. goto idle_irq;
  4045. /* ack bmdma irq events */
  4046. ap->ops->irq_clear(ap);
  4047. ata_hsm_move(ap, qc, status, 0);
  4048. return 1; /* irq handled */
  4049. idle_irq:
  4050. ap->stats.idle_irq++;
  4051. #ifdef ATA_IRQ_TRAP
  4052. if ((ap->stats.idle_irq % 1000) == 0) {
  4053. ata_irq_ack(ap, 0); /* debug trap */
  4054. ata_port_printk(ap, KERN_WARNING, "irq trap\n");
  4055. return 1;
  4056. }
  4057. #endif
  4058. return 0; /* irq not handled */
  4059. }
  4060. /**
  4061. * ata_interrupt - Default ATA host interrupt handler
  4062. * @irq: irq line (unused)
  4063. * @dev_instance: pointer to our ata_host_set information structure
  4064. * @regs: unused
  4065. *
  4066. * Default interrupt handler for PCI IDE devices. Calls
  4067. * ata_host_intr() for each port that is not disabled.
  4068. *
  4069. * LOCKING:
  4070. * Obtains host_set lock during operation.
  4071. *
  4072. * RETURNS:
  4073. * IRQ_NONE or IRQ_HANDLED.
  4074. */
  4075. irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
  4076. {
  4077. struct ata_host_set *host_set = dev_instance;
  4078. unsigned int i;
  4079. unsigned int handled = 0;
  4080. unsigned long flags;
  4081. /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
  4082. spin_lock_irqsave(&host_set->lock, flags);
  4083. for (i = 0; i < host_set->n_ports; i++) {
  4084. struct ata_port *ap;
  4085. ap = host_set->ports[i];
  4086. if (ap &&
  4087. !(ap->flags & ATA_FLAG_DISABLED)) {
  4088. struct ata_queued_cmd *qc;
  4089. qc = ata_qc_from_tag(ap, ap->active_tag);
  4090. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
  4091. (qc->flags & ATA_QCFLAG_ACTIVE))
  4092. handled |= ata_host_intr(ap, qc);
  4093. }
  4094. }
  4095. spin_unlock_irqrestore(&host_set->lock, flags);
  4096. return IRQ_RETVAL(handled);
  4097. }
  4098. /**
  4099. * sata_scr_valid - test whether SCRs are accessible
  4100. * @ap: ATA port to test SCR accessibility for
  4101. *
  4102. * Test whether SCRs are accessible for @ap.
  4103. *
  4104. * LOCKING:
  4105. * None.
  4106. *
  4107. * RETURNS:
  4108. * 1 if SCRs are accessible, 0 otherwise.
  4109. */
  4110. int sata_scr_valid(struct ata_port *ap)
  4111. {
  4112. return ap->cbl == ATA_CBL_SATA && ap->ops->scr_read;
  4113. }
  4114. /**
  4115. * sata_scr_read - read SCR register of the specified port
  4116. * @ap: ATA port to read SCR for
  4117. * @reg: SCR to read
  4118. * @val: Place to store read value
  4119. *
  4120. * Read SCR register @reg of @ap into *@val. This function is
  4121. * guaranteed to succeed if the cable type of the port is SATA
  4122. * and the port implements ->scr_read.
  4123. *
  4124. * LOCKING:
  4125. * None.
  4126. *
  4127. * RETURNS:
  4128. * 0 on success, negative errno on failure.
  4129. */
  4130. int sata_scr_read(struct ata_port *ap, int reg, u32 *val)
  4131. {
  4132. if (sata_scr_valid(ap)) {
  4133. *val = ap->ops->scr_read(ap, reg);
  4134. return 0;
  4135. }
  4136. return -EOPNOTSUPP;
  4137. }
  4138. /**
  4139. * sata_scr_write - write SCR register of the specified port
  4140. * @ap: ATA port to write SCR for
  4141. * @reg: SCR to write
  4142. * @val: value to write
  4143. *
  4144. * Write @val to SCR register @reg of @ap. This function is
  4145. * guaranteed to succeed if the cable type of the port is SATA
  4146. * and the port implements ->scr_read.
  4147. *
  4148. * LOCKING:
  4149. * None.
  4150. *
  4151. * RETURNS:
  4152. * 0 on success, negative errno on failure.
  4153. */
  4154. int sata_scr_write(struct ata_port *ap, int reg, u32 val)
  4155. {
  4156. if (sata_scr_valid(ap)) {
  4157. ap->ops->scr_write(ap, reg, val);
  4158. return 0;
  4159. }
  4160. return -EOPNOTSUPP;
  4161. }
  4162. /**
  4163. * sata_scr_write_flush - write SCR register of the specified port and flush
  4164. * @ap: ATA port to write SCR for
  4165. * @reg: SCR to write
  4166. * @val: value to write
  4167. *
  4168. * This function is identical to sata_scr_write() except that this
  4169. * function performs flush after writing to the register.
  4170. *
  4171. * LOCKING:
  4172. * None.
  4173. *
  4174. * RETURNS:
  4175. * 0 on success, negative errno on failure.
  4176. */
  4177. int sata_scr_write_flush(struct ata_port *ap, int reg, u32 val)
  4178. {
  4179. if (sata_scr_valid(ap)) {
  4180. ap->ops->scr_write(ap, reg, val);
  4181. ap->ops->scr_read(ap, reg);
  4182. return 0;
  4183. }
  4184. return -EOPNOTSUPP;
  4185. }
  4186. /**
  4187. * ata_port_online - test whether the given port is online
  4188. * @ap: ATA port to test
  4189. *
  4190. * Test whether @ap is online. Note that this function returns 0
  4191. * if online status of @ap cannot be obtained, so
  4192. * ata_port_online(ap) != !ata_port_offline(ap).
  4193. *
  4194. * LOCKING:
  4195. * None.
  4196. *
  4197. * RETURNS:
  4198. * 1 if the port online status is available and online.
  4199. */
  4200. int ata_port_online(struct ata_port *ap)
  4201. {
  4202. u32 sstatus;
  4203. if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) == 0x3)
  4204. return 1;
  4205. return 0;
  4206. }
  4207. /**
  4208. * ata_port_offline - test whether the given port is offline
  4209. * @ap: ATA port to test
  4210. *
  4211. * Test whether @ap is offline. Note that this function returns
  4212. * 0 if offline status of @ap cannot be obtained, so
  4213. * ata_port_online(ap) != !ata_port_offline(ap).
  4214. *
  4215. * LOCKING:
  4216. * None.
  4217. *
  4218. * RETURNS:
  4219. * 1 if the port offline status is available and offline.
  4220. */
  4221. int ata_port_offline(struct ata_port *ap)
  4222. {
  4223. u32 sstatus;
  4224. if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) != 0x3)
  4225. return 1;
  4226. return 0;
  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);