libata-core.c 113 KB

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