libata-core.c 131 KB

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