libata-core.c 105 KB

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