libata-core.c 118 KB

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