libata-core.c 125 KB

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