libata-core.c 119 KB

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