libata-core.c 119 KB

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