libata-core.c 125 KB

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