libata-core.c 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127
  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. * @verbose: fail verbosely
  2050. * @classes: resulting classes of attached devices
  2051. *
  2052. * Reset host port using ATA SRST. This function is to be used
  2053. * as standard callback for ata_drive_*_reset() functions.
  2054. *
  2055. * LOCKING:
  2056. * Kernel thread context (may sleep)
  2057. *
  2058. * RETURNS:
  2059. * 0 on success, -errno otherwise.
  2060. */
  2061. int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
  2062. {
  2063. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  2064. unsigned int devmask = 0, err_mask;
  2065. u8 err;
  2066. DPRINTK("ENTER\n");
  2067. if (ap->ops->scr_read && !sata_dev_present(ap)) {
  2068. classes[0] = ATA_DEV_NONE;
  2069. goto out;
  2070. }
  2071. /* determine if device 0/1 are present */
  2072. if (ata_devchk(ap, 0))
  2073. devmask |= (1 << 0);
  2074. if (slave_possible && ata_devchk(ap, 1))
  2075. devmask |= (1 << 1);
  2076. /* select device 0 again */
  2077. ap->ops->dev_select(ap, 0);
  2078. /* issue bus reset */
  2079. DPRINTK("about to softreset, devmask=%x\n", devmask);
  2080. err_mask = ata_bus_softreset(ap, devmask);
  2081. if (err_mask) {
  2082. if (verbose)
  2083. printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
  2084. ap->id, err_mask);
  2085. else
  2086. DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
  2087. err_mask);
  2088. return -EIO;
  2089. }
  2090. /* determine by signature whether we have ATA or ATAPI devices */
  2091. classes[0] = ata_dev_try_classify(ap, 0, &err);
  2092. if (slave_possible && err != 0x81)
  2093. classes[1] = ata_dev_try_classify(ap, 1, &err);
  2094. out:
  2095. DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
  2096. return 0;
  2097. }
  2098. /**
  2099. * sata_std_hardreset - reset host port via SATA phy reset
  2100. * @ap: port to reset
  2101. * @verbose: fail verbosely
  2102. * @class: resulting class of attached device
  2103. *
  2104. * SATA phy-reset host port using DET bits of SControl register.
  2105. * This function is to be used as standard callback for
  2106. * ata_drive_*_reset().
  2107. *
  2108. * LOCKING:
  2109. * Kernel thread context (may sleep)
  2110. *
  2111. * RETURNS:
  2112. * 0 on success, -errno otherwise.
  2113. */
  2114. int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
  2115. {
  2116. u32 scontrol;
  2117. DPRINTK("ENTER\n");
  2118. if (ata_set_sata_spd_needed(ap)) {
  2119. /* SATA spec says nothing about how to reconfigure
  2120. * spd. To be on the safe side, turn off phy during
  2121. * reconfiguration. This works for at least ICH7 AHCI
  2122. * and Sil3124.
  2123. */
  2124. scontrol = scr_read(ap, SCR_CONTROL);
  2125. scontrol = (scontrol & 0x0f0) | 0x302;
  2126. scr_write_flush(ap, SCR_CONTROL, scontrol);
  2127. ata_set_sata_spd(ap);
  2128. }
  2129. /* issue phy wake/reset */
  2130. scontrol = scr_read(ap, SCR_CONTROL);
  2131. scontrol = (scontrol & 0x0f0) | 0x301;
  2132. scr_write_flush(ap, SCR_CONTROL, scontrol);
  2133. /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
  2134. * 10.4.2 says at least 1 ms.
  2135. */
  2136. msleep(1);
  2137. /* bring phy back */
  2138. sata_phy_resume(ap);
  2139. /* TODO: phy layer with polling, timeouts, etc. */
  2140. if (!sata_dev_present(ap)) {
  2141. *class = ATA_DEV_NONE;
  2142. DPRINTK("EXIT, link offline\n");
  2143. return 0;
  2144. }
  2145. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  2146. if (verbose)
  2147. printk(KERN_ERR "ata%u: COMRESET failed "
  2148. "(device not ready)\n", ap->id);
  2149. else
  2150. DPRINTK("EXIT, device not ready\n");
  2151. return -EIO;
  2152. }
  2153. ap->ops->dev_select(ap, 0); /* probably unnecessary */
  2154. *class = ata_dev_try_classify(ap, 0, NULL);
  2155. DPRINTK("EXIT, class=%u\n", *class);
  2156. return 0;
  2157. }
  2158. /**
  2159. * ata_std_postreset - standard postreset callback
  2160. * @ap: the target ata_port
  2161. * @classes: classes of attached devices
  2162. *
  2163. * This function is invoked after a successful reset. Note that
  2164. * the device might have been reset more than once using
  2165. * different reset methods before postreset is invoked.
  2166. *
  2167. * This function is to be used as standard callback for
  2168. * ata_drive_*_reset().
  2169. *
  2170. * LOCKING:
  2171. * Kernel thread context (may sleep)
  2172. */
  2173. void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
  2174. {
  2175. DPRINTK("ENTER\n");
  2176. /* print link status */
  2177. if (ap->cbl == ATA_CBL_SATA)
  2178. sata_print_link_status(ap);
  2179. /* re-enable interrupts */
  2180. if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
  2181. ata_irq_on(ap);
  2182. /* is double-select really necessary? */
  2183. if (classes[0] != ATA_DEV_NONE)
  2184. ap->ops->dev_select(ap, 1);
  2185. if (classes[1] != ATA_DEV_NONE)
  2186. ap->ops->dev_select(ap, 0);
  2187. /* bail out if no device is present */
  2188. if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
  2189. DPRINTK("EXIT, no device\n");
  2190. return;
  2191. }
  2192. /* set up device control */
  2193. if (ap->ioaddr.ctl_addr) {
  2194. if (ap->flags & ATA_FLAG_MMIO)
  2195. writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
  2196. else
  2197. outb(ap->ctl, ap->ioaddr.ctl_addr);
  2198. }
  2199. DPRINTK("EXIT\n");
  2200. }
  2201. /**
  2202. * ata_std_probe_reset - standard probe reset method
  2203. * @ap: prot to perform probe-reset
  2204. * @classes: resulting classes of attached devices
  2205. *
  2206. * The stock off-the-shelf ->probe_reset method.
  2207. *
  2208. * LOCKING:
  2209. * Kernel thread context (may sleep)
  2210. *
  2211. * RETURNS:
  2212. * 0 on success, -errno otherwise.
  2213. */
  2214. int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
  2215. {
  2216. ata_reset_fn_t hardreset;
  2217. hardreset = NULL;
  2218. if (ap->cbl == ATA_CBL_SATA && ap->ops->scr_read)
  2219. hardreset = sata_std_hardreset;
  2220. return ata_drive_probe_reset(ap, ata_std_probeinit,
  2221. ata_std_softreset, hardreset,
  2222. ata_std_postreset, classes);
  2223. }
  2224. int ata_do_reset(struct ata_port *ap,
  2225. ata_reset_fn_t reset, ata_postreset_fn_t postreset,
  2226. int verbose, unsigned int *classes)
  2227. {
  2228. int i, rc;
  2229. for (i = 0; i < ATA_MAX_DEVICES; i++)
  2230. classes[i] = ATA_DEV_UNKNOWN;
  2231. rc = reset(ap, verbose, classes);
  2232. if (rc)
  2233. return rc;
  2234. /* If any class isn't ATA_DEV_UNKNOWN, consider classification
  2235. * is complete and convert all ATA_DEV_UNKNOWN to
  2236. * ATA_DEV_NONE.
  2237. */
  2238. for (i = 0; i < ATA_MAX_DEVICES; i++)
  2239. if (classes[i] != ATA_DEV_UNKNOWN)
  2240. break;
  2241. if (i < ATA_MAX_DEVICES)
  2242. for (i = 0; i < ATA_MAX_DEVICES; i++)
  2243. if (classes[i] == ATA_DEV_UNKNOWN)
  2244. classes[i] = ATA_DEV_NONE;
  2245. if (postreset)
  2246. postreset(ap, classes);
  2247. return 0;
  2248. }
  2249. /**
  2250. * ata_drive_probe_reset - Perform probe reset with given methods
  2251. * @ap: port to reset
  2252. * @probeinit: probeinit method (can be NULL)
  2253. * @softreset: softreset method (can be NULL)
  2254. * @hardreset: hardreset method (can be NULL)
  2255. * @postreset: postreset method (can be NULL)
  2256. * @classes: resulting classes of attached devices
  2257. *
  2258. * Reset the specified port and classify attached devices using
  2259. * given methods. This function prefers softreset but tries all
  2260. * possible reset sequences to reset and classify devices. This
  2261. * function is intended to be used for constructing ->probe_reset
  2262. * callback by low level drivers.
  2263. *
  2264. * Reset methods should follow the following rules.
  2265. *
  2266. * - Return 0 on sucess, -errno on failure.
  2267. * - If classification is supported, fill classes[] with
  2268. * recognized class codes.
  2269. * - If classification is not supported, leave classes[] alone.
  2270. * - If verbose is non-zero, print error message on failure;
  2271. * otherwise, shut up.
  2272. *
  2273. * LOCKING:
  2274. * Kernel thread context (may sleep)
  2275. *
  2276. * RETURNS:
  2277. * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
  2278. * if classification fails, and any error code from reset
  2279. * methods.
  2280. */
  2281. int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
  2282. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  2283. ata_postreset_fn_t postreset, unsigned int *classes)
  2284. {
  2285. int rc = -EINVAL;
  2286. if (probeinit)
  2287. probeinit(ap);
  2288. if (softreset && !ata_set_sata_spd_needed(ap)) {
  2289. rc = ata_do_reset(ap, softreset, postreset, 0, classes);
  2290. if (rc == 0 && classes[0] != ATA_DEV_UNKNOWN)
  2291. goto done;
  2292. printk(KERN_INFO "ata%u: softreset failed, will try "
  2293. "hardreset in 5 secs\n", ap->id);
  2294. ssleep(5);
  2295. }
  2296. if (!hardreset)
  2297. goto done;
  2298. while (1) {
  2299. rc = ata_do_reset(ap, hardreset, postreset, 0, classes);
  2300. if (rc == 0) {
  2301. if (classes[0] != ATA_DEV_UNKNOWN)
  2302. goto done;
  2303. break;
  2304. }
  2305. if (ata_down_sata_spd_limit(ap))
  2306. goto done;
  2307. printk(KERN_INFO "ata%u: hardreset failed, will retry "
  2308. "in 5 secs\n", ap->id);
  2309. ssleep(5);
  2310. }
  2311. if (softreset) {
  2312. printk(KERN_INFO "ata%u: hardreset succeeded without "
  2313. "classification, will retry softreset in 5 secs\n",
  2314. ap->id);
  2315. ssleep(5);
  2316. rc = ata_do_reset(ap, softreset, postreset, 0, classes);
  2317. }
  2318. done:
  2319. if (rc == 0 && classes[0] == ATA_DEV_UNKNOWN)
  2320. rc = -ENODEV;
  2321. return rc;
  2322. }
  2323. /**
  2324. * ata_dev_same_device - Determine whether new ID matches configured device
  2325. * @ap: port on which the device to compare against resides
  2326. * @dev: device to compare against
  2327. * @new_class: class of the new device
  2328. * @new_id: IDENTIFY page of the new device
  2329. *
  2330. * Compare @new_class and @new_id against @dev and determine
  2331. * whether @dev is the device indicated by @new_class and
  2332. * @new_id.
  2333. *
  2334. * LOCKING:
  2335. * None.
  2336. *
  2337. * RETURNS:
  2338. * 1 if @dev matches @new_class and @new_id, 0 otherwise.
  2339. */
  2340. static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
  2341. unsigned int new_class, const u16 *new_id)
  2342. {
  2343. const u16 *old_id = dev->id;
  2344. unsigned char model[2][41], serial[2][21];
  2345. u64 new_n_sectors;
  2346. if (dev->class != new_class) {
  2347. printk(KERN_INFO
  2348. "ata%u: dev %u class mismatch %d != %d\n",
  2349. ap->id, dev->devno, dev->class, new_class);
  2350. return 0;
  2351. }
  2352. ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
  2353. ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
  2354. ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
  2355. ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
  2356. new_n_sectors = ata_id_n_sectors(new_id);
  2357. if (strcmp(model[0], model[1])) {
  2358. printk(KERN_INFO
  2359. "ata%u: dev %u model number mismatch '%s' != '%s'\n",
  2360. ap->id, dev->devno, model[0], model[1]);
  2361. return 0;
  2362. }
  2363. if (strcmp(serial[0], serial[1])) {
  2364. printk(KERN_INFO
  2365. "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
  2366. ap->id, dev->devno, serial[0], serial[1]);
  2367. return 0;
  2368. }
  2369. if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
  2370. printk(KERN_INFO
  2371. "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
  2372. ap->id, dev->devno, (unsigned long long)dev->n_sectors,
  2373. (unsigned long long)new_n_sectors);
  2374. return 0;
  2375. }
  2376. return 1;
  2377. }
  2378. /**
  2379. * ata_dev_revalidate - Revalidate ATA device
  2380. * @ap: port on which the device to revalidate resides
  2381. * @dev: device to revalidate
  2382. * @post_reset: is this revalidation after reset?
  2383. *
  2384. * Re-read IDENTIFY page and make sure @dev is still attached to
  2385. * the port.
  2386. *
  2387. * LOCKING:
  2388. * Kernel thread context (may sleep)
  2389. *
  2390. * RETURNS:
  2391. * 0 on success, negative errno otherwise
  2392. */
  2393. int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
  2394. int post_reset)
  2395. {
  2396. unsigned int class = dev->class;
  2397. u16 *id = NULL;
  2398. int rc;
  2399. if (!ata_dev_enabled(dev)) {
  2400. rc = -ENODEV;
  2401. goto fail;
  2402. }
  2403. /* allocate & read ID data */
  2404. rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
  2405. if (rc)
  2406. goto fail;
  2407. /* is the device still there? */
  2408. if (!ata_dev_same_device(ap, dev, class, id)) {
  2409. rc = -ENODEV;
  2410. goto fail;
  2411. }
  2412. kfree(dev->id);
  2413. dev->id = id;
  2414. /* configure device according to the new ID */
  2415. rc = ata_dev_configure(ap, dev, 0);
  2416. if (rc == 0)
  2417. return 0;
  2418. fail:
  2419. printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
  2420. ap->id, dev->devno, rc);
  2421. kfree(id);
  2422. return rc;
  2423. }
  2424. static const char * const ata_dma_blacklist [] = {
  2425. "WDC AC11000H", NULL,
  2426. "WDC AC22100H", NULL,
  2427. "WDC AC32500H", NULL,
  2428. "WDC AC33100H", NULL,
  2429. "WDC AC31600H", NULL,
  2430. "WDC AC32100H", "24.09P07",
  2431. "WDC AC23200L", "21.10N21",
  2432. "Compaq CRD-8241B", NULL,
  2433. "CRD-8400B", NULL,
  2434. "CRD-8480B", NULL,
  2435. "CRD-8482B", NULL,
  2436. "CRD-84", NULL,
  2437. "SanDisk SDP3B", NULL,
  2438. "SanDisk SDP3B-64", NULL,
  2439. "SANYO CD-ROM CRD", NULL,
  2440. "HITACHI CDR-8", NULL,
  2441. "HITACHI CDR-8335", NULL,
  2442. "HITACHI CDR-8435", NULL,
  2443. "Toshiba CD-ROM XM-6202B", NULL,
  2444. "TOSHIBA CD-ROM XM-1702BC", NULL,
  2445. "CD-532E-A", NULL,
  2446. "E-IDE CD-ROM CR-840", NULL,
  2447. "CD-ROM Drive/F5A", NULL,
  2448. "WPI CDD-820", NULL,
  2449. "SAMSUNG CD-ROM SC-148C", NULL,
  2450. "SAMSUNG CD-ROM SC", NULL,
  2451. "SanDisk SDP3B-64", NULL,
  2452. "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,
  2453. "_NEC DV5800A", NULL,
  2454. "SAMSUNG CD-ROM SN-124", "N001"
  2455. };
  2456. static int ata_strim(char *s, size_t len)
  2457. {
  2458. len = strnlen(s, len);
  2459. /* ATAPI specifies that empty space is blank-filled; remove blanks */
  2460. while ((len > 0) && (s[len - 1] == ' ')) {
  2461. len--;
  2462. s[len] = 0;
  2463. }
  2464. return len;
  2465. }
  2466. static int ata_dma_blacklisted(const struct ata_device *dev)
  2467. {
  2468. unsigned char model_num[40];
  2469. unsigned char model_rev[16];
  2470. unsigned int nlen, rlen;
  2471. int i;
  2472. ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
  2473. sizeof(model_num));
  2474. ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS,
  2475. sizeof(model_rev));
  2476. nlen = ata_strim(model_num, sizeof(model_num));
  2477. rlen = ata_strim(model_rev, sizeof(model_rev));
  2478. for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i += 2) {
  2479. if (!strncmp(ata_dma_blacklist[i], model_num, nlen)) {
  2480. if (ata_dma_blacklist[i+1] == NULL)
  2481. return 1;
  2482. if (!strncmp(ata_dma_blacklist[i], model_rev, rlen))
  2483. return 1;
  2484. }
  2485. }
  2486. return 0;
  2487. }
  2488. /**
  2489. * ata_dev_xfermask - Compute supported xfermask of the given device
  2490. * @ap: Port on which the device to compute xfermask for resides
  2491. * @dev: Device to compute xfermask for
  2492. *
  2493. * Compute supported xfermask of @dev and store it in
  2494. * dev->*_mask. This function is responsible for applying all
  2495. * known limits including host controller limits, device
  2496. * blacklist, etc...
  2497. *
  2498. * FIXME: The current implementation limits all transfer modes to
  2499. * the fastest of the lowested device on the port. This is not
  2500. * required on most controllers.
  2501. *
  2502. * LOCKING:
  2503. * None.
  2504. */
  2505. static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev)
  2506. {
  2507. struct ata_host_set *hs = ap->host_set;
  2508. unsigned long xfer_mask;
  2509. int i;
  2510. xfer_mask = ata_pack_xfermask(ap->pio_mask,
  2511. ap->mwdma_mask, ap->udma_mask);
  2512. /* Apply cable rule here. Don't apply it early because when
  2513. * we handle hot plug the cable type can itself change.
  2514. */
  2515. if (ap->cbl == ATA_CBL_PATA40)
  2516. xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
  2517. /* FIXME: Use port-wide xfermask for now */
  2518. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  2519. struct ata_device *d = &ap->device[i];
  2520. if (ata_dev_absent(d))
  2521. continue;
  2522. if (ata_dev_disabled(d)) {
  2523. /* to avoid violating device selection timing */
  2524. xfer_mask &= ata_pack_xfermask(d->pio_mask,
  2525. UINT_MAX, UINT_MAX);
  2526. continue;
  2527. }
  2528. xfer_mask &= ata_pack_xfermask(d->pio_mask,
  2529. d->mwdma_mask, d->udma_mask);
  2530. xfer_mask &= ata_id_xfermask(d->id);
  2531. if (ata_dma_blacklisted(d))
  2532. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  2533. }
  2534. if (ata_dma_blacklisted(dev))
  2535. printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, "
  2536. "disabling DMA\n", ap->id, dev->devno);
  2537. if (hs->flags & ATA_HOST_SIMPLEX) {
  2538. if (hs->simplex_claimed)
  2539. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  2540. }
  2541. if (ap->ops->mode_filter)
  2542. xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask);
  2543. ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
  2544. &dev->mwdma_mask, &dev->udma_mask);
  2545. }
  2546. /**
  2547. * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
  2548. * @ap: Port associated with device @dev
  2549. * @dev: Device to which command will be sent
  2550. *
  2551. * Issue SET FEATURES - XFER MODE command to device @dev
  2552. * on port @ap.
  2553. *
  2554. * LOCKING:
  2555. * PCI/etc. bus probe sem.
  2556. *
  2557. * RETURNS:
  2558. * 0 on success, AC_ERR_* mask otherwise.
  2559. */
  2560. static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
  2561. struct ata_device *dev)
  2562. {
  2563. struct ata_taskfile tf;
  2564. unsigned int err_mask;
  2565. /* set up set-features taskfile */
  2566. DPRINTK("set features - xfer mode\n");
  2567. ata_tf_init(ap, &tf, dev->devno);
  2568. tf.command = ATA_CMD_SET_FEATURES;
  2569. tf.feature = SETFEATURES_XFER;
  2570. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2571. tf.protocol = ATA_PROT_NODATA;
  2572. tf.nsect = dev->xfer_mode;
  2573. err_mask = ata_exec_internal(ap, dev, &tf, NULL, DMA_NONE, NULL, 0);
  2574. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  2575. return err_mask;
  2576. }
  2577. /**
  2578. * ata_dev_init_params - Issue INIT DEV PARAMS command
  2579. * @ap: Port associated with device @dev
  2580. * @dev: Device to which command will be sent
  2581. *
  2582. * LOCKING:
  2583. * Kernel thread context (may sleep)
  2584. *
  2585. * RETURNS:
  2586. * 0 on success, AC_ERR_* mask otherwise.
  2587. */
  2588. static unsigned int ata_dev_init_params(struct ata_port *ap,
  2589. struct ata_device *dev,
  2590. u16 heads,
  2591. u16 sectors)
  2592. {
  2593. struct ata_taskfile tf;
  2594. unsigned int err_mask;
  2595. /* Number of sectors per track 1-255. Number of heads 1-16 */
  2596. if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
  2597. return AC_ERR_INVALID;
  2598. /* set up init dev params taskfile */
  2599. DPRINTK("init dev params \n");
  2600. ata_tf_init(ap, &tf, dev->devno);
  2601. tf.command = ATA_CMD_INIT_DEV_PARAMS;
  2602. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2603. tf.protocol = ATA_PROT_NODATA;
  2604. tf.nsect = sectors;
  2605. tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
  2606. err_mask = ata_exec_internal(ap, dev, &tf, NULL, DMA_NONE, NULL, 0);
  2607. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  2608. return err_mask;
  2609. }
  2610. /**
  2611. * ata_sg_clean - Unmap DMA memory associated with command
  2612. * @qc: Command containing DMA memory to be released
  2613. *
  2614. * Unmap all mapped DMA memory associated with this command.
  2615. *
  2616. * LOCKING:
  2617. * spin_lock_irqsave(host_set lock)
  2618. */
  2619. static void ata_sg_clean(struct ata_queued_cmd *qc)
  2620. {
  2621. struct ata_port *ap = qc->ap;
  2622. struct scatterlist *sg = qc->__sg;
  2623. int dir = qc->dma_dir;
  2624. void *pad_buf = NULL;
  2625. WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
  2626. WARN_ON(sg == NULL);
  2627. if (qc->flags & ATA_QCFLAG_SINGLE)
  2628. WARN_ON(qc->n_elem > 1);
  2629. VPRINTK("unmapping %u sg elements\n", qc->n_elem);
  2630. /* if we padded the buffer out to 32-bit bound, and data
  2631. * xfer direction is from-device, we must copy from the
  2632. * pad buffer back into the supplied buffer
  2633. */
  2634. if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
  2635. pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  2636. if (qc->flags & ATA_QCFLAG_SG) {
  2637. if (qc->n_elem)
  2638. dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
  2639. /* restore last sg */
  2640. sg[qc->orig_n_elem - 1].length += qc->pad_len;
  2641. if (pad_buf) {
  2642. struct scatterlist *psg = &qc->pad_sgent;
  2643. void *addr = kmap_atomic(psg->page, KM_IRQ0);
  2644. memcpy(addr + psg->offset, pad_buf, qc->pad_len);
  2645. kunmap_atomic(addr, KM_IRQ0);
  2646. }
  2647. } else {
  2648. if (qc->n_elem)
  2649. dma_unmap_single(ap->dev,
  2650. sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
  2651. dir);
  2652. /* restore sg */
  2653. sg->length += qc->pad_len;
  2654. if (pad_buf)
  2655. memcpy(qc->buf_virt + sg->length - qc->pad_len,
  2656. pad_buf, qc->pad_len);
  2657. }
  2658. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  2659. qc->__sg = NULL;
  2660. }
  2661. /**
  2662. * ata_fill_sg - Fill PCI IDE PRD table
  2663. * @qc: Metadata associated with taskfile to be transferred
  2664. *
  2665. * Fill PCI IDE PRD (scatter-gather) table with segments
  2666. * associated with the current disk command.
  2667. *
  2668. * LOCKING:
  2669. * spin_lock_irqsave(host_set lock)
  2670. *
  2671. */
  2672. static void ata_fill_sg(struct ata_queued_cmd *qc)
  2673. {
  2674. struct ata_port *ap = qc->ap;
  2675. struct scatterlist *sg;
  2676. unsigned int idx;
  2677. WARN_ON(qc->__sg == NULL);
  2678. WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
  2679. idx = 0;
  2680. ata_for_each_sg(sg, qc) {
  2681. u32 addr, offset;
  2682. u32 sg_len, len;
  2683. /* determine if physical DMA addr spans 64K boundary.
  2684. * Note h/w doesn't support 64-bit, so we unconditionally
  2685. * truncate dma_addr_t to u32.
  2686. */
  2687. addr = (u32) sg_dma_address(sg);
  2688. sg_len = sg_dma_len(sg);
  2689. while (sg_len) {
  2690. offset = addr & 0xffff;
  2691. len = sg_len;
  2692. if ((offset + sg_len) > 0x10000)
  2693. len = 0x10000 - offset;
  2694. ap->prd[idx].addr = cpu_to_le32(addr);
  2695. ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
  2696. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
  2697. idx++;
  2698. sg_len -= len;
  2699. addr += len;
  2700. }
  2701. }
  2702. if (idx)
  2703. ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  2704. }
  2705. /**
  2706. * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
  2707. * @qc: Metadata associated with taskfile to check
  2708. *
  2709. * Allow low-level driver to filter ATA PACKET commands, returning
  2710. * a status indicating whether or not it is OK to use DMA for the
  2711. * supplied PACKET command.
  2712. *
  2713. * LOCKING:
  2714. * spin_lock_irqsave(host_set lock)
  2715. *
  2716. * RETURNS: 0 when ATAPI DMA can be used
  2717. * nonzero otherwise
  2718. */
  2719. int ata_check_atapi_dma(struct ata_queued_cmd *qc)
  2720. {
  2721. struct ata_port *ap = qc->ap;
  2722. int rc = 0; /* Assume ATAPI DMA is OK by default */
  2723. if (ap->ops->check_atapi_dma)
  2724. rc = ap->ops->check_atapi_dma(qc);
  2725. return rc;
  2726. }
  2727. /**
  2728. * ata_qc_prep - Prepare taskfile for submission
  2729. * @qc: Metadata associated with taskfile to be prepared
  2730. *
  2731. * Prepare ATA taskfile for submission.
  2732. *
  2733. * LOCKING:
  2734. * spin_lock_irqsave(host_set lock)
  2735. */
  2736. void ata_qc_prep(struct ata_queued_cmd *qc)
  2737. {
  2738. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  2739. return;
  2740. ata_fill_sg(qc);
  2741. }
  2742. void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
  2743. /**
  2744. * ata_sg_init_one - Associate command with memory buffer
  2745. * @qc: Command to be associated
  2746. * @buf: Memory buffer
  2747. * @buflen: Length of memory buffer, in bytes.
  2748. *
  2749. * Initialize the data-related elements of queued_cmd @qc
  2750. * to point to a single memory buffer, @buf of byte length @buflen.
  2751. *
  2752. * LOCKING:
  2753. * spin_lock_irqsave(host_set lock)
  2754. */
  2755. void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
  2756. {
  2757. struct scatterlist *sg;
  2758. qc->flags |= ATA_QCFLAG_SINGLE;
  2759. memset(&qc->sgent, 0, sizeof(qc->sgent));
  2760. qc->__sg = &qc->sgent;
  2761. qc->n_elem = 1;
  2762. qc->orig_n_elem = 1;
  2763. qc->buf_virt = buf;
  2764. sg = qc->__sg;
  2765. sg_init_one(sg, buf, buflen);
  2766. }
  2767. /**
  2768. * ata_sg_init - Associate command with scatter-gather table.
  2769. * @qc: Command to be associated
  2770. * @sg: Scatter-gather table.
  2771. * @n_elem: Number of elements in s/g table.
  2772. *
  2773. * Initialize the data-related elements of queued_cmd @qc
  2774. * to point to a scatter-gather table @sg, containing @n_elem
  2775. * elements.
  2776. *
  2777. * LOCKING:
  2778. * spin_lock_irqsave(host_set lock)
  2779. */
  2780. void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
  2781. unsigned int n_elem)
  2782. {
  2783. qc->flags |= ATA_QCFLAG_SG;
  2784. qc->__sg = sg;
  2785. qc->n_elem = n_elem;
  2786. qc->orig_n_elem = n_elem;
  2787. }
  2788. /**
  2789. * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
  2790. * @qc: Command with memory buffer to be mapped.
  2791. *
  2792. * DMA-map the memory buffer associated with queued_cmd @qc.
  2793. *
  2794. * LOCKING:
  2795. * spin_lock_irqsave(host_set lock)
  2796. *
  2797. * RETURNS:
  2798. * Zero on success, negative on error.
  2799. */
  2800. static int ata_sg_setup_one(struct ata_queued_cmd *qc)
  2801. {
  2802. struct ata_port *ap = qc->ap;
  2803. int dir = qc->dma_dir;
  2804. struct scatterlist *sg = qc->__sg;
  2805. dma_addr_t dma_address;
  2806. int trim_sg = 0;
  2807. /* we must lengthen transfers to end on a 32-bit boundary */
  2808. qc->pad_len = sg->length & 3;
  2809. if (qc->pad_len) {
  2810. void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  2811. struct scatterlist *psg = &qc->pad_sgent;
  2812. WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
  2813. memset(pad_buf, 0, ATA_DMA_PAD_SZ);
  2814. if (qc->tf.flags & ATA_TFLAG_WRITE)
  2815. memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
  2816. qc->pad_len);
  2817. sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
  2818. sg_dma_len(psg) = ATA_DMA_PAD_SZ;
  2819. /* trim sg */
  2820. sg->length -= qc->pad_len;
  2821. if (sg->length == 0)
  2822. trim_sg = 1;
  2823. DPRINTK("padding done, sg->length=%u pad_len=%u\n",
  2824. sg->length, qc->pad_len);
  2825. }
  2826. if (trim_sg) {
  2827. qc->n_elem--;
  2828. goto skip_map;
  2829. }
  2830. dma_address = dma_map_single(ap->dev, qc->buf_virt,
  2831. sg->length, dir);
  2832. if (dma_mapping_error(dma_address)) {
  2833. /* restore sg */
  2834. sg->length += qc->pad_len;
  2835. return -1;
  2836. }
  2837. sg_dma_address(sg) = dma_address;
  2838. sg_dma_len(sg) = sg->length;
  2839. skip_map:
  2840. DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
  2841. qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  2842. return 0;
  2843. }
  2844. /**
  2845. * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
  2846. * @qc: Command with scatter-gather table to be mapped.
  2847. *
  2848. * DMA-map the scatter-gather table associated with queued_cmd @qc.
  2849. *
  2850. * LOCKING:
  2851. * spin_lock_irqsave(host_set lock)
  2852. *
  2853. * RETURNS:
  2854. * Zero on success, negative on error.
  2855. *
  2856. */
  2857. static int ata_sg_setup(struct ata_queued_cmd *qc)
  2858. {
  2859. struct ata_port *ap = qc->ap;
  2860. struct scatterlist *sg = qc->__sg;
  2861. struct scatterlist *lsg = &sg[qc->n_elem - 1];
  2862. int n_elem, pre_n_elem, dir, trim_sg = 0;
  2863. VPRINTK("ENTER, ata%u\n", ap->id);
  2864. WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
  2865. /* we must lengthen transfers to end on a 32-bit boundary */
  2866. qc->pad_len = lsg->length & 3;
  2867. if (qc->pad_len) {
  2868. void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  2869. struct scatterlist *psg = &qc->pad_sgent;
  2870. unsigned int offset;
  2871. WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
  2872. memset(pad_buf, 0, ATA_DMA_PAD_SZ);
  2873. /*
  2874. * psg->page/offset are used to copy to-be-written
  2875. * data in this function or read data in ata_sg_clean.
  2876. */
  2877. offset = lsg->offset + lsg->length - qc->pad_len;
  2878. psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
  2879. psg->offset = offset_in_page(offset);
  2880. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  2881. void *addr = kmap_atomic(psg->page, KM_IRQ0);
  2882. memcpy(pad_buf, addr + psg->offset, qc->pad_len);
  2883. kunmap_atomic(addr, KM_IRQ0);
  2884. }
  2885. sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
  2886. sg_dma_len(psg) = ATA_DMA_PAD_SZ;
  2887. /* trim last sg */
  2888. lsg->length -= qc->pad_len;
  2889. if (lsg->length == 0)
  2890. trim_sg = 1;
  2891. DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
  2892. qc->n_elem - 1, lsg->length, qc->pad_len);
  2893. }
  2894. pre_n_elem = qc->n_elem;
  2895. if (trim_sg && pre_n_elem)
  2896. pre_n_elem--;
  2897. if (!pre_n_elem) {
  2898. n_elem = 0;
  2899. goto skip_map;
  2900. }
  2901. dir = qc->dma_dir;
  2902. n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
  2903. if (n_elem < 1) {
  2904. /* restore last sg */
  2905. lsg->length += qc->pad_len;
  2906. return -1;
  2907. }
  2908. DPRINTK("%d sg elements mapped\n", n_elem);
  2909. skip_map:
  2910. qc->n_elem = n_elem;
  2911. return 0;
  2912. }
  2913. /**
  2914. * ata_poll_qc_complete - turn irq back on and finish qc
  2915. * @qc: Command to complete
  2916. * @err_mask: ATA status register content
  2917. *
  2918. * LOCKING:
  2919. * None. (grabs host lock)
  2920. */
  2921. void ata_poll_qc_complete(struct ata_queued_cmd *qc)
  2922. {
  2923. struct ata_port *ap = qc->ap;
  2924. unsigned long flags;
  2925. spin_lock_irqsave(&ap->host_set->lock, flags);
  2926. ap->flags &= ~ATA_FLAG_NOINTR;
  2927. ata_irq_on(ap);
  2928. ata_qc_complete(qc);
  2929. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  2930. }
  2931. /**
  2932. * ata_pio_poll - poll using PIO, depending on current state
  2933. * @qc: qc in progress
  2934. *
  2935. * LOCKING:
  2936. * None. (executing in kernel thread context)
  2937. *
  2938. * RETURNS:
  2939. * timeout value to use
  2940. */
  2941. static unsigned long ata_pio_poll(struct ata_queued_cmd *qc)
  2942. {
  2943. struct ata_port *ap = qc->ap;
  2944. u8 status;
  2945. unsigned int poll_state = HSM_ST_UNKNOWN;
  2946. unsigned int reg_state = HSM_ST_UNKNOWN;
  2947. switch (ap->hsm_task_state) {
  2948. case HSM_ST:
  2949. case HSM_ST_POLL:
  2950. poll_state = HSM_ST_POLL;
  2951. reg_state = HSM_ST;
  2952. break;
  2953. case HSM_ST_LAST:
  2954. case HSM_ST_LAST_POLL:
  2955. poll_state = HSM_ST_LAST_POLL;
  2956. reg_state = HSM_ST_LAST;
  2957. break;
  2958. default:
  2959. BUG();
  2960. break;
  2961. }
  2962. status = ata_chk_status(ap);
  2963. if (status & ATA_BUSY) {
  2964. if (time_after(jiffies, ap->pio_task_timeout)) {
  2965. qc->err_mask |= AC_ERR_TIMEOUT;
  2966. ap->hsm_task_state = HSM_ST_TMOUT;
  2967. return 0;
  2968. }
  2969. ap->hsm_task_state = poll_state;
  2970. return ATA_SHORT_PAUSE;
  2971. }
  2972. ap->hsm_task_state = reg_state;
  2973. return 0;
  2974. }
  2975. /**
  2976. * ata_pio_complete - check if drive is busy or idle
  2977. * @qc: qc to complete
  2978. *
  2979. * LOCKING:
  2980. * None. (executing in kernel thread context)
  2981. *
  2982. * RETURNS:
  2983. * Non-zero if qc completed, zero otherwise.
  2984. */
  2985. static int ata_pio_complete(struct ata_queued_cmd *qc)
  2986. {
  2987. struct ata_port *ap = qc->ap;
  2988. u8 drv_stat;
  2989. /*
  2990. * This is purely heuristic. This is a fast path. Sometimes when
  2991. * we enter, BSY will be cleared in a chk-status or two. If not,
  2992. * the drive is probably seeking or something. Snooze for a couple
  2993. * msecs, then chk-status again. If still busy, fall back to
  2994. * HSM_ST_POLL state.
  2995. */
  2996. drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
  2997. if (drv_stat & ATA_BUSY) {
  2998. msleep(2);
  2999. drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
  3000. if (drv_stat & ATA_BUSY) {
  3001. ap->hsm_task_state = HSM_ST_LAST_POLL;
  3002. ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
  3003. return 0;
  3004. }
  3005. }
  3006. drv_stat = ata_wait_idle(ap);
  3007. if (!ata_ok(drv_stat)) {
  3008. qc->err_mask |= __ac_err_mask(drv_stat);
  3009. ap->hsm_task_state = HSM_ST_ERR;
  3010. return 0;
  3011. }
  3012. ap->hsm_task_state = HSM_ST_IDLE;
  3013. WARN_ON(qc->err_mask);
  3014. ata_poll_qc_complete(qc);
  3015. /* another command may start at this point */
  3016. return 1;
  3017. }
  3018. /**
  3019. * swap_buf_le16 - swap halves of 16-bit words in place
  3020. * @buf: Buffer to swap
  3021. * @buf_words: Number of 16-bit words in buffer.
  3022. *
  3023. * Swap halves of 16-bit words if needed to convert from
  3024. * little-endian byte order to native cpu byte order, or
  3025. * vice-versa.
  3026. *
  3027. * LOCKING:
  3028. * Inherited from caller.
  3029. */
  3030. void swap_buf_le16(u16 *buf, unsigned int buf_words)
  3031. {
  3032. #ifdef __BIG_ENDIAN
  3033. unsigned int i;
  3034. for (i = 0; i < buf_words; i++)
  3035. buf[i] = le16_to_cpu(buf[i]);
  3036. #endif /* __BIG_ENDIAN */
  3037. }
  3038. /**
  3039. * ata_mmio_data_xfer - Transfer data by MMIO
  3040. * @ap: port to read/write
  3041. * @buf: data buffer
  3042. * @buflen: buffer length
  3043. * @write_data: read/write
  3044. *
  3045. * Transfer data from/to the device data register by MMIO.
  3046. *
  3047. * LOCKING:
  3048. * Inherited from caller.
  3049. */
  3050. static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
  3051. unsigned int buflen, int write_data)
  3052. {
  3053. unsigned int i;
  3054. unsigned int words = buflen >> 1;
  3055. u16 *buf16 = (u16 *) buf;
  3056. void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
  3057. /* Transfer multiple of 2 bytes */
  3058. if (write_data) {
  3059. for (i = 0; i < words; i++)
  3060. writew(le16_to_cpu(buf16[i]), mmio);
  3061. } else {
  3062. for (i = 0; i < words; i++)
  3063. buf16[i] = cpu_to_le16(readw(mmio));
  3064. }
  3065. /* Transfer trailing 1 byte, if any. */
  3066. if (unlikely(buflen & 0x01)) {
  3067. u16 align_buf[1] = { 0 };
  3068. unsigned char *trailing_buf = buf + buflen - 1;
  3069. if (write_data) {
  3070. memcpy(align_buf, trailing_buf, 1);
  3071. writew(le16_to_cpu(align_buf[0]), mmio);
  3072. } else {
  3073. align_buf[0] = cpu_to_le16(readw(mmio));
  3074. memcpy(trailing_buf, align_buf, 1);
  3075. }
  3076. }
  3077. }
  3078. /**
  3079. * ata_pio_data_xfer - Transfer data by PIO
  3080. * @ap: port to read/write
  3081. * @buf: data buffer
  3082. * @buflen: buffer length
  3083. * @write_data: read/write
  3084. *
  3085. * Transfer data from/to the device data register by PIO.
  3086. *
  3087. * LOCKING:
  3088. * Inherited from caller.
  3089. */
  3090. static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
  3091. unsigned int buflen, int write_data)
  3092. {
  3093. unsigned int words = buflen >> 1;
  3094. /* Transfer multiple of 2 bytes */
  3095. if (write_data)
  3096. outsw(ap->ioaddr.data_addr, buf, words);
  3097. else
  3098. insw(ap->ioaddr.data_addr, buf, words);
  3099. /* Transfer trailing 1 byte, if any. */
  3100. if (unlikely(buflen & 0x01)) {
  3101. u16 align_buf[1] = { 0 };
  3102. unsigned char *trailing_buf = buf + buflen - 1;
  3103. if (write_data) {
  3104. memcpy(align_buf, trailing_buf, 1);
  3105. outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
  3106. } else {
  3107. align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
  3108. memcpy(trailing_buf, align_buf, 1);
  3109. }
  3110. }
  3111. }
  3112. /**
  3113. * ata_data_xfer - Transfer data from/to the data register.
  3114. * @ap: port to read/write
  3115. * @buf: data buffer
  3116. * @buflen: buffer length
  3117. * @do_write: read/write
  3118. *
  3119. * Transfer data from/to the device data register.
  3120. *
  3121. * LOCKING:
  3122. * Inherited from caller.
  3123. */
  3124. static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
  3125. unsigned int buflen, int do_write)
  3126. {
  3127. /* Make the crap hardware pay the costs not the good stuff */
  3128. if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
  3129. unsigned long flags;
  3130. local_irq_save(flags);
  3131. if (ap->flags & ATA_FLAG_MMIO)
  3132. ata_mmio_data_xfer(ap, buf, buflen, do_write);
  3133. else
  3134. ata_pio_data_xfer(ap, buf, buflen, do_write);
  3135. local_irq_restore(flags);
  3136. } else {
  3137. if (ap->flags & ATA_FLAG_MMIO)
  3138. ata_mmio_data_xfer(ap, buf, buflen, do_write);
  3139. else
  3140. ata_pio_data_xfer(ap, buf, buflen, do_write);
  3141. }
  3142. }
  3143. /**
  3144. * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
  3145. * @qc: Command on going
  3146. *
  3147. * Transfer ATA_SECT_SIZE of data from/to the ATA device.
  3148. *
  3149. * LOCKING:
  3150. * Inherited from caller.
  3151. */
  3152. static void ata_pio_sector(struct ata_queued_cmd *qc)
  3153. {
  3154. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  3155. struct scatterlist *sg = qc->__sg;
  3156. struct ata_port *ap = qc->ap;
  3157. struct page *page;
  3158. unsigned int offset;
  3159. unsigned char *buf;
  3160. if (qc->cursect == (qc->nsect - 1))
  3161. ap->hsm_task_state = HSM_ST_LAST;
  3162. page = sg[qc->cursg].page;
  3163. offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
  3164. /* get the current page and offset */
  3165. page = nth_page(page, (offset >> PAGE_SHIFT));
  3166. offset %= PAGE_SIZE;
  3167. buf = kmap(page) + offset;
  3168. qc->cursect++;
  3169. qc->cursg_ofs++;
  3170. if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
  3171. qc->cursg++;
  3172. qc->cursg_ofs = 0;
  3173. }
  3174. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  3175. /* do the actual data transfer */
  3176. do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  3177. ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
  3178. kunmap(page);
  3179. }
  3180. /**
  3181. * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
  3182. * @qc: Command on going
  3183. * @bytes: number of bytes
  3184. *
  3185. * Transfer Transfer data from/to the ATAPI device.
  3186. *
  3187. * LOCKING:
  3188. * Inherited from caller.
  3189. *
  3190. */
  3191. static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
  3192. {
  3193. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  3194. struct scatterlist *sg = qc->__sg;
  3195. struct ata_port *ap = qc->ap;
  3196. struct page *page;
  3197. unsigned char *buf;
  3198. unsigned int offset, count;
  3199. if (qc->curbytes + bytes >= qc->nbytes)
  3200. ap->hsm_task_state = HSM_ST_LAST;
  3201. next_sg:
  3202. if (unlikely(qc->cursg >= qc->n_elem)) {
  3203. /*
  3204. * The end of qc->sg is reached and the device expects
  3205. * more data to transfer. In order not to overrun qc->sg
  3206. * and fulfill length specified in the byte count register,
  3207. * - for read case, discard trailing data from the device
  3208. * - for write case, padding zero data to the device
  3209. */
  3210. u16 pad_buf[1] = { 0 };
  3211. unsigned int words = bytes >> 1;
  3212. unsigned int i;
  3213. if (words) /* warning if bytes > 1 */
  3214. printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
  3215. ap->id, bytes);
  3216. for (i = 0; i < words; i++)
  3217. ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
  3218. ap->hsm_task_state = HSM_ST_LAST;
  3219. return;
  3220. }
  3221. sg = &qc->__sg[qc->cursg];
  3222. page = sg->page;
  3223. offset = sg->offset + qc->cursg_ofs;
  3224. /* get the current page and offset */
  3225. page = nth_page(page, (offset >> PAGE_SHIFT));
  3226. offset %= PAGE_SIZE;
  3227. /* don't overrun current sg */
  3228. count = min(sg->length - qc->cursg_ofs, bytes);
  3229. /* don't cross page boundaries */
  3230. count = min(count, (unsigned int)PAGE_SIZE - offset);
  3231. buf = kmap(page) + offset;
  3232. bytes -= count;
  3233. qc->curbytes += count;
  3234. qc->cursg_ofs += count;
  3235. if (qc->cursg_ofs == sg->length) {
  3236. qc->cursg++;
  3237. qc->cursg_ofs = 0;
  3238. }
  3239. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  3240. /* do the actual data transfer */
  3241. ata_data_xfer(ap, buf, count, do_write);
  3242. kunmap(page);
  3243. if (bytes)
  3244. goto next_sg;
  3245. }
  3246. /**
  3247. * atapi_pio_bytes - Transfer data from/to the ATAPI device.
  3248. * @qc: Command on going
  3249. *
  3250. * Transfer Transfer data from/to the ATAPI device.
  3251. *
  3252. * LOCKING:
  3253. * Inherited from caller.
  3254. */
  3255. static void atapi_pio_bytes(struct ata_queued_cmd *qc)
  3256. {
  3257. struct ata_port *ap = qc->ap;
  3258. struct ata_device *dev = qc->dev;
  3259. unsigned int ireason, bc_lo, bc_hi, bytes;
  3260. int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
  3261. ap->ops->tf_read(ap, &qc->tf);
  3262. ireason = qc->tf.nsect;
  3263. bc_lo = qc->tf.lbam;
  3264. bc_hi = qc->tf.lbah;
  3265. bytes = (bc_hi << 8) | bc_lo;
  3266. /* shall be cleared to zero, indicating xfer of data */
  3267. if (ireason & (1 << 0))
  3268. goto err_out;
  3269. /* make sure transfer direction matches expected */
  3270. i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
  3271. if (do_write != i_write)
  3272. goto err_out;
  3273. __atapi_pio_bytes(qc, bytes);
  3274. return;
  3275. err_out:
  3276. printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
  3277. ap->id, dev->devno);
  3278. qc->err_mask |= AC_ERR_HSM;
  3279. ap->hsm_task_state = HSM_ST_ERR;
  3280. }
  3281. /**
  3282. * ata_pio_block - start PIO on a block
  3283. * @qc: qc to transfer block for
  3284. *
  3285. * LOCKING:
  3286. * None. (executing in kernel thread context)
  3287. */
  3288. static void ata_pio_block(struct ata_queued_cmd *qc)
  3289. {
  3290. struct ata_port *ap = qc->ap;
  3291. u8 status;
  3292. /*
  3293. * This is purely heuristic. This is a fast path.
  3294. * Sometimes when we enter, BSY will be cleared in
  3295. * a chk-status or two. If not, the drive is probably seeking
  3296. * or something. Snooze for a couple msecs, then
  3297. * chk-status again. If still busy, fall back to
  3298. * HSM_ST_POLL state.
  3299. */
  3300. status = ata_busy_wait(ap, ATA_BUSY, 5);
  3301. if (status & ATA_BUSY) {
  3302. msleep(2);
  3303. status = ata_busy_wait(ap, ATA_BUSY, 10);
  3304. if (status & ATA_BUSY) {
  3305. ap->hsm_task_state = HSM_ST_POLL;
  3306. ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
  3307. return;
  3308. }
  3309. }
  3310. /* check error */
  3311. if (status & (ATA_ERR | ATA_DF)) {
  3312. qc->err_mask |= AC_ERR_DEV;
  3313. ap->hsm_task_state = HSM_ST_ERR;
  3314. return;
  3315. }
  3316. /* transfer data if any */
  3317. if (is_atapi_taskfile(&qc->tf)) {
  3318. /* DRQ=0 means no more data to transfer */
  3319. if ((status & ATA_DRQ) == 0) {
  3320. ap->hsm_task_state = HSM_ST_LAST;
  3321. return;
  3322. }
  3323. atapi_pio_bytes(qc);
  3324. } else {
  3325. /* handle BSY=0, DRQ=0 as error */
  3326. if ((status & ATA_DRQ) == 0) {
  3327. qc->err_mask |= AC_ERR_HSM;
  3328. ap->hsm_task_state = HSM_ST_ERR;
  3329. return;
  3330. }
  3331. ata_pio_sector(qc);
  3332. }
  3333. }
  3334. static void ata_pio_error(struct ata_queued_cmd *qc)
  3335. {
  3336. struct ata_port *ap = qc->ap;
  3337. if (qc->tf.command != ATA_CMD_PACKET)
  3338. printk(KERN_WARNING "ata%u: dev %u PIO error\n",
  3339. ap->id, qc->dev->devno);
  3340. /* make sure qc->err_mask is available to
  3341. * know what's wrong and recover
  3342. */
  3343. WARN_ON(qc->err_mask == 0);
  3344. ap->hsm_task_state = HSM_ST_IDLE;
  3345. ata_poll_qc_complete(qc);
  3346. }
  3347. static void ata_pio_task(void *_data)
  3348. {
  3349. struct ata_queued_cmd *qc = _data;
  3350. struct ata_port *ap = qc->ap;
  3351. unsigned long timeout;
  3352. int qc_completed;
  3353. fsm_start:
  3354. timeout = 0;
  3355. qc_completed = 0;
  3356. switch (ap->hsm_task_state) {
  3357. case HSM_ST_IDLE:
  3358. return;
  3359. case HSM_ST:
  3360. ata_pio_block(qc);
  3361. break;
  3362. case HSM_ST_LAST:
  3363. qc_completed = ata_pio_complete(qc);
  3364. break;
  3365. case HSM_ST_POLL:
  3366. case HSM_ST_LAST_POLL:
  3367. timeout = ata_pio_poll(qc);
  3368. break;
  3369. case HSM_ST_TMOUT:
  3370. case HSM_ST_ERR:
  3371. ata_pio_error(qc);
  3372. return;
  3373. }
  3374. if (timeout)
  3375. ata_port_queue_task(ap, ata_pio_task, qc, timeout);
  3376. else if (!qc_completed)
  3377. goto fsm_start;
  3378. }
  3379. /**
  3380. * atapi_packet_task - Write CDB bytes to hardware
  3381. * @_data: qc in progress
  3382. *
  3383. * When device has indicated its readiness to accept
  3384. * a CDB, this function is called. Send the CDB.
  3385. * If DMA is to be performed, exit immediately.
  3386. * Otherwise, we are in polling mode, so poll
  3387. * status under operation succeeds or fails.
  3388. *
  3389. * LOCKING:
  3390. * Kernel thread context (may sleep)
  3391. */
  3392. static void atapi_packet_task(void *_data)
  3393. {
  3394. struct ata_queued_cmd *qc = _data;
  3395. struct ata_port *ap = qc->ap;
  3396. u8 status;
  3397. /* sleep-wait for BSY to clear */
  3398. DPRINTK("busy wait\n");
  3399. if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
  3400. qc->err_mask |= AC_ERR_TIMEOUT;
  3401. goto err_out;
  3402. }
  3403. /* make sure DRQ is set */
  3404. status = ata_chk_status(ap);
  3405. if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
  3406. qc->err_mask |= AC_ERR_HSM;
  3407. goto err_out;
  3408. }
  3409. /* send SCSI cdb */
  3410. DPRINTK("send cdb\n");
  3411. WARN_ON(qc->dev->cdb_len < 12);
  3412. if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
  3413. qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
  3414. unsigned long flags;
  3415. /* Once we're done issuing command and kicking bmdma,
  3416. * irq handler takes over. To not lose irq, we need
  3417. * to clear NOINTR flag before sending cdb, but
  3418. * interrupt handler shouldn't be invoked before we're
  3419. * finished. Hence, the following locking.
  3420. */
  3421. spin_lock_irqsave(&ap->host_set->lock, flags);
  3422. ap->flags &= ~ATA_FLAG_NOINTR;
  3423. ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
  3424. if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
  3425. ap->ops->bmdma_start(qc); /* initiate bmdma */
  3426. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  3427. } else {
  3428. ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
  3429. /* PIO commands are handled by polling */
  3430. ap->hsm_task_state = HSM_ST;
  3431. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  3432. }
  3433. return;
  3434. err_out:
  3435. ata_poll_qc_complete(qc);
  3436. }
  3437. /**
  3438. * ata_qc_new - Request an available ATA command, for queueing
  3439. * @ap: Port associated with device @dev
  3440. * @dev: Device from whom we request an available command structure
  3441. *
  3442. * LOCKING:
  3443. * None.
  3444. */
  3445. static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
  3446. {
  3447. struct ata_queued_cmd *qc = NULL;
  3448. unsigned int i;
  3449. for (i = 0; i < ATA_MAX_QUEUE; i++)
  3450. if (!test_and_set_bit(i, &ap->qactive)) {
  3451. qc = ata_qc_from_tag(ap, i);
  3452. break;
  3453. }
  3454. if (qc)
  3455. qc->tag = i;
  3456. return qc;
  3457. }
  3458. /**
  3459. * ata_qc_new_init - Request an available ATA command, and initialize it
  3460. * @ap: Port associated with device @dev
  3461. * @dev: Device from whom we request an available command structure
  3462. *
  3463. * LOCKING:
  3464. * None.
  3465. */
  3466. struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
  3467. struct ata_device *dev)
  3468. {
  3469. struct ata_queued_cmd *qc;
  3470. qc = ata_qc_new(ap);
  3471. if (qc) {
  3472. qc->scsicmd = NULL;
  3473. qc->ap = ap;
  3474. qc->dev = dev;
  3475. ata_qc_reinit(qc);
  3476. }
  3477. return qc;
  3478. }
  3479. /**
  3480. * ata_qc_free - free unused ata_queued_cmd
  3481. * @qc: Command to complete
  3482. *
  3483. * Designed to free unused ata_queued_cmd object
  3484. * in case something prevents using it.
  3485. *
  3486. * LOCKING:
  3487. * spin_lock_irqsave(host_set lock)
  3488. */
  3489. void ata_qc_free(struct ata_queued_cmd *qc)
  3490. {
  3491. struct ata_port *ap = qc->ap;
  3492. unsigned int tag;
  3493. WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  3494. qc->flags = 0;
  3495. tag = qc->tag;
  3496. if (likely(ata_tag_valid(tag))) {
  3497. if (tag == ap->active_tag)
  3498. ap->active_tag = ATA_TAG_POISON;
  3499. qc->tag = ATA_TAG_POISON;
  3500. clear_bit(tag, &ap->qactive);
  3501. }
  3502. }
  3503. void __ata_qc_complete(struct ata_queued_cmd *qc)
  3504. {
  3505. WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  3506. WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
  3507. if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
  3508. ata_sg_clean(qc);
  3509. /* atapi: mark qc as inactive to prevent the interrupt handler
  3510. * from completing the command twice later, before the error handler
  3511. * is called. (when rc != 0 and atapi request sense is needed)
  3512. */
  3513. qc->flags &= ~ATA_QCFLAG_ACTIVE;
  3514. /* call completion callback */
  3515. qc->complete_fn(qc);
  3516. }
  3517. static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
  3518. {
  3519. struct ata_port *ap = qc->ap;
  3520. switch (qc->tf.protocol) {
  3521. case ATA_PROT_DMA:
  3522. case ATA_PROT_ATAPI_DMA:
  3523. return 1;
  3524. case ATA_PROT_ATAPI:
  3525. case ATA_PROT_PIO:
  3526. if (ap->flags & ATA_FLAG_PIO_DMA)
  3527. return 1;
  3528. /* fall through */
  3529. default:
  3530. return 0;
  3531. }
  3532. /* never reached */
  3533. }
  3534. /**
  3535. * ata_qc_issue - issue taskfile to device
  3536. * @qc: command to issue to device
  3537. *
  3538. * Prepare an ATA command to submission to device.
  3539. * This includes mapping the data into a DMA-able
  3540. * area, filling in the S/G table, and finally
  3541. * writing the taskfile to hardware, starting the command.
  3542. *
  3543. * LOCKING:
  3544. * spin_lock_irqsave(host_set lock)
  3545. */
  3546. void ata_qc_issue(struct ata_queued_cmd *qc)
  3547. {
  3548. struct ata_port *ap = qc->ap;
  3549. qc->ap->active_tag = qc->tag;
  3550. qc->flags |= ATA_QCFLAG_ACTIVE;
  3551. if (ata_should_dma_map(qc)) {
  3552. if (qc->flags & ATA_QCFLAG_SG) {
  3553. if (ata_sg_setup(qc))
  3554. goto sg_err;
  3555. } else if (qc->flags & ATA_QCFLAG_SINGLE) {
  3556. if (ata_sg_setup_one(qc))
  3557. goto sg_err;
  3558. }
  3559. } else {
  3560. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  3561. }
  3562. ap->ops->qc_prep(qc);
  3563. qc->err_mask |= ap->ops->qc_issue(qc);
  3564. if (unlikely(qc->err_mask))
  3565. goto err;
  3566. return;
  3567. sg_err:
  3568. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  3569. qc->err_mask |= AC_ERR_SYSTEM;
  3570. err:
  3571. ata_qc_complete(qc);
  3572. }
  3573. /**
  3574. * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
  3575. * @qc: command to issue to device
  3576. *
  3577. * Using various libata functions and hooks, this function
  3578. * starts an ATA command. ATA commands are grouped into
  3579. * classes called "protocols", and issuing each type of protocol
  3580. * is slightly different.
  3581. *
  3582. * May be used as the qc_issue() entry in ata_port_operations.
  3583. *
  3584. * LOCKING:
  3585. * spin_lock_irqsave(host_set lock)
  3586. *
  3587. * RETURNS:
  3588. * Zero on success, AC_ERR_* mask on failure
  3589. */
  3590. unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
  3591. {
  3592. struct ata_port *ap = qc->ap;
  3593. ata_dev_select(ap, qc->dev->devno, 1, 0);
  3594. switch (qc->tf.protocol) {
  3595. case ATA_PROT_NODATA:
  3596. ata_tf_to_host(ap, &qc->tf);
  3597. break;
  3598. case ATA_PROT_DMA:
  3599. ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
  3600. ap->ops->bmdma_setup(qc); /* set up bmdma */
  3601. ap->ops->bmdma_start(qc); /* initiate bmdma */
  3602. break;
  3603. case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
  3604. ata_qc_set_polling(qc);
  3605. ata_tf_to_host(ap, &qc->tf);
  3606. ap->hsm_task_state = HSM_ST;
  3607. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  3608. break;
  3609. case ATA_PROT_ATAPI:
  3610. ata_qc_set_polling(qc);
  3611. ata_tf_to_host(ap, &qc->tf);
  3612. ata_port_queue_task(ap, atapi_packet_task, qc, 0);
  3613. break;
  3614. case ATA_PROT_ATAPI_NODATA:
  3615. ap->flags |= ATA_FLAG_NOINTR;
  3616. ata_tf_to_host(ap, &qc->tf);
  3617. ata_port_queue_task(ap, atapi_packet_task, qc, 0);
  3618. break;
  3619. case ATA_PROT_ATAPI_DMA:
  3620. ap->flags |= ATA_FLAG_NOINTR;
  3621. ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
  3622. ap->ops->bmdma_setup(qc); /* set up bmdma */
  3623. ata_port_queue_task(ap, atapi_packet_task, qc, 0);
  3624. break;
  3625. default:
  3626. WARN_ON(1);
  3627. return AC_ERR_SYSTEM;
  3628. }
  3629. return 0;
  3630. }
  3631. /**
  3632. * ata_host_intr - Handle host interrupt for given (port, task)
  3633. * @ap: Port on which interrupt arrived (possibly...)
  3634. * @qc: Taskfile currently active in engine
  3635. *
  3636. * Handle host interrupt for given queued command. Currently,
  3637. * only DMA interrupts are handled. All other commands are
  3638. * handled via polling with interrupts disabled (nIEN bit).
  3639. *
  3640. * LOCKING:
  3641. * spin_lock_irqsave(host_set lock)
  3642. *
  3643. * RETURNS:
  3644. * One if interrupt was handled, zero if not (shared irq).
  3645. */
  3646. inline unsigned int ata_host_intr (struct ata_port *ap,
  3647. struct ata_queued_cmd *qc)
  3648. {
  3649. u8 status, host_stat;
  3650. switch (qc->tf.protocol) {
  3651. case ATA_PROT_DMA:
  3652. case ATA_PROT_ATAPI_DMA:
  3653. case ATA_PROT_ATAPI:
  3654. /* check status of DMA engine */
  3655. host_stat = ap->ops->bmdma_status(ap);
  3656. VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
  3657. /* if it's not our irq... */
  3658. if (!(host_stat & ATA_DMA_INTR))
  3659. goto idle_irq;
  3660. /* before we do anything else, clear DMA-Start bit */
  3661. ap->ops->bmdma_stop(qc);
  3662. /* fall through */
  3663. case ATA_PROT_ATAPI_NODATA:
  3664. case ATA_PROT_NODATA:
  3665. /* check altstatus */
  3666. status = ata_altstatus(ap);
  3667. if (status & ATA_BUSY)
  3668. goto idle_irq;
  3669. /* check main status, clearing INTRQ */
  3670. status = ata_chk_status(ap);
  3671. if (unlikely(status & ATA_BUSY))
  3672. goto idle_irq;
  3673. DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
  3674. ap->id, qc->tf.protocol, status);
  3675. /* ack bmdma irq events */
  3676. ap->ops->irq_clear(ap);
  3677. /* complete taskfile transaction */
  3678. qc->err_mask |= ac_err_mask(status);
  3679. ata_qc_complete(qc);
  3680. break;
  3681. default:
  3682. goto idle_irq;
  3683. }
  3684. return 1; /* irq handled */
  3685. idle_irq:
  3686. ap->stats.idle_irq++;
  3687. #ifdef ATA_IRQ_TRAP
  3688. if ((ap->stats.idle_irq % 1000) == 0) {
  3689. ata_irq_ack(ap, 0); /* debug trap */
  3690. printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
  3691. return 1;
  3692. }
  3693. #endif
  3694. return 0; /* irq not handled */
  3695. }
  3696. /**
  3697. * ata_interrupt - Default ATA host interrupt handler
  3698. * @irq: irq line (unused)
  3699. * @dev_instance: pointer to our ata_host_set information structure
  3700. * @regs: unused
  3701. *
  3702. * Default interrupt handler for PCI IDE devices. Calls
  3703. * ata_host_intr() for each port that is not disabled.
  3704. *
  3705. * LOCKING:
  3706. * Obtains host_set lock during operation.
  3707. *
  3708. * RETURNS:
  3709. * IRQ_NONE or IRQ_HANDLED.
  3710. */
  3711. irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
  3712. {
  3713. struct ata_host_set *host_set = dev_instance;
  3714. unsigned int i;
  3715. unsigned int handled = 0;
  3716. unsigned long flags;
  3717. /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
  3718. spin_lock_irqsave(&host_set->lock, flags);
  3719. for (i = 0; i < host_set->n_ports; i++) {
  3720. struct ata_port *ap;
  3721. ap = host_set->ports[i];
  3722. if (ap &&
  3723. !(ap->flags & (ATA_FLAG_DISABLED | ATA_FLAG_NOINTR))) {
  3724. struct ata_queued_cmd *qc;
  3725. qc = ata_qc_from_tag(ap, ap->active_tag);
  3726. if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
  3727. (qc->flags & ATA_QCFLAG_ACTIVE))
  3728. handled |= ata_host_intr(ap, qc);
  3729. }
  3730. }
  3731. spin_unlock_irqrestore(&host_set->lock, flags);
  3732. return IRQ_RETVAL(handled);
  3733. }
  3734. /*
  3735. * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
  3736. * without filling any other registers
  3737. */
  3738. static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
  3739. u8 cmd)
  3740. {
  3741. struct ata_taskfile tf;
  3742. int err;
  3743. ata_tf_init(ap, &tf, dev->devno);
  3744. tf.command = cmd;
  3745. tf.flags |= ATA_TFLAG_DEVICE;
  3746. tf.protocol = ATA_PROT_NODATA;
  3747. err = ata_exec_internal(ap, dev, &tf, NULL, DMA_NONE, NULL, 0);
  3748. if (err)
  3749. printk(KERN_ERR "%s: ata command failed: %d\n",
  3750. __FUNCTION__, err);
  3751. return err;
  3752. }
  3753. static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
  3754. {
  3755. u8 cmd;
  3756. if (!ata_try_flush_cache(dev))
  3757. return 0;
  3758. if (ata_id_has_flush_ext(dev->id))
  3759. cmd = ATA_CMD_FLUSH_EXT;
  3760. else
  3761. cmd = ATA_CMD_FLUSH;
  3762. return ata_do_simple_cmd(ap, dev, cmd);
  3763. }
  3764. static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
  3765. {
  3766. return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
  3767. }
  3768. static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
  3769. {
  3770. return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
  3771. }
  3772. /**
  3773. * ata_device_resume - wakeup a previously suspended devices
  3774. * @ap: port the device is connected to
  3775. * @dev: the device to resume
  3776. *
  3777. * Kick the drive back into action, by sending it an idle immediate
  3778. * command and making sure its transfer mode matches between drive
  3779. * and host.
  3780. *
  3781. */
  3782. int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
  3783. {
  3784. if (ap->flags & ATA_FLAG_SUSPENDED) {
  3785. struct ata_device *failed_dev;
  3786. ap->flags &= ~ATA_FLAG_SUSPENDED;
  3787. while (ata_set_mode(ap, &failed_dev))
  3788. ata_dev_disable(ap, failed_dev);
  3789. }
  3790. if (!ata_dev_enabled(dev))
  3791. return 0;
  3792. if (dev->class == ATA_DEV_ATA)
  3793. ata_start_drive(ap, dev);
  3794. return 0;
  3795. }
  3796. /**
  3797. * ata_device_suspend - prepare a device for suspend
  3798. * @ap: port the device is connected to
  3799. * @dev: the device to suspend
  3800. *
  3801. * Flush the cache on the drive, if appropriate, then issue a
  3802. * standbynow command.
  3803. */
  3804. int ata_device_suspend(struct ata_port *ap, struct ata_device *dev, pm_message_t state)
  3805. {
  3806. if (!ata_dev_enabled(dev))
  3807. return 0;
  3808. if (dev->class == ATA_DEV_ATA)
  3809. ata_flush_cache(ap, dev);
  3810. if (state.event != PM_EVENT_FREEZE)
  3811. ata_standby_drive(ap, dev);
  3812. ap->flags |= ATA_FLAG_SUSPENDED;
  3813. return 0;
  3814. }
  3815. /**
  3816. * ata_port_start - Set port up for dma.
  3817. * @ap: Port to initialize
  3818. *
  3819. * Called just after data structures for each port are
  3820. * initialized. Allocates space for PRD table.
  3821. *
  3822. * May be used as the port_start() entry in ata_port_operations.
  3823. *
  3824. * LOCKING:
  3825. * Inherited from caller.
  3826. */
  3827. int ata_port_start (struct ata_port *ap)
  3828. {
  3829. struct device *dev = ap->dev;
  3830. int rc;
  3831. ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
  3832. if (!ap->prd)
  3833. return -ENOMEM;
  3834. rc = ata_pad_alloc(ap, dev);
  3835. if (rc) {
  3836. dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
  3837. return rc;
  3838. }
  3839. DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
  3840. return 0;
  3841. }
  3842. /**
  3843. * ata_port_stop - Undo ata_port_start()
  3844. * @ap: Port to shut down
  3845. *
  3846. * Frees the PRD table.
  3847. *
  3848. * May be used as the port_stop() entry in ata_port_operations.
  3849. *
  3850. * LOCKING:
  3851. * Inherited from caller.
  3852. */
  3853. void ata_port_stop (struct ata_port *ap)
  3854. {
  3855. struct device *dev = ap->dev;
  3856. dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
  3857. ata_pad_free(ap, dev);
  3858. }
  3859. void ata_host_stop (struct ata_host_set *host_set)
  3860. {
  3861. if (host_set->mmio_base)
  3862. iounmap(host_set->mmio_base);
  3863. }
  3864. /**
  3865. * ata_host_remove - Unregister SCSI host structure with upper layers
  3866. * @ap: Port to unregister
  3867. * @do_unregister: 1 if we fully unregister, 0 to just stop the port
  3868. *
  3869. * LOCKING:
  3870. * Inherited from caller.
  3871. */
  3872. static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
  3873. {
  3874. struct Scsi_Host *sh = ap->host;
  3875. DPRINTK("ENTER\n");
  3876. if (do_unregister)
  3877. scsi_remove_host(sh);
  3878. ap->ops->port_stop(ap);
  3879. }
  3880. /**
  3881. * ata_host_init - Initialize an ata_port structure
  3882. * @ap: Structure to initialize
  3883. * @host: associated SCSI mid-layer structure
  3884. * @host_set: Collection of hosts to which @ap belongs
  3885. * @ent: Probe information provided by low-level driver
  3886. * @port_no: Port number associated with this ata_port
  3887. *
  3888. * Initialize a new ata_port structure, and its associated
  3889. * scsi_host.
  3890. *
  3891. * LOCKING:
  3892. * Inherited from caller.
  3893. */
  3894. static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
  3895. struct ata_host_set *host_set,
  3896. const struct ata_probe_ent *ent, unsigned int port_no)
  3897. {
  3898. unsigned int i;
  3899. host->max_id = 16;
  3900. host->max_lun = 1;
  3901. host->max_channel = 1;
  3902. host->unique_id = ata_unique_id++;
  3903. host->max_cmd_len = 12;
  3904. ap->flags = ATA_FLAG_DISABLED;
  3905. ap->id = host->unique_id;
  3906. ap->host = host;
  3907. ap->ctl = ATA_DEVCTL_OBS;
  3908. ap->host_set = host_set;
  3909. ap->dev = ent->dev;
  3910. ap->port_no = port_no;
  3911. ap->hard_port_no =
  3912. ent->legacy_mode ? ent->hard_port_no : port_no;
  3913. ap->pio_mask = ent->pio_mask;
  3914. ap->mwdma_mask = ent->mwdma_mask;
  3915. ap->udma_mask = ent->udma_mask;
  3916. ap->flags |= ent->host_flags;
  3917. ap->ops = ent->port_ops;
  3918. ap->cbl = ATA_CBL_NONE;
  3919. ap->sata_spd_limit = UINT_MAX;
  3920. ap->active_tag = ATA_TAG_POISON;
  3921. ap->last_ctl = 0xFF;
  3922. INIT_WORK(&ap->port_task, NULL, NULL);
  3923. INIT_LIST_HEAD(&ap->eh_done_q);
  3924. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  3925. struct ata_device *dev = &ap->device[i];
  3926. dev->devno = i;
  3927. dev->pio_mask = UINT_MAX;
  3928. dev->mwdma_mask = UINT_MAX;
  3929. dev->udma_mask = UINT_MAX;
  3930. }
  3931. #ifdef ATA_IRQ_TRAP
  3932. ap->stats.unhandled_irq = 1;
  3933. ap->stats.idle_irq = 1;
  3934. #endif
  3935. memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
  3936. }
  3937. /**
  3938. * ata_host_add - Attach low-level ATA driver to system
  3939. * @ent: Information provided by low-level driver
  3940. * @host_set: Collections of ports to which we add
  3941. * @port_no: Port number associated with this host
  3942. *
  3943. * Attach low-level ATA driver to system.
  3944. *
  3945. * LOCKING:
  3946. * PCI/etc. bus probe sem.
  3947. *
  3948. * RETURNS:
  3949. * New ata_port on success, for NULL on error.
  3950. */
  3951. static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
  3952. struct ata_host_set *host_set,
  3953. unsigned int port_no)
  3954. {
  3955. struct Scsi_Host *host;
  3956. struct ata_port *ap;
  3957. int rc;
  3958. DPRINTK("ENTER\n");
  3959. if (!ent->port_ops->probe_reset &&
  3960. !(ent->host_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) {
  3961. printk(KERN_ERR "ata%u: no reset mechanism available\n",
  3962. port_no);
  3963. return NULL;
  3964. }
  3965. host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
  3966. if (!host)
  3967. return NULL;
  3968. host->transportt = &ata_scsi_transport_template;
  3969. ap = ata_shost_to_port(host);
  3970. ata_host_init(ap, host, host_set, ent, port_no);
  3971. rc = ap->ops->port_start(ap);
  3972. if (rc)
  3973. goto err_out;
  3974. return ap;
  3975. err_out:
  3976. scsi_host_put(host);
  3977. return NULL;
  3978. }
  3979. /**
  3980. * ata_device_add - Register hardware device with ATA and SCSI layers
  3981. * @ent: Probe information describing hardware device to be registered
  3982. *
  3983. * This function processes the information provided in the probe
  3984. * information struct @ent, allocates the necessary ATA and SCSI
  3985. * host information structures, initializes them, and registers
  3986. * everything with requisite kernel subsystems.
  3987. *
  3988. * This function requests irqs, probes the ATA bus, and probes
  3989. * the SCSI bus.
  3990. *
  3991. * LOCKING:
  3992. * PCI/etc. bus probe sem.
  3993. *
  3994. * RETURNS:
  3995. * Number of ports registered. Zero on error (no ports registered).
  3996. */
  3997. int ata_device_add(const struct ata_probe_ent *ent)
  3998. {
  3999. unsigned int count = 0, i;
  4000. struct device *dev = ent->dev;
  4001. struct ata_host_set *host_set;
  4002. DPRINTK("ENTER\n");
  4003. /* alloc a container for our list of ATA ports (buses) */
  4004. host_set = kzalloc(sizeof(struct ata_host_set) +
  4005. (ent->n_ports * sizeof(void *)), GFP_KERNEL);
  4006. if (!host_set)
  4007. return 0;
  4008. spin_lock_init(&host_set->lock);
  4009. host_set->dev = dev;
  4010. host_set->n_ports = ent->n_ports;
  4011. host_set->irq = ent->irq;
  4012. host_set->mmio_base = ent->mmio_base;
  4013. host_set->private_data = ent->private_data;
  4014. host_set->ops = ent->port_ops;
  4015. host_set->flags = ent->host_set_flags;
  4016. /* register each port bound to this device */
  4017. for (i = 0; i < ent->n_ports; i++) {
  4018. struct ata_port *ap;
  4019. unsigned long xfer_mode_mask;
  4020. ap = ata_host_add(ent, host_set, i);
  4021. if (!ap)
  4022. goto err_out;
  4023. host_set->ports[i] = ap;
  4024. xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
  4025. (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
  4026. (ap->pio_mask << ATA_SHIFT_PIO);
  4027. /* print per-port info to dmesg */
  4028. printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
  4029. "bmdma 0x%lX irq %lu\n",
  4030. ap->id,
  4031. ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
  4032. ata_mode_string(xfer_mode_mask),
  4033. ap->ioaddr.cmd_addr,
  4034. ap->ioaddr.ctl_addr,
  4035. ap->ioaddr.bmdma_addr,
  4036. ent->irq);
  4037. ata_chk_status(ap);
  4038. host_set->ops->irq_clear(ap);
  4039. count++;
  4040. }
  4041. if (!count)
  4042. goto err_free_ret;
  4043. /* obtain irq, that is shared between channels */
  4044. if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
  4045. DRV_NAME, host_set))
  4046. goto err_out;
  4047. /* perform each probe synchronously */
  4048. DPRINTK("probe begin\n");
  4049. for (i = 0; i < count; i++) {
  4050. struct ata_port *ap;
  4051. int rc;
  4052. ap = host_set->ports[i];
  4053. DPRINTK("ata%u: bus probe begin\n", ap->id);
  4054. rc = ata_bus_probe(ap);
  4055. DPRINTK("ata%u: bus probe end\n", ap->id);
  4056. if (rc) {
  4057. /* FIXME: do something useful here?
  4058. * Current libata behavior will
  4059. * tear down everything when
  4060. * the module is removed
  4061. * or the h/w is unplugged.
  4062. */
  4063. }
  4064. rc = scsi_add_host(ap->host, dev);
  4065. if (rc) {
  4066. printk(KERN_ERR "ata%u: scsi_add_host failed\n",
  4067. ap->id);
  4068. /* FIXME: do something useful here */
  4069. /* FIXME: handle unconditional calls to
  4070. * scsi_scan_host and ata_host_remove, below,
  4071. * at the very least
  4072. */
  4073. }
  4074. }
  4075. /* probes are done, now scan each port's disk(s) */
  4076. DPRINTK("host probe begin\n");
  4077. for (i = 0; i < count; i++) {
  4078. struct ata_port *ap = host_set->ports[i];
  4079. ata_scsi_scan_host(ap);
  4080. }
  4081. dev_set_drvdata(dev, host_set);
  4082. VPRINTK("EXIT, returning %u\n", ent->n_ports);
  4083. return ent->n_ports; /* success */
  4084. err_out:
  4085. for (i = 0; i < count; i++) {
  4086. ata_host_remove(host_set->ports[i], 1);
  4087. scsi_host_put(host_set->ports[i]->host);
  4088. }
  4089. err_free_ret:
  4090. kfree(host_set);
  4091. VPRINTK("EXIT, returning 0\n");
  4092. return 0;
  4093. }
  4094. /**
  4095. * ata_host_set_remove - PCI layer callback for device removal
  4096. * @host_set: ATA host set that was removed
  4097. *
  4098. * Unregister all objects associated with this host set. Free those
  4099. * objects.
  4100. *
  4101. * LOCKING:
  4102. * Inherited from calling layer (may sleep).
  4103. */
  4104. void ata_host_set_remove(struct ata_host_set *host_set)
  4105. {
  4106. struct ata_port *ap;
  4107. unsigned int i;
  4108. for (i = 0; i < host_set->n_ports; i++) {
  4109. ap = host_set->ports[i];
  4110. scsi_remove_host(ap->host);
  4111. }
  4112. free_irq(host_set->irq, host_set);
  4113. for (i = 0; i < host_set->n_ports; i++) {
  4114. ap = host_set->ports[i];
  4115. ata_scsi_release(ap->host);
  4116. if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
  4117. struct ata_ioports *ioaddr = &ap->ioaddr;
  4118. if (ioaddr->cmd_addr == 0x1f0)
  4119. release_region(0x1f0, 8);
  4120. else if (ioaddr->cmd_addr == 0x170)
  4121. release_region(0x170, 8);
  4122. }
  4123. scsi_host_put(ap->host);
  4124. }
  4125. if (host_set->ops->host_stop)
  4126. host_set->ops->host_stop(host_set);
  4127. kfree(host_set);
  4128. }
  4129. /**
  4130. * ata_scsi_release - SCSI layer callback hook for host unload
  4131. * @host: libata host to be unloaded
  4132. *
  4133. * Performs all duties necessary to shut down a libata port...
  4134. * Kill port kthread, disable port, and release resources.
  4135. *
  4136. * LOCKING:
  4137. * Inherited from SCSI layer.
  4138. *
  4139. * RETURNS:
  4140. * One.
  4141. */
  4142. int ata_scsi_release(struct Scsi_Host *host)
  4143. {
  4144. struct ata_port *ap = ata_shost_to_port(host);
  4145. int i;
  4146. DPRINTK("ENTER\n");
  4147. ap->ops->port_disable(ap);
  4148. ata_host_remove(ap, 0);
  4149. for (i = 0; i < ATA_MAX_DEVICES; i++)
  4150. kfree(ap->device[i].id);
  4151. DPRINTK("EXIT\n");
  4152. return 1;
  4153. }
  4154. /**
  4155. * ata_std_ports - initialize ioaddr with standard port offsets.
  4156. * @ioaddr: IO address structure to be initialized
  4157. *
  4158. * Utility function which initializes data_addr, error_addr,
  4159. * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
  4160. * device_addr, status_addr, and command_addr to standard offsets
  4161. * relative to cmd_addr.
  4162. *
  4163. * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
  4164. */
  4165. void ata_std_ports(struct ata_ioports *ioaddr)
  4166. {
  4167. ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
  4168. ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
  4169. ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
  4170. ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
  4171. ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
  4172. ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
  4173. ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
  4174. ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
  4175. ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
  4176. ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
  4177. }
  4178. #ifdef CONFIG_PCI
  4179. void ata_pci_host_stop (struct ata_host_set *host_set)
  4180. {
  4181. struct pci_dev *pdev = to_pci_dev(host_set->dev);
  4182. pci_iounmap(pdev, host_set->mmio_base);
  4183. }
  4184. /**
  4185. * ata_pci_remove_one - PCI layer callback for device removal
  4186. * @pdev: PCI device that was removed
  4187. *
  4188. * PCI layer indicates to libata via this hook that
  4189. * hot-unplug or module unload event has occurred.
  4190. * Handle this by unregistering all objects associated
  4191. * with this PCI device. Free those objects. Then finally
  4192. * release PCI resources and disable device.
  4193. *
  4194. * LOCKING:
  4195. * Inherited from PCI layer (may sleep).
  4196. */
  4197. void ata_pci_remove_one (struct pci_dev *pdev)
  4198. {
  4199. struct device *dev = pci_dev_to_dev(pdev);
  4200. struct ata_host_set *host_set = dev_get_drvdata(dev);
  4201. ata_host_set_remove(host_set);
  4202. pci_release_regions(pdev);
  4203. pci_disable_device(pdev);
  4204. dev_set_drvdata(dev, NULL);
  4205. }
  4206. /* move to PCI subsystem */
  4207. int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
  4208. {
  4209. unsigned long tmp = 0;
  4210. switch (bits->width) {
  4211. case 1: {
  4212. u8 tmp8 = 0;
  4213. pci_read_config_byte(pdev, bits->reg, &tmp8);
  4214. tmp = tmp8;
  4215. break;
  4216. }
  4217. case 2: {
  4218. u16 tmp16 = 0;
  4219. pci_read_config_word(pdev, bits->reg, &tmp16);
  4220. tmp = tmp16;
  4221. break;
  4222. }
  4223. case 4: {
  4224. u32 tmp32 = 0;
  4225. pci_read_config_dword(pdev, bits->reg, &tmp32);
  4226. tmp = tmp32;
  4227. break;
  4228. }
  4229. default:
  4230. return -EINVAL;
  4231. }
  4232. tmp &= bits->mask;
  4233. return (tmp == bits->val) ? 1 : 0;
  4234. }
  4235. int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
  4236. {
  4237. pci_save_state(pdev);
  4238. pci_disable_device(pdev);
  4239. pci_set_power_state(pdev, PCI_D3hot);
  4240. return 0;
  4241. }
  4242. int ata_pci_device_resume(struct pci_dev *pdev)
  4243. {
  4244. pci_set_power_state(pdev, PCI_D0);
  4245. pci_restore_state(pdev);
  4246. pci_enable_device(pdev);
  4247. pci_set_master(pdev);
  4248. return 0;
  4249. }
  4250. #endif /* CONFIG_PCI */
  4251. static int __init ata_init(void)
  4252. {
  4253. ata_wq = create_workqueue("ata");
  4254. if (!ata_wq)
  4255. return -ENOMEM;
  4256. printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
  4257. return 0;
  4258. }
  4259. static void __exit ata_exit(void)
  4260. {
  4261. destroy_workqueue(ata_wq);
  4262. }
  4263. module_init(ata_init);
  4264. module_exit(ata_exit);
  4265. static unsigned long ratelimit_time;
  4266. static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
  4267. int ata_ratelimit(void)
  4268. {
  4269. int rc;
  4270. unsigned long flags;
  4271. spin_lock_irqsave(&ata_ratelimit_lock, flags);
  4272. if (time_after(jiffies, ratelimit_time)) {
  4273. rc = 1;
  4274. ratelimit_time = jiffies + (HZ/5);
  4275. } else
  4276. rc = 0;
  4277. spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
  4278. return rc;
  4279. }
  4280. /*
  4281. * libata is essentially a library of internal helper functions for
  4282. * low-level ATA host controller drivers. As such, the API/ABI is
  4283. * likely to change as new drivers are added and updated.
  4284. * Do not depend on ABI/API stability.
  4285. */
  4286. EXPORT_SYMBOL_GPL(ata_std_bios_param);
  4287. EXPORT_SYMBOL_GPL(ata_std_ports);
  4288. EXPORT_SYMBOL_GPL(ata_device_add);
  4289. EXPORT_SYMBOL_GPL(ata_host_set_remove);
  4290. EXPORT_SYMBOL_GPL(ata_sg_init);
  4291. EXPORT_SYMBOL_GPL(ata_sg_init_one);
  4292. EXPORT_SYMBOL_GPL(__ata_qc_complete);
  4293. EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
  4294. EXPORT_SYMBOL_GPL(ata_tf_load);
  4295. EXPORT_SYMBOL_GPL(ata_tf_read);
  4296. EXPORT_SYMBOL_GPL(ata_noop_dev_select);
  4297. EXPORT_SYMBOL_GPL(ata_std_dev_select);
  4298. EXPORT_SYMBOL_GPL(ata_tf_to_fis);
  4299. EXPORT_SYMBOL_GPL(ata_tf_from_fis);
  4300. EXPORT_SYMBOL_GPL(ata_check_status);
  4301. EXPORT_SYMBOL_GPL(ata_altstatus);
  4302. EXPORT_SYMBOL_GPL(ata_exec_command);
  4303. EXPORT_SYMBOL_GPL(ata_port_start);
  4304. EXPORT_SYMBOL_GPL(ata_port_stop);
  4305. EXPORT_SYMBOL_GPL(ata_host_stop);
  4306. EXPORT_SYMBOL_GPL(ata_interrupt);
  4307. EXPORT_SYMBOL_GPL(ata_qc_prep);
  4308. EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
  4309. EXPORT_SYMBOL_GPL(ata_bmdma_setup);
  4310. EXPORT_SYMBOL_GPL(ata_bmdma_start);
  4311. EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
  4312. EXPORT_SYMBOL_GPL(ata_bmdma_status);
  4313. EXPORT_SYMBOL_GPL(ata_bmdma_stop);
  4314. EXPORT_SYMBOL_GPL(ata_port_probe);
  4315. EXPORT_SYMBOL_GPL(sata_phy_reset);
  4316. EXPORT_SYMBOL_GPL(__sata_phy_reset);
  4317. EXPORT_SYMBOL_GPL(ata_bus_reset);
  4318. EXPORT_SYMBOL_GPL(ata_std_probeinit);
  4319. EXPORT_SYMBOL_GPL(ata_std_softreset);
  4320. EXPORT_SYMBOL_GPL(sata_std_hardreset);
  4321. EXPORT_SYMBOL_GPL(ata_std_postreset);
  4322. EXPORT_SYMBOL_GPL(ata_std_probe_reset);
  4323. EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
  4324. EXPORT_SYMBOL_GPL(ata_dev_revalidate);
  4325. EXPORT_SYMBOL_GPL(ata_dev_classify);
  4326. EXPORT_SYMBOL_GPL(ata_dev_pair);
  4327. EXPORT_SYMBOL_GPL(ata_port_disable);
  4328. EXPORT_SYMBOL_GPL(ata_ratelimit);
  4329. EXPORT_SYMBOL_GPL(ata_busy_sleep);
  4330. EXPORT_SYMBOL_GPL(ata_port_queue_task);
  4331. EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
  4332. EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
  4333. EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
  4334. EXPORT_SYMBOL_GPL(ata_scsi_release);
  4335. EXPORT_SYMBOL_GPL(ata_host_intr);
  4336. EXPORT_SYMBOL_GPL(ata_id_string);
  4337. EXPORT_SYMBOL_GPL(ata_id_c_string);
  4338. EXPORT_SYMBOL_GPL(ata_scsi_simulate);
  4339. EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
  4340. EXPORT_SYMBOL_GPL(ata_timing_compute);
  4341. EXPORT_SYMBOL_GPL(ata_timing_merge);
  4342. #ifdef CONFIG_PCI
  4343. EXPORT_SYMBOL_GPL(pci_test_config_bits);
  4344. EXPORT_SYMBOL_GPL(ata_pci_host_stop);
  4345. EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
  4346. EXPORT_SYMBOL_GPL(ata_pci_init_one);
  4347. EXPORT_SYMBOL_GPL(ata_pci_remove_one);
  4348. EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
  4349. EXPORT_SYMBOL_GPL(ata_pci_device_resume);
  4350. EXPORT_SYMBOL_GPL(ata_pci_default_filter);
  4351. EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
  4352. #endif /* CONFIG_PCI */
  4353. EXPORT_SYMBOL_GPL(ata_device_suspend);
  4354. EXPORT_SYMBOL_GPL(ata_device_resume);
  4355. EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
  4356. EXPORT_SYMBOL_GPL(ata_scsi_device_resume);
  4357. EXPORT_SYMBOL_GPL(ata_scsi_error);
  4358. EXPORT_SYMBOL_GPL(ata_eng_timeout);
  4359. EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
  4360. EXPORT_SYMBOL_GPL(ata_eh_qc_retry);