libata-core.c 119 KB

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