libata-core.c 143 KB

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