libata-core.c 125 KB

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