libata-core.c 122 KB

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