libata-core.c 122 KB

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