libata-core.c 118 KB

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