libata-core.c 125 KB

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