libata-core.c 120 KB

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