libata-core.c 124 KB

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