libata-core.c 119 KB

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