libata-core.c 119 KB

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