libata-core.c 109 KB

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