libata-core.c 127 KB

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