libata-core.c 104 KB

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