libata-core.c 118 KB

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