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