libata-core.c 155 KB

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