libata-core.c 119 KB

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