libata-core.c 123 KB

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