libata-core.c 119 KB

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