libata-core.c 129 KB

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