libata-core.c 119 KB

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