libata-core.c 120 KB

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