libata-core.c 156 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423
  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. * @unknown: 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. We have to do the identify
  1593. specific sequence bass-ackwards so that PDIAG- is released by
  1594. the slave device */
  1595. for (i = ATA_MAX_DEVICES - 1; i >= 0; i--) {
  1596. dev = &ap->device[i];
  1597. if (tries[i])
  1598. dev->class = classes[i];
  1599. if (!ata_dev_enabled(dev))
  1600. continue;
  1601. rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
  1602. dev->id);
  1603. if (rc)
  1604. goto fail;
  1605. }
  1606. /* After the identify sequence we can now set up the devices. We do
  1607. this in the normal order so that the user doesn't get confused */
  1608. for(i = 0; i < ATA_MAX_DEVICES; i++) {
  1609. dev = &ap->device[i];
  1610. if (!ata_dev_enabled(dev))
  1611. continue;
  1612. ap->eh_context.i.flags |= ATA_EHI_PRINTINFO;
  1613. rc = ata_dev_configure(dev);
  1614. ap->eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
  1615. if (rc)
  1616. goto fail;
  1617. }
  1618. /* configure transfer mode */
  1619. rc = ata_set_mode(ap, &dev);
  1620. if (rc)
  1621. goto fail;
  1622. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1623. if (ata_dev_enabled(&ap->device[i]))
  1624. return 0;
  1625. /* no device present, disable port */
  1626. ata_port_disable(ap);
  1627. ap->ops->port_disable(ap);
  1628. return -ENODEV;
  1629. fail:
  1630. tries[dev->devno]--;
  1631. switch (rc) {
  1632. case -EINVAL:
  1633. /* eeek, something went very wrong, give up */
  1634. tries[dev->devno] = 0;
  1635. break;
  1636. case -ENODEV:
  1637. /* give it just one more chance */
  1638. tries[dev->devno] = min(tries[dev->devno], 1);
  1639. case -EIO:
  1640. if (tries[dev->devno] == 1) {
  1641. /* This is the last chance, better to slow
  1642. * down than lose it.
  1643. */
  1644. sata_down_spd_limit(ap);
  1645. ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
  1646. }
  1647. }
  1648. if (!tries[dev->devno])
  1649. ata_dev_disable(dev);
  1650. goto retry;
  1651. }
  1652. /**
  1653. * ata_port_probe - Mark port as enabled
  1654. * @ap: Port for which we indicate enablement
  1655. *
  1656. * Modify @ap data structure such that the system
  1657. * thinks that the entire port is enabled.
  1658. *
  1659. * LOCKING: host lock, or some other form of
  1660. * serialization.
  1661. */
  1662. void ata_port_probe(struct ata_port *ap)
  1663. {
  1664. ap->flags &= ~ATA_FLAG_DISABLED;
  1665. }
  1666. /**
  1667. * sata_print_link_status - Print SATA link status
  1668. * @ap: SATA port to printk link status about
  1669. *
  1670. * This function prints link speed and status of a SATA link.
  1671. *
  1672. * LOCKING:
  1673. * None.
  1674. */
  1675. static void sata_print_link_status(struct ata_port *ap)
  1676. {
  1677. u32 sstatus, scontrol, tmp;
  1678. if (sata_scr_read(ap, SCR_STATUS, &sstatus))
  1679. return;
  1680. sata_scr_read(ap, SCR_CONTROL, &scontrol);
  1681. if (ata_port_online(ap)) {
  1682. tmp = (sstatus >> 4) & 0xf;
  1683. ata_port_printk(ap, KERN_INFO,
  1684. "SATA link up %s (SStatus %X SControl %X)\n",
  1685. sata_spd_string(tmp), sstatus, scontrol);
  1686. } else {
  1687. ata_port_printk(ap, KERN_INFO,
  1688. "SATA link down (SStatus %X SControl %X)\n",
  1689. sstatus, scontrol);
  1690. }
  1691. }
  1692. /**
  1693. * __sata_phy_reset - Wake/reset a low-level SATA PHY
  1694. * @ap: SATA port associated with target SATA PHY.
  1695. *
  1696. * This function issues commands to standard SATA Sxxx
  1697. * PHY registers, to wake up the phy (and device), and
  1698. * clear any reset condition.
  1699. *
  1700. * LOCKING:
  1701. * PCI/etc. bus probe sem.
  1702. *
  1703. */
  1704. void __sata_phy_reset(struct ata_port *ap)
  1705. {
  1706. u32 sstatus;
  1707. unsigned long timeout = jiffies + (HZ * 5);
  1708. if (ap->flags & ATA_FLAG_SATA_RESET) {
  1709. /* issue phy wake/reset */
  1710. sata_scr_write_flush(ap, SCR_CONTROL, 0x301);
  1711. /* Couldn't find anything in SATA I/II specs, but
  1712. * AHCI-1.1 10.4.2 says at least 1 ms. */
  1713. mdelay(1);
  1714. }
  1715. /* phy wake/clear reset */
  1716. sata_scr_write_flush(ap, SCR_CONTROL, 0x300);
  1717. /* wait for phy to become ready, if necessary */
  1718. do {
  1719. msleep(200);
  1720. sata_scr_read(ap, SCR_STATUS, &sstatus);
  1721. if ((sstatus & 0xf) != 1)
  1722. break;
  1723. } while (time_before(jiffies, timeout));
  1724. /* print link status */
  1725. sata_print_link_status(ap);
  1726. /* TODO: phy layer with polling, timeouts, etc. */
  1727. if (!ata_port_offline(ap))
  1728. ata_port_probe(ap);
  1729. else
  1730. ata_port_disable(ap);
  1731. if (ap->flags & ATA_FLAG_DISABLED)
  1732. return;
  1733. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  1734. ata_port_disable(ap);
  1735. return;
  1736. }
  1737. ap->cbl = ATA_CBL_SATA;
  1738. }
  1739. /**
  1740. * sata_phy_reset - Reset SATA bus.
  1741. * @ap: SATA port associated with target SATA PHY.
  1742. *
  1743. * This function resets the SATA bus, and then probes
  1744. * the bus for devices.
  1745. *
  1746. * LOCKING:
  1747. * PCI/etc. bus probe sem.
  1748. *
  1749. */
  1750. void sata_phy_reset(struct ata_port *ap)
  1751. {
  1752. __sata_phy_reset(ap);
  1753. if (ap->flags & ATA_FLAG_DISABLED)
  1754. return;
  1755. ata_bus_reset(ap);
  1756. }
  1757. /**
  1758. * ata_dev_pair - return other device on cable
  1759. * @adev: device
  1760. *
  1761. * Obtain the other device on the same cable, or if none is
  1762. * present NULL is returned
  1763. */
  1764. struct ata_device *ata_dev_pair(struct ata_device *adev)
  1765. {
  1766. struct ata_port *ap = adev->ap;
  1767. struct ata_device *pair = &ap->device[1 - adev->devno];
  1768. if (!ata_dev_enabled(pair))
  1769. return NULL;
  1770. return pair;
  1771. }
  1772. /**
  1773. * ata_port_disable - Disable port.
  1774. * @ap: Port to be disabled.
  1775. *
  1776. * Modify @ap data structure such that the system
  1777. * thinks that the entire port is disabled, and should
  1778. * never attempt to probe or communicate with devices
  1779. * on this port.
  1780. *
  1781. * LOCKING: host lock, or some other form of
  1782. * serialization.
  1783. */
  1784. void ata_port_disable(struct ata_port *ap)
  1785. {
  1786. ap->device[0].class = ATA_DEV_NONE;
  1787. ap->device[1].class = ATA_DEV_NONE;
  1788. ap->flags |= ATA_FLAG_DISABLED;
  1789. }
  1790. /**
  1791. * sata_down_spd_limit - adjust SATA spd limit downward
  1792. * @ap: Port to adjust SATA spd limit for
  1793. *
  1794. * Adjust SATA spd limit of @ap downward. Note that this
  1795. * function only adjusts the limit. The change must be applied
  1796. * using sata_set_spd().
  1797. *
  1798. * LOCKING:
  1799. * Inherited from caller.
  1800. *
  1801. * RETURNS:
  1802. * 0 on success, negative errno on failure
  1803. */
  1804. int sata_down_spd_limit(struct ata_port *ap)
  1805. {
  1806. u32 sstatus, spd, mask;
  1807. int rc, highbit;
  1808. rc = sata_scr_read(ap, SCR_STATUS, &sstatus);
  1809. if (rc)
  1810. return rc;
  1811. mask = ap->sata_spd_limit;
  1812. if (mask <= 1)
  1813. return -EINVAL;
  1814. highbit = fls(mask) - 1;
  1815. mask &= ~(1 << highbit);
  1816. spd = (sstatus >> 4) & 0xf;
  1817. if (spd <= 1)
  1818. return -EINVAL;
  1819. spd--;
  1820. mask &= (1 << spd) - 1;
  1821. if (!mask)
  1822. return -EINVAL;
  1823. ap->sata_spd_limit = mask;
  1824. ata_port_printk(ap, KERN_WARNING, "limiting SATA link speed to %s\n",
  1825. sata_spd_string(fls(mask)));
  1826. return 0;
  1827. }
  1828. static int __sata_set_spd_needed(struct ata_port *ap, u32 *scontrol)
  1829. {
  1830. u32 spd, limit;
  1831. if (ap->sata_spd_limit == UINT_MAX)
  1832. limit = 0;
  1833. else
  1834. limit = fls(ap->sata_spd_limit);
  1835. spd = (*scontrol >> 4) & 0xf;
  1836. *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
  1837. return spd != limit;
  1838. }
  1839. /**
  1840. * sata_set_spd_needed - is SATA spd configuration needed
  1841. * @ap: Port in question
  1842. *
  1843. * Test whether the spd limit in SControl matches
  1844. * @ap->sata_spd_limit. This function is used to determine
  1845. * whether hardreset is necessary to apply SATA spd
  1846. * configuration.
  1847. *
  1848. * LOCKING:
  1849. * Inherited from caller.
  1850. *
  1851. * RETURNS:
  1852. * 1 if SATA spd configuration is needed, 0 otherwise.
  1853. */
  1854. int sata_set_spd_needed(struct ata_port *ap)
  1855. {
  1856. u32 scontrol;
  1857. if (sata_scr_read(ap, SCR_CONTROL, &scontrol))
  1858. return 0;
  1859. return __sata_set_spd_needed(ap, &scontrol);
  1860. }
  1861. /**
  1862. * sata_set_spd - set SATA spd according to spd limit
  1863. * @ap: Port to set SATA spd for
  1864. *
  1865. * Set SATA spd of @ap according to sata_spd_limit.
  1866. *
  1867. * LOCKING:
  1868. * Inherited from caller.
  1869. *
  1870. * RETURNS:
  1871. * 0 if spd doesn't need to be changed, 1 if spd has been
  1872. * changed. Negative errno if SCR registers are inaccessible.
  1873. */
  1874. int sata_set_spd(struct ata_port *ap)
  1875. {
  1876. u32 scontrol;
  1877. int rc;
  1878. if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
  1879. return rc;
  1880. if (!__sata_set_spd_needed(ap, &scontrol))
  1881. return 0;
  1882. if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
  1883. return rc;
  1884. return 1;
  1885. }
  1886. /*
  1887. * This mode timing computation functionality is ported over from
  1888. * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
  1889. */
  1890. /*
  1891. * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
  1892. * These were taken from ATA/ATAPI-6 standard, rev 0a, except
  1893. * for UDMA6, which is currently supported only by Maxtor drives.
  1894. *
  1895. * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
  1896. */
  1897. static const struct ata_timing ata_timing[] = {
  1898. { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
  1899. { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
  1900. { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
  1901. { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
  1902. { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
  1903. { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
  1904. { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
  1905. { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
  1906. { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
  1907. /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
  1908. { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
  1909. { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
  1910. { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
  1911. { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
  1912. { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
  1913. { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
  1914. { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
  1915. { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
  1916. { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
  1917. { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
  1918. { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
  1919. { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
  1920. { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
  1921. /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
  1922. { 0xFF }
  1923. };
  1924. #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
  1925. #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
  1926. static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
  1927. {
  1928. q->setup = EZ(t->setup * 1000, T);
  1929. q->act8b = EZ(t->act8b * 1000, T);
  1930. q->rec8b = EZ(t->rec8b * 1000, T);
  1931. q->cyc8b = EZ(t->cyc8b * 1000, T);
  1932. q->active = EZ(t->active * 1000, T);
  1933. q->recover = EZ(t->recover * 1000, T);
  1934. q->cycle = EZ(t->cycle * 1000, T);
  1935. q->udma = EZ(t->udma * 1000, UT);
  1936. }
  1937. void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
  1938. struct ata_timing *m, unsigned int what)
  1939. {
  1940. if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
  1941. if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
  1942. if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
  1943. if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
  1944. if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
  1945. if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
  1946. if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
  1947. if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
  1948. }
  1949. static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
  1950. {
  1951. const struct ata_timing *t;
  1952. for (t = ata_timing; t->mode != speed; t++)
  1953. if (t->mode == 0xFF)
  1954. return NULL;
  1955. return t;
  1956. }
  1957. int ata_timing_compute(struct ata_device *adev, unsigned short speed,
  1958. struct ata_timing *t, int T, int UT)
  1959. {
  1960. const struct ata_timing *s;
  1961. struct ata_timing p;
  1962. /*
  1963. * Find the mode.
  1964. */
  1965. if (!(s = ata_timing_find_mode(speed)))
  1966. return -EINVAL;
  1967. memcpy(t, s, sizeof(*s));
  1968. /*
  1969. * If the drive is an EIDE drive, it can tell us it needs extended
  1970. * PIO/MW_DMA cycle timing.
  1971. */
  1972. if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
  1973. memset(&p, 0, sizeof(p));
  1974. if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
  1975. if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
  1976. else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
  1977. } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
  1978. p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
  1979. }
  1980. ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
  1981. }
  1982. /*
  1983. * Convert the timing to bus clock counts.
  1984. */
  1985. ata_timing_quantize(t, t, T, UT);
  1986. /*
  1987. * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
  1988. * S.M.A.R.T * and some other commands. We have to ensure that the
  1989. * DMA cycle timing is slower/equal than the fastest PIO timing.
  1990. */
  1991. if (speed > XFER_PIO_6) {
  1992. ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
  1993. ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
  1994. }
  1995. /*
  1996. * Lengthen active & recovery time so that cycle time is correct.
  1997. */
  1998. if (t->act8b + t->rec8b < t->cyc8b) {
  1999. t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
  2000. t->rec8b = t->cyc8b - t->act8b;
  2001. }
  2002. if (t->active + t->recover < t->cycle) {
  2003. t->active += (t->cycle - (t->active + t->recover)) / 2;
  2004. t->recover = t->cycle - t->active;
  2005. }
  2006. return 0;
  2007. }
  2008. /**
  2009. * ata_down_xfermask_limit - adjust dev xfer masks downward
  2010. * @dev: Device to adjust xfer masks
  2011. * @sel: ATA_DNXFER_* selector
  2012. *
  2013. * Adjust xfer masks of @dev downward. Note that this function
  2014. * does not apply the change. Invoking ata_set_mode() afterwards
  2015. * will apply the limit.
  2016. *
  2017. * LOCKING:
  2018. * Inherited from caller.
  2019. *
  2020. * RETURNS:
  2021. * 0 on success, negative errno on failure
  2022. */
  2023. int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
  2024. {
  2025. char buf[32];
  2026. unsigned int orig_mask, xfer_mask;
  2027. unsigned int pio_mask, mwdma_mask, udma_mask;
  2028. int quiet, highbit;
  2029. quiet = !!(sel & ATA_DNXFER_QUIET);
  2030. sel &= ~ATA_DNXFER_QUIET;
  2031. xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
  2032. dev->mwdma_mask,
  2033. dev->udma_mask);
  2034. ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
  2035. switch (sel) {
  2036. case ATA_DNXFER_PIO:
  2037. highbit = fls(pio_mask) - 1;
  2038. pio_mask &= ~(1 << highbit);
  2039. break;
  2040. case ATA_DNXFER_DMA:
  2041. if (udma_mask) {
  2042. highbit = fls(udma_mask) - 1;
  2043. udma_mask &= ~(1 << highbit);
  2044. if (!udma_mask)
  2045. return -ENOENT;
  2046. } else if (mwdma_mask) {
  2047. highbit = fls(mwdma_mask) - 1;
  2048. mwdma_mask &= ~(1 << highbit);
  2049. if (!mwdma_mask)
  2050. return -ENOENT;
  2051. }
  2052. break;
  2053. case ATA_DNXFER_40C:
  2054. udma_mask &= ATA_UDMA_MASK_40C;
  2055. break;
  2056. case ATA_DNXFER_FORCE_PIO0:
  2057. pio_mask &= 1;
  2058. case ATA_DNXFER_FORCE_PIO:
  2059. mwdma_mask = 0;
  2060. udma_mask = 0;
  2061. break;
  2062. default:
  2063. BUG();
  2064. }
  2065. xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
  2066. if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
  2067. return -ENOENT;
  2068. if (!quiet) {
  2069. if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
  2070. snprintf(buf, sizeof(buf), "%s:%s",
  2071. ata_mode_string(xfer_mask),
  2072. ata_mode_string(xfer_mask & ATA_MASK_PIO));
  2073. else
  2074. snprintf(buf, sizeof(buf), "%s",
  2075. ata_mode_string(xfer_mask));
  2076. ata_dev_printk(dev, KERN_WARNING,
  2077. "limiting speed to %s\n", buf);
  2078. }
  2079. ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
  2080. &dev->udma_mask);
  2081. return 0;
  2082. }
  2083. static int ata_dev_set_mode(struct ata_device *dev)
  2084. {
  2085. struct ata_eh_context *ehc = &dev->ap->eh_context;
  2086. unsigned int err_mask;
  2087. int rc;
  2088. dev->flags &= ~ATA_DFLAG_PIO;
  2089. if (dev->xfer_shift == ATA_SHIFT_PIO)
  2090. dev->flags |= ATA_DFLAG_PIO;
  2091. err_mask = ata_dev_set_xfermode(dev);
  2092. /* Old CFA may refuse this command, which is just fine */
  2093. if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
  2094. err_mask &= ~AC_ERR_DEV;
  2095. if (err_mask) {
  2096. ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
  2097. "(err_mask=0x%x)\n", err_mask);
  2098. return -EIO;
  2099. }
  2100. ehc->i.flags |= ATA_EHI_POST_SETMODE;
  2101. rc = ata_dev_revalidate(dev, 0);
  2102. ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
  2103. if (rc)
  2104. return rc;
  2105. DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
  2106. dev->xfer_shift, (int)dev->xfer_mode);
  2107. ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
  2108. ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
  2109. return 0;
  2110. }
  2111. /**
  2112. * ata_set_mode - Program timings and issue SET FEATURES - XFER
  2113. * @ap: port on which timings will be programmed
  2114. * @r_failed_dev: out paramter for failed device
  2115. *
  2116. * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
  2117. * ata_set_mode() fails, pointer to the failing device is
  2118. * returned in @r_failed_dev.
  2119. *
  2120. * LOCKING:
  2121. * PCI/etc. bus probe sem.
  2122. *
  2123. * RETURNS:
  2124. * 0 on success, negative errno otherwise
  2125. */
  2126. int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
  2127. {
  2128. struct ata_device *dev;
  2129. int i, rc = 0, used_dma = 0, found = 0;
  2130. /* has private set_mode? */
  2131. if (ap->ops->set_mode)
  2132. return ap->ops->set_mode(ap, r_failed_dev);
  2133. /* step 1: calculate xfer_mask */
  2134. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  2135. unsigned int pio_mask, dma_mask;
  2136. dev = &ap->device[i];
  2137. if (!ata_dev_enabled(dev))
  2138. continue;
  2139. ata_dev_xfermask(dev);
  2140. pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
  2141. dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
  2142. dev->pio_mode = ata_xfer_mask2mode(pio_mask);
  2143. dev->dma_mode = ata_xfer_mask2mode(dma_mask);
  2144. found = 1;
  2145. if (dev->dma_mode)
  2146. used_dma = 1;
  2147. }
  2148. if (!found)
  2149. goto out;
  2150. /* step 2: always set host PIO timings */
  2151. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  2152. dev = &ap->device[i];
  2153. if (!ata_dev_enabled(dev))
  2154. continue;
  2155. if (!dev->pio_mode) {
  2156. ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
  2157. rc = -EINVAL;
  2158. goto out;
  2159. }
  2160. dev->xfer_mode = dev->pio_mode;
  2161. dev->xfer_shift = ATA_SHIFT_PIO;
  2162. if (ap->ops->set_piomode)
  2163. ap->ops->set_piomode(ap, dev);
  2164. }
  2165. /* step 3: set host DMA timings */
  2166. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  2167. dev = &ap->device[i];
  2168. if (!ata_dev_enabled(dev) || !dev->dma_mode)
  2169. continue;
  2170. dev->xfer_mode = dev->dma_mode;
  2171. dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
  2172. if (ap->ops->set_dmamode)
  2173. ap->ops->set_dmamode(ap, dev);
  2174. }
  2175. /* step 4: update devices' xfer mode */
  2176. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  2177. dev = &ap->device[i];
  2178. /* don't update suspended devices' xfer mode */
  2179. if (!ata_dev_ready(dev))
  2180. continue;
  2181. rc = ata_dev_set_mode(dev);
  2182. if (rc)
  2183. goto out;
  2184. }
  2185. /* Record simplex status. If we selected DMA then the other
  2186. * host channels are not permitted to do so.
  2187. */
  2188. if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
  2189. ap->host->simplex_claimed = ap;
  2190. /* step5: chip specific finalisation */
  2191. if (ap->ops->post_set_mode)
  2192. ap->ops->post_set_mode(ap);
  2193. out:
  2194. if (rc)
  2195. *r_failed_dev = dev;
  2196. return rc;
  2197. }
  2198. /**
  2199. * ata_tf_to_host - issue ATA taskfile to host controller
  2200. * @ap: port to which command is being issued
  2201. * @tf: ATA taskfile register set
  2202. *
  2203. * Issues ATA taskfile register set to ATA host controller,
  2204. * with proper synchronization with interrupt handler and
  2205. * other threads.
  2206. *
  2207. * LOCKING:
  2208. * spin_lock_irqsave(host lock)
  2209. */
  2210. static inline void ata_tf_to_host(struct ata_port *ap,
  2211. const struct ata_taskfile *tf)
  2212. {
  2213. ap->ops->tf_load(ap, tf);
  2214. ap->ops->exec_command(ap, tf);
  2215. }
  2216. /**
  2217. * ata_busy_sleep - sleep until BSY clears, or timeout
  2218. * @ap: port containing status register to be polled
  2219. * @tmout_pat: impatience timeout
  2220. * @tmout: overall timeout
  2221. *
  2222. * Sleep until ATA Status register bit BSY clears,
  2223. * or a timeout occurs.
  2224. *
  2225. * LOCKING:
  2226. * Kernel thread context (may sleep).
  2227. *
  2228. * RETURNS:
  2229. * 0 on success, -errno otherwise.
  2230. */
  2231. int ata_busy_sleep(struct ata_port *ap,
  2232. unsigned long tmout_pat, unsigned long tmout)
  2233. {
  2234. unsigned long timer_start, timeout;
  2235. u8 status;
  2236. status = ata_busy_wait(ap, ATA_BUSY, 300);
  2237. timer_start = jiffies;
  2238. timeout = timer_start + tmout_pat;
  2239. while (status != 0xff && (status & ATA_BUSY) &&
  2240. time_before(jiffies, timeout)) {
  2241. msleep(50);
  2242. status = ata_busy_wait(ap, ATA_BUSY, 3);
  2243. }
  2244. if (status != 0xff && (status & ATA_BUSY))
  2245. ata_port_printk(ap, KERN_WARNING,
  2246. "port is slow to respond, please be patient "
  2247. "(Status 0x%x)\n", status);
  2248. timeout = timer_start + tmout;
  2249. while (status != 0xff && (status & ATA_BUSY) &&
  2250. time_before(jiffies, timeout)) {
  2251. msleep(50);
  2252. status = ata_chk_status(ap);
  2253. }
  2254. if (status == 0xff)
  2255. return -ENODEV;
  2256. if (status & ATA_BUSY) {
  2257. ata_port_printk(ap, KERN_ERR, "port failed to respond "
  2258. "(%lu secs, Status 0x%x)\n",
  2259. tmout / HZ, status);
  2260. return -EBUSY;
  2261. }
  2262. return 0;
  2263. }
  2264. static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
  2265. {
  2266. struct ata_ioports *ioaddr = &ap->ioaddr;
  2267. unsigned int dev0 = devmask & (1 << 0);
  2268. unsigned int dev1 = devmask & (1 << 1);
  2269. unsigned long timeout;
  2270. /* if device 0 was found in ata_devchk, wait for its
  2271. * BSY bit to clear
  2272. */
  2273. if (dev0)
  2274. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  2275. /* if device 1 was found in ata_devchk, wait for
  2276. * register access, then wait for BSY to clear
  2277. */
  2278. timeout = jiffies + ATA_TMOUT_BOOT;
  2279. while (dev1) {
  2280. u8 nsect, lbal;
  2281. ap->ops->dev_select(ap, 1);
  2282. nsect = ioread8(ioaddr->nsect_addr);
  2283. lbal = ioread8(ioaddr->lbal_addr);
  2284. if ((nsect == 1) && (lbal == 1))
  2285. break;
  2286. if (time_after(jiffies, timeout)) {
  2287. dev1 = 0;
  2288. break;
  2289. }
  2290. msleep(50); /* give drive a breather */
  2291. }
  2292. if (dev1)
  2293. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  2294. /* is all this really necessary? */
  2295. ap->ops->dev_select(ap, 0);
  2296. if (dev1)
  2297. ap->ops->dev_select(ap, 1);
  2298. if (dev0)
  2299. ap->ops->dev_select(ap, 0);
  2300. }
  2301. static unsigned int ata_bus_softreset(struct ata_port *ap,
  2302. unsigned int devmask)
  2303. {
  2304. struct ata_ioports *ioaddr = &ap->ioaddr;
  2305. DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
  2306. /* software reset. causes dev0 to be selected */
  2307. iowrite8(ap->ctl, ioaddr->ctl_addr);
  2308. udelay(20); /* FIXME: flush */
  2309. iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
  2310. udelay(20); /* FIXME: flush */
  2311. iowrite8(ap->ctl, ioaddr->ctl_addr);
  2312. /* spec mandates ">= 2ms" before checking status.
  2313. * We wait 150ms, because that was the magic delay used for
  2314. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  2315. * between when the ATA command register is written, and then
  2316. * status is checked. Because waiting for "a while" before
  2317. * checking status is fine, post SRST, we perform this magic
  2318. * delay here as well.
  2319. *
  2320. * Old drivers/ide uses the 2mS rule and then waits for ready
  2321. */
  2322. msleep(150);
  2323. /* Before we perform post reset processing we want to see if
  2324. * the bus shows 0xFF because the odd clown forgets the D7
  2325. * pulldown resistor.
  2326. */
  2327. if (ata_check_status(ap) == 0xFF)
  2328. return 0;
  2329. ata_bus_post_reset(ap, devmask);
  2330. return 0;
  2331. }
  2332. /**
  2333. * ata_bus_reset - reset host port and associated ATA channel
  2334. * @ap: port to reset
  2335. *
  2336. * This is typically the first time we actually start issuing
  2337. * commands to the ATA channel. We wait for BSY to clear, then
  2338. * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
  2339. * result. Determine what devices, if any, are on the channel
  2340. * by looking at the device 0/1 error register. Look at the signature
  2341. * stored in each device's taskfile registers, to determine if
  2342. * the device is ATA or ATAPI.
  2343. *
  2344. * LOCKING:
  2345. * PCI/etc. bus probe sem.
  2346. * Obtains host lock.
  2347. *
  2348. * SIDE EFFECTS:
  2349. * Sets ATA_FLAG_DISABLED if bus reset fails.
  2350. */
  2351. void ata_bus_reset(struct ata_port *ap)
  2352. {
  2353. struct ata_ioports *ioaddr = &ap->ioaddr;
  2354. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  2355. u8 err;
  2356. unsigned int dev0, dev1 = 0, devmask = 0;
  2357. DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
  2358. /* determine if device 0/1 are present */
  2359. if (ap->flags & ATA_FLAG_SATA_RESET)
  2360. dev0 = 1;
  2361. else {
  2362. dev0 = ata_devchk(ap, 0);
  2363. if (slave_possible)
  2364. dev1 = ata_devchk(ap, 1);
  2365. }
  2366. if (dev0)
  2367. devmask |= (1 << 0);
  2368. if (dev1)
  2369. devmask |= (1 << 1);
  2370. /* select device 0 again */
  2371. ap->ops->dev_select(ap, 0);
  2372. /* issue bus reset */
  2373. if (ap->flags & ATA_FLAG_SRST)
  2374. if (ata_bus_softreset(ap, devmask))
  2375. goto err_out;
  2376. /*
  2377. * determine by signature whether we have ATA or ATAPI devices
  2378. */
  2379. ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
  2380. if ((slave_possible) && (err != 0x81))
  2381. ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
  2382. /* re-enable interrupts */
  2383. ap->ops->irq_on(ap);
  2384. /* is double-select really necessary? */
  2385. if (ap->device[1].class != ATA_DEV_NONE)
  2386. ap->ops->dev_select(ap, 1);
  2387. if (ap->device[0].class != ATA_DEV_NONE)
  2388. ap->ops->dev_select(ap, 0);
  2389. /* if no devices were detected, disable this port */
  2390. if ((ap->device[0].class == ATA_DEV_NONE) &&
  2391. (ap->device[1].class == ATA_DEV_NONE))
  2392. goto err_out;
  2393. if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
  2394. /* set up device control for ATA_FLAG_SATA_RESET */
  2395. iowrite8(ap->ctl, ioaddr->ctl_addr);
  2396. }
  2397. DPRINTK("EXIT\n");
  2398. return;
  2399. err_out:
  2400. ata_port_printk(ap, KERN_ERR, "disabling port\n");
  2401. ap->ops->port_disable(ap);
  2402. DPRINTK("EXIT\n");
  2403. }
  2404. /**
  2405. * sata_phy_debounce - debounce SATA phy status
  2406. * @ap: ATA port to debounce SATA phy status for
  2407. * @params: timing parameters { interval, duratinon, timeout } in msec
  2408. *
  2409. * Make sure SStatus of @ap reaches stable state, determined by
  2410. * holding the same value where DET is not 1 for @duration polled
  2411. * every @interval, before @timeout. Timeout constraints the
  2412. * beginning of the stable state. Because, after hot unplugging,
  2413. * DET gets stuck at 1 on some controllers, this functions waits
  2414. * until timeout then returns 0 if DET is stable at 1.
  2415. *
  2416. * LOCKING:
  2417. * Kernel thread context (may sleep)
  2418. *
  2419. * RETURNS:
  2420. * 0 on success, -errno on failure.
  2421. */
  2422. int sata_phy_debounce(struct ata_port *ap, const unsigned long *params)
  2423. {
  2424. unsigned long interval_msec = params[0];
  2425. unsigned long duration = params[1] * HZ / 1000;
  2426. unsigned long timeout = jiffies + params[2] * HZ / 1000;
  2427. unsigned long last_jiffies;
  2428. u32 last, cur;
  2429. int rc;
  2430. if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
  2431. return rc;
  2432. cur &= 0xf;
  2433. last = cur;
  2434. last_jiffies = jiffies;
  2435. while (1) {
  2436. msleep(interval_msec);
  2437. if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
  2438. return rc;
  2439. cur &= 0xf;
  2440. /* DET stable? */
  2441. if (cur == last) {
  2442. if (cur == 1 && time_before(jiffies, timeout))
  2443. continue;
  2444. if (time_after(jiffies, last_jiffies + duration))
  2445. return 0;
  2446. continue;
  2447. }
  2448. /* unstable, start over */
  2449. last = cur;
  2450. last_jiffies = jiffies;
  2451. /* check timeout */
  2452. if (time_after(jiffies, timeout))
  2453. return -EBUSY;
  2454. }
  2455. }
  2456. /**
  2457. * sata_phy_resume - resume SATA phy
  2458. * @ap: ATA port to resume SATA phy for
  2459. * @params: timing parameters { interval, duratinon, timeout } in msec
  2460. *
  2461. * Resume SATA phy of @ap and debounce it.
  2462. *
  2463. * LOCKING:
  2464. * Kernel thread context (may sleep)
  2465. *
  2466. * RETURNS:
  2467. * 0 on success, -errno on failure.
  2468. */
  2469. int sata_phy_resume(struct ata_port *ap, const unsigned long *params)
  2470. {
  2471. u32 scontrol;
  2472. int rc;
  2473. if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
  2474. return rc;
  2475. scontrol = (scontrol & 0x0f0) | 0x300;
  2476. if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
  2477. return rc;
  2478. /* Some PHYs react badly if SStatus is pounded immediately
  2479. * after resuming. Delay 200ms before debouncing.
  2480. */
  2481. msleep(200);
  2482. return sata_phy_debounce(ap, params);
  2483. }
  2484. static void ata_wait_spinup(struct ata_port *ap)
  2485. {
  2486. struct ata_eh_context *ehc = &ap->eh_context;
  2487. unsigned long end, secs;
  2488. int rc;
  2489. /* first, debounce phy if SATA */
  2490. if (ap->cbl == ATA_CBL_SATA) {
  2491. rc = sata_phy_debounce(ap, sata_deb_timing_hotplug);
  2492. /* if debounced successfully and offline, no need to wait */
  2493. if ((rc == 0 || rc == -EOPNOTSUPP) && ata_port_offline(ap))
  2494. return;
  2495. }
  2496. /* okay, let's give the drive time to spin up */
  2497. end = ehc->i.hotplug_timestamp + ATA_SPINUP_WAIT * HZ / 1000;
  2498. secs = ((end - jiffies) + HZ - 1) / HZ;
  2499. if (time_after(jiffies, end))
  2500. return;
  2501. if (secs > 5)
  2502. ata_port_printk(ap, KERN_INFO, "waiting for device to spin up "
  2503. "(%lu secs)\n", secs);
  2504. schedule_timeout_uninterruptible(end - jiffies);
  2505. }
  2506. /**
  2507. * ata_std_prereset - prepare for reset
  2508. * @ap: ATA port to be reset
  2509. *
  2510. * @ap is about to be reset. Initialize it.
  2511. *
  2512. * LOCKING:
  2513. * Kernel thread context (may sleep)
  2514. *
  2515. * RETURNS:
  2516. * 0 on success, -errno otherwise.
  2517. */
  2518. int ata_std_prereset(struct ata_port *ap)
  2519. {
  2520. struct ata_eh_context *ehc = &ap->eh_context;
  2521. const unsigned long *timing = sata_ehc_deb_timing(ehc);
  2522. int rc;
  2523. /* handle link resume & hotplug spinup */
  2524. if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
  2525. (ap->flags & ATA_FLAG_HRST_TO_RESUME))
  2526. ehc->i.action |= ATA_EH_HARDRESET;
  2527. if ((ehc->i.flags & ATA_EHI_HOTPLUGGED) &&
  2528. (ap->flags & ATA_FLAG_SKIP_D2H_BSY))
  2529. ata_wait_spinup(ap);
  2530. /* if we're about to do hardreset, nothing more to do */
  2531. if (ehc->i.action & ATA_EH_HARDRESET)
  2532. return 0;
  2533. /* if SATA, resume phy */
  2534. if (ap->cbl == ATA_CBL_SATA) {
  2535. rc = sata_phy_resume(ap, timing);
  2536. if (rc && rc != -EOPNOTSUPP) {
  2537. /* phy resume failed */
  2538. ata_port_printk(ap, KERN_WARNING, "failed to resume "
  2539. "link for reset (errno=%d)\n", rc);
  2540. return rc;
  2541. }
  2542. }
  2543. /* Wait for !BSY if the controller can wait for the first D2H
  2544. * Reg FIS and we don't know that no device is attached.
  2545. */
  2546. if (!(ap->flags & ATA_FLAG_SKIP_D2H_BSY) && !ata_port_offline(ap))
  2547. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  2548. return 0;
  2549. }
  2550. /**
  2551. * ata_std_softreset - reset host port via ATA SRST
  2552. * @ap: port to reset
  2553. * @classes: resulting classes of attached devices
  2554. *
  2555. * Reset host port using ATA SRST.
  2556. *
  2557. * LOCKING:
  2558. * Kernel thread context (may sleep)
  2559. *
  2560. * RETURNS:
  2561. * 0 on success, -errno otherwise.
  2562. */
  2563. int ata_std_softreset(struct ata_port *ap, unsigned int *classes)
  2564. {
  2565. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  2566. unsigned int devmask = 0, err_mask;
  2567. u8 err;
  2568. DPRINTK("ENTER\n");
  2569. if (ata_port_offline(ap)) {
  2570. classes[0] = ATA_DEV_NONE;
  2571. goto out;
  2572. }
  2573. /* determine if device 0/1 are present */
  2574. if (ata_devchk(ap, 0))
  2575. devmask |= (1 << 0);
  2576. if (slave_possible && ata_devchk(ap, 1))
  2577. devmask |= (1 << 1);
  2578. /* select device 0 again */
  2579. ap->ops->dev_select(ap, 0);
  2580. /* issue bus reset */
  2581. DPRINTK("about to softreset, devmask=%x\n", devmask);
  2582. err_mask = ata_bus_softreset(ap, devmask);
  2583. if (err_mask) {
  2584. ata_port_printk(ap, KERN_ERR, "SRST failed (err_mask=0x%x)\n",
  2585. err_mask);
  2586. return -EIO;
  2587. }
  2588. /* determine by signature whether we have ATA or ATAPI devices */
  2589. classes[0] = ata_dev_try_classify(ap, 0, &err);
  2590. if (slave_possible && err != 0x81)
  2591. classes[1] = ata_dev_try_classify(ap, 1, &err);
  2592. out:
  2593. DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
  2594. return 0;
  2595. }
  2596. /**
  2597. * sata_port_hardreset - reset port via SATA phy reset
  2598. * @ap: port to reset
  2599. * @timing: timing parameters { interval, duratinon, timeout } in msec
  2600. *
  2601. * SATA phy-reset host port using DET bits of SControl register.
  2602. *
  2603. * LOCKING:
  2604. * Kernel thread context (may sleep)
  2605. *
  2606. * RETURNS:
  2607. * 0 on success, -errno otherwise.
  2608. */
  2609. int sata_port_hardreset(struct ata_port *ap, const unsigned long *timing)
  2610. {
  2611. u32 scontrol;
  2612. int rc;
  2613. DPRINTK("ENTER\n");
  2614. if (sata_set_spd_needed(ap)) {
  2615. /* SATA spec says nothing about how to reconfigure
  2616. * spd. To be on the safe side, turn off phy during
  2617. * reconfiguration. This works for at least ICH7 AHCI
  2618. * and Sil3124.
  2619. */
  2620. if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
  2621. goto out;
  2622. scontrol = (scontrol & 0x0f0) | 0x304;
  2623. if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
  2624. goto out;
  2625. sata_set_spd(ap);
  2626. }
  2627. /* issue phy wake/reset */
  2628. if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
  2629. goto out;
  2630. scontrol = (scontrol & 0x0f0) | 0x301;
  2631. if ((rc = sata_scr_write_flush(ap, SCR_CONTROL, scontrol)))
  2632. goto out;
  2633. /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
  2634. * 10.4.2 says at least 1 ms.
  2635. */
  2636. msleep(1);
  2637. /* bring phy back */
  2638. rc = sata_phy_resume(ap, timing);
  2639. out:
  2640. DPRINTK("EXIT, rc=%d\n", rc);
  2641. return rc;
  2642. }
  2643. /**
  2644. * sata_std_hardreset - reset host port via SATA phy reset
  2645. * @ap: port to reset
  2646. * @class: resulting class of attached device
  2647. *
  2648. * SATA phy-reset host port using DET bits of SControl register,
  2649. * wait for !BSY and classify the attached device.
  2650. *
  2651. * LOCKING:
  2652. * Kernel thread context (may sleep)
  2653. *
  2654. * RETURNS:
  2655. * 0 on success, -errno otherwise.
  2656. */
  2657. int sata_std_hardreset(struct ata_port *ap, unsigned int *class)
  2658. {
  2659. const unsigned long *timing = sata_ehc_deb_timing(&ap->eh_context);
  2660. int rc;
  2661. DPRINTK("ENTER\n");
  2662. /* do hardreset */
  2663. rc = sata_port_hardreset(ap, timing);
  2664. if (rc) {
  2665. ata_port_printk(ap, KERN_ERR,
  2666. "COMRESET failed (errno=%d)\n", rc);
  2667. return rc;
  2668. }
  2669. /* TODO: phy layer with polling, timeouts, etc. */
  2670. if (ata_port_offline(ap)) {
  2671. *class = ATA_DEV_NONE;
  2672. DPRINTK("EXIT, link offline\n");
  2673. return 0;
  2674. }
  2675. /* wait a while before checking status, see SRST for more info */
  2676. msleep(150);
  2677. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  2678. ata_port_printk(ap, KERN_ERR,
  2679. "COMRESET failed (device not ready)\n");
  2680. return -EIO;
  2681. }
  2682. ap->ops->dev_select(ap, 0); /* probably unnecessary */
  2683. *class = ata_dev_try_classify(ap, 0, NULL);
  2684. DPRINTK("EXIT, class=%u\n", *class);
  2685. return 0;
  2686. }
  2687. /**
  2688. * ata_std_postreset - standard postreset callback
  2689. * @ap: the target ata_port
  2690. * @classes: classes of attached devices
  2691. *
  2692. * This function is invoked after a successful reset. Note that
  2693. * the device might have been reset more than once using
  2694. * different reset methods before postreset is invoked.
  2695. *
  2696. * LOCKING:
  2697. * Kernel thread context (may sleep)
  2698. */
  2699. void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
  2700. {
  2701. u32 serror;
  2702. DPRINTK("ENTER\n");
  2703. /* print link status */
  2704. sata_print_link_status(ap);
  2705. /* clear SError */
  2706. if (sata_scr_read(ap, SCR_ERROR, &serror) == 0)
  2707. sata_scr_write(ap, SCR_ERROR, serror);
  2708. /* re-enable interrupts */
  2709. if (!ap->ops->error_handler)
  2710. ap->ops->irq_on(ap);
  2711. /* is double-select really necessary? */
  2712. if (classes[0] != ATA_DEV_NONE)
  2713. ap->ops->dev_select(ap, 1);
  2714. if (classes[1] != ATA_DEV_NONE)
  2715. ap->ops->dev_select(ap, 0);
  2716. /* bail out if no device is present */
  2717. if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
  2718. DPRINTK("EXIT, no device\n");
  2719. return;
  2720. }
  2721. /* set up device control */
  2722. if (ap->ioaddr.ctl_addr)
  2723. iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
  2724. DPRINTK("EXIT\n");
  2725. }
  2726. /**
  2727. * ata_dev_same_device - Determine whether new ID matches configured device
  2728. * @dev: device to compare against
  2729. * @new_class: class of the new device
  2730. * @new_id: IDENTIFY page of the new device
  2731. *
  2732. * Compare @new_class and @new_id against @dev and determine
  2733. * whether @dev is the device indicated by @new_class and
  2734. * @new_id.
  2735. *
  2736. * LOCKING:
  2737. * None.
  2738. *
  2739. * RETURNS:
  2740. * 1 if @dev matches @new_class and @new_id, 0 otherwise.
  2741. */
  2742. static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
  2743. const u16 *new_id)
  2744. {
  2745. const u16 *old_id = dev->id;
  2746. unsigned char model[2][ATA_ID_PROD_LEN + 1];
  2747. unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
  2748. u64 new_n_sectors;
  2749. if (dev->class != new_class) {
  2750. ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
  2751. dev->class, new_class);
  2752. return 0;
  2753. }
  2754. ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
  2755. ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
  2756. ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
  2757. ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
  2758. new_n_sectors = ata_id_n_sectors(new_id);
  2759. if (strcmp(model[0], model[1])) {
  2760. ata_dev_printk(dev, KERN_INFO, "model number mismatch "
  2761. "'%s' != '%s'\n", model[0], model[1]);
  2762. return 0;
  2763. }
  2764. if (strcmp(serial[0], serial[1])) {
  2765. ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
  2766. "'%s' != '%s'\n", serial[0], serial[1]);
  2767. return 0;
  2768. }
  2769. if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
  2770. ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
  2771. "%llu != %llu\n",
  2772. (unsigned long long)dev->n_sectors,
  2773. (unsigned long long)new_n_sectors);
  2774. return 0;
  2775. }
  2776. return 1;
  2777. }
  2778. /**
  2779. * ata_dev_revalidate - Revalidate ATA device
  2780. * @dev: device to revalidate
  2781. * @readid_flags: read ID flags
  2782. *
  2783. * Re-read IDENTIFY page and make sure @dev is still attached to
  2784. * the port.
  2785. *
  2786. * LOCKING:
  2787. * Kernel thread context (may sleep)
  2788. *
  2789. * RETURNS:
  2790. * 0 on success, negative errno otherwise
  2791. */
  2792. int ata_dev_revalidate(struct ata_device *dev, unsigned int readid_flags)
  2793. {
  2794. unsigned int class = dev->class;
  2795. u16 *id = (void *)dev->ap->sector_buf;
  2796. int rc;
  2797. if (!ata_dev_enabled(dev)) {
  2798. rc = -ENODEV;
  2799. goto fail;
  2800. }
  2801. /* read ID data */
  2802. rc = ata_dev_read_id(dev, &class, readid_flags, id);
  2803. if (rc)
  2804. goto fail;
  2805. /* is the device still there? */
  2806. if (!ata_dev_same_device(dev, class, id)) {
  2807. rc = -ENODEV;
  2808. goto fail;
  2809. }
  2810. memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
  2811. /* configure device according to the new ID */
  2812. rc = ata_dev_configure(dev);
  2813. if (rc == 0)
  2814. return 0;
  2815. fail:
  2816. ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
  2817. return rc;
  2818. }
  2819. struct ata_blacklist_entry {
  2820. const char *model_num;
  2821. const char *model_rev;
  2822. unsigned long horkage;
  2823. };
  2824. static const struct ata_blacklist_entry ata_device_blacklist [] = {
  2825. /* Devices with DMA related problems under Linux */
  2826. { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
  2827. { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
  2828. { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
  2829. { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
  2830. { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
  2831. { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
  2832. { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
  2833. { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
  2834. { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
  2835. { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
  2836. { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
  2837. { "CRD-84", NULL, ATA_HORKAGE_NODMA },
  2838. { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
  2839. { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
  2840. { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
  2841. { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
  2842. { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
  2843. { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
  2844. { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
  2845. { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
  2846. { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
  2847. { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
  2848. { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
  2849. { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
  2850. { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
  2851. { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
  2852. { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
  2853. { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
  2854. { "SAMSUNG CD-ROM SN-124","N001", ATA_HORKAGE_NODMA },
  2855. /* Devices we expect to fail diagnostics */
  2856. /* Devices where NCQ should be avoided */
  2857. /* NCQ is slow */
  2858. { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
  2859. /* http://thread.gmane.org/gmane.linux.ide/14907 */
  2860. { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
  2861. /* Devices with NCQ limits */
  2862. /* End Marker */
  2863. { }
  2864. };
  2865. unsigned long ata_device_blacklisted(const struct ata_device *dev)
  2866. {
  2867. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  2868. unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
  2869. const struct ata_blacklist_entry *ad = ata_device_blacklist;
  2870. ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
  2871. ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
  2872. while (ad->model_num) {
  2873. if (!strcmp(ad->model_num, model_num)) {
  2874. if (ad->model_rev == NULL)
  2875. return ad->horkage;
  2876. if (!strcmp(ad->model_rev, model_rev))
  2877. return ad->horkage;
  2878. }
  2879. ad++;
  2880. }
  2881. return 0;
  2882. }
  2883. static int ata_dma_blacklisted(const struct ata_device *dev)
  2884. {
  2885. /* We don't support polling DMA.
  2886. * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
  2887. * if the LLDD handles only interrupts in the HSM_ST_LAST state.
  2888. */
  2889. if ((dev->ap->flags & ATA_FLAG_PIO_POLLING) &&
  2890. (dev->flags & ATA_DFLAG_CDB_INTR))
  2891. return 1;
  2892. return (ata_device_blacklisted(dev) & ATA_HORKAGE_NODMA) ? 1 : 0;
  2893. }
  2894. /**
  2895. * ata_dev_xfermask - Compute supported xfermask of the given device
  2896. * @dev: Device to compute xfermask for
  2897. *
  2898. * Compute supported xfermask of @dev and store it in
  2899. * dev->*_mask. This function is responsible for applying all
  2900. * known limits including host controller limits, device
  2901. * blacklist, etc...
  2902. *
  2903. * LOCKING:
  2904. * None.
  2905. */
  2906. static void ata_dev_xfermask(struct ata_device *dev)
  2907. {
  2908. struct ata_port *ap = dev->ap;
  2909. struct ata_host *host = ap->host;
  2910. unsigned long xfer_mask;
  2911. /* controller modes available */
  2912. xfer_mask = ata_pack_xfermask(ap->pio_mask,
  2913. ap->mwdma_mask, ap->udma_mask);
  2914. /* Apply cable rule here. Don't apply it early because when
  2915. * we handle hot plug the cable type can itself change.
  2916. */
  2917. if (ap->cbl == ATA_CBL_PATA40)
  2918. xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
  2919. /* Apply drive side cable rule. Unknown or 80 pin cables reported
  2920. * host side are checked drive side as well. Cases where we know a
  2921. * 40wire cable is used safely for 80 are not checked here.
  2922. */
  2923. if (ata_drive_40wire(dev->id) && (ap->cbl == ATA_CBL_PATA_UNK || ap->cbl == ATA_CBL_PATA80))
  2924. xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
  2925. xfer_mask &= ata_pack_xfermask(dev->pio_mask,
  2926. dev->mwdma_mask, dev->udma_mask);
  2927. xfer_mask &= ata_id_xfermask(dev->id);
  2928. /*
  2929. * CFA Advanced TrueIDE timings are not allowed on a shared
  2930. * cable
  2931. */
  2932. if (ata_dev_pair(dev)) {
  2933. /* No PIO5 or PIO6 */
  2934. xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
  2935. /* No MWDMA3 or MWDMA 4 */
  2936. xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
  2937. }
  2938. if (ata_dma_blacklisted(dev)) {
  2939. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  2940. ata_dev_printk(dev, KERN_WARNING,
  2941. "device is on DMA blacklist, disabling DMA\n");
  2942. }
  2943. if ((host->flags & ATA_HOST_SIMPLEX) &&
  2944. host->simplex_claimed && host->simplex_claimed != ap) {
  2945. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  2946. ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
  2947. "other device, disabling DMA\n");
  2948. }
  2949. if (ap->ops->mode_filter)
  2950. xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask);
  2951. ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
  2952. &dev->mwdma_mask, &dev->udma_mask);
  2953. }
  2954. /**
  2955. * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
  2956. * @dev: Device to which command will be sent
  2957. *
  2958. * Issue SET FEATURES - XFER MODE command to device @dev
  2959. * on port @ap.
  2960. *
  2961. * LOCKING:
  2962. * PCI/etc. bus probe sem.
  2963. *
  2964. * RETURNS:
  2965. * 0 on success, AC_ERR_* mask otherwise.
  2966. */
  2967. static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
  2968. {
  2969. struct ata_taskfile tf;
  2970. unsigned int err_mask;
  2971. /* set up set-features taskfile */
  2972. DPRINTK("set features - xfer mode\n");
  2973. ata_tf_init(dev, &tf);
  2974. tf.command = ATA_CMD_SET_FEATURES;
  2975. tf.feature = SETFEATURES_XFER;
  2976. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2977. tf.protocol = ATA_PROT_NODATA;
  2978. tf.nsect = dev->xfer_mode;
  2979. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
  2980. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  2981. return err_mask;
  2982. }
  2983. /**
  2984. * ata_dev_init_params - Issue INIT DEV PARAMS command
  2985. * @dev: Device to which command will be sent
  2986. * @heads: Number of heads (taskfile parameter)
  2987. * @sectors: Number of sectors (taskfile parameter)
  2988. *
  2989. * LOCKING:
  2990. * Kernel thread context (may sleep)
  2991. *
  2992. * RETURNS:
  2993. * 0 on success, AC_ERR_* mask otherwise.
  2994. */
  2995. static unsigned int ata_dev_init_params(struct ata_device *dev,
  2996. u16 heads, u16 sectors)
  2997. {
  2998. struct ata_taskfile tf;
  2999. unsigned int err_mask;
  3000. /* Number of sectors per track 1-255. Number of heads 1-16 */
  3001. if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
  3002. return AC_ERR_INVALID;
  3003. /* set up init dev params taskfile */
  3004. DPRINTK("init dev params \n");
  3005. ata_tf_init(dev, &tf);
  3006. tf.command = ATA_CMD_INIT_DEV_PARAMS;
  3007. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  3008. tf.protocol = ATA_PROT_NODATA;
  3009. tf.nsect = sectors;
  3010. tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
  3011. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
  3012. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  3013. return err_mask;
  3014. }
  3015. /**
  3016. * ata_sg_clean - Unmap DMA memory associated with command
  3017. * @qc: Command containing DMA memory to be released
  3018. *
  3019. * Unmap all mapped DMA memory associated with this command.
  3020. *
  3021. * LOCKING:
  3022. * spin_lock_irqsave(host lock)
  3023. */
  3024. void ata_sg_clean(struct ata_queued_cmd *qc)
  3025. {
  3026. struct ata_port *ap = qc->ap;
  3027. struct scatterlist *sg = qc->__sg;
  3028. int dir = qc->dma_dir;
  3029. void *pad_buf = NULL;
  3030. WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
  3031. WARN_ON(sg == NULL);
  3032. if (qc->flags & ATA_QCFLAG_SINGLE)
  3033. WARN_ON(qc->n_elem > 1);
  3034. VPRINTK("unmapping %u sg elements\n", qc->n_elem);
  3035. /* if we padded the buffer out to 32-bit bound, and data
  3036. * xfer direction is from-device, we must copy from the
  3037. * pad buffer back into the supplied buffer
  3038. */
  3039. if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
  3040. pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  3041. if (qc->flags & ATA_QCFLAG_SG) {
  3042. if (qc->n_elem)
  3043. dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
  3044. /* restore last sg */
  3045. sg[qc->orig_n_elem - 1].length += qc->pad_len;
  3046. if (pad_buf) {
  3047. struct scatterlist *psg = &qc->pad_sgent;
  3048. void *addr = kmap_atomic(psg->page, KM_IRQ0);
  3049. memcpy(addr + psg->offset, pad_buf, qc->pad_len);
  3050. kunmap_atomic(addr, KM_IRQ0);
  3051. }
  3052. } else {
  3053. if (qc->n_elem)
  3054. dma_unmap_single(ap->dev,
  3055. sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
  3056. dir);
  3057. /* restore sg */
  3058. sg->length += qc->pad_len;
  3059. if (pad_buf)
  3060. memcpy(qc->buf_virt + sg->length - qc->pad_len,
  3061. pad_buf, qc->pad_len);
  3062. }
  3063. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  3064. qc->__sg = NULL;
  3065. }
  3066. /**
  3067. * ata_fill_sg - Fill PCI IDE PRD table
  3068. * @qc: Metadata associated with taskfile to be transferred
  3069. *
  3070. * Fill PCI IDE PRD (scatter-gather) table with segments
  3071. * associated with the current disk command.
  3072. *
  3073. * LOCKING:
  3074. * spin_lock_irqsave(host lock)
  3075. *
  3076. */
  3077. static void ata_fill_sg(struct ata_queued_cmd *qc)
  3078. {
  3079. struct ata_port *ap = qc->ap;
  3080. struct scatterlist *sg;
  3081. unsigned int idx;
  3082. WARN_ON(qc->__sg == NULL);
  3083. WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
  3084. idx = 0;
  3085. ata_for_each_sg(sg, qc) {
  3086. u32 addr, offset;
  3087. u32 sg_len, len;
  3088. /* determine if physical DMA addr spans 64K boundary.
  3089. * Note h/w doesn't support 64-bit, so we unconditionally
  3090. * truncate dma_addr_t to u32.
  3091. */
  3092. addr = (u32) sg_dma_address(sg);
  3093. sg_len = sg_dma_len(sg);
  3094. while (sg_len) {
  3095. offset = addr & 0xffff;
  3096. len = sg_len;
  3097. if ((offset + sg_len) > 0x10000)
  3098. len = 0x10000 - offset;
  3099. ap->prd[idx].addr = cpu_to_le32(addr);
  3100. ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
  3101. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
  3102. idx++;
  3103. sg_len -= len;
  3104. addr += len;
  3105. }
  3106. }
  3107. if (idx)
  3108. ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  3109. }
  3110. /**
  3111. * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
  3112. * @qc: Metadata associated with taskfile to check
  3113. *
  3114. * Allow low-level driver to filter ATA PACKET commands, returning
  3115. * a status indicating whether or not it is OK to use DMA for the
  3116. * supplied PACKET command.
  3117. *
  3118. * LOCKING:
  3119. * spin_lock_irqsave(host lock)
  3120. *
  3121. * RETURNS: 0 when ATAPI DMA can be used
  3122. * nonzero otherwise
  3123. */
  3124. int ata_check_atapi_dma(struct ata_queued_cmd *qc)
  3125. {
  3126. struct ata_port *ap = qc->ap;
  3127. int rc = 0; /* Assume ATAPI DMA is OK by default */
  3128. if (ap->ops->check_atapi_dma)
  3129. rc = ap->ops->check_atapi_dma(qc);
  3130. return rc;
  3131. }
  3132. /**
  3133. * ata_qc_prep - Prepare taskfile for submission
  3134. * @qc: Metadata associated with taskfile to be prepared
  3135. *
  3136. * Prepare ATA taskfile for submission.
  3137. *
  3138. * LOCKING:
  3139. * spin_lock_irqsave(host lock)
  3140. */
  3141. void ata_qc_prep(struct ata_queued_cmd *qc)
  3142. {
  3143. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  3144. return;
  3145. ata_fill_sg(qc);
  3146. }
  3147. void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
  3148. /**
  3149. * ata_sg_init_one - Associate command with memory buffer
  3150. * @qc: Command to be associated
  3151. * @buf: Memory buffer
  3152. * @buflen: Length of memory buffer, in bytes.
  3153. *
  3154. * Initialize the data-related elements of queued_cmd @qc
  3155. * to point to a single memory buffer, @buf of byte length @buflen.
  3156. *
  3157. * LOCKING:
  3158. * spin_lock_irqsave(host lock)
  3159. */
  3160. void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
  3161. {
  3162. qc->flags |= ATA_QCFLAG_SINGLE;
  3163. qc->__sg = &qc->sgent;
  3164. qc->n_elem = 1;
  3165. qc->orig_n_elem = 1;
  3166. qc->buf_virt = buf;
  3167. qc->nbytes = buflen;
  3168. sg_init_one(&qc->sgent, buf, buflen);
  3169. }
  3170. /**
  3171. * ata_sg_init - Associate command with scatter-gather table.
  3172. * @qc: Command to be associated
  3173. * @sg: Scatter-gather table.
  3174. * @n_elem: Number of elements in s/g table.
  3175. *
  3176. * Initialize the data-related elements of queued_cmd @qc
  3177. * to point to a scatter-gather table @sg, containing @n_elem
  3178. * elements.
  3179. *
  3180. * LOCKING:
  3181. * spin_lock_irqsave(host lock)
  3182. */
  3183. void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
  3184. unsigned int n_elem)
  3185. {
  3186. qc->flags |= ATA_QCFLAG_SG;
  3187. qc->__sg = sg;
  3188. qc->n_elem = n_elem;
  3189. qc->orig_n_elem = n_elem;
  3190. }
  3191. /**
  3192. * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
  3193. * @qc: Command with memory buffer to be mapped.
  3194. *
  3195. * DMA-map the memory buffer associated with queued_cmd @qc.
  3196. *
  3197. * LOCKING:
  3198. * spin_lock_irqsave(host lock)
  3199. *
  3200. * RETURNS:
  3201. * Zero on success, negative on error.
  3202. */
  3203. static int ata_sg_setup_one(struct ata_queued_cmd *qc)
  3204. {
  3205. struct ata_port *ap = qc->ap;
  3206. int dir = qc->dma_dir;
  3207. struct scatterlist *sg = qc->__sg;
  3208. dma_addr_t dma_address;
  3209. int trim_sg = 0;
  3210. /* we must lengthen transfers to end on a 32-bit boundary */
  3211. qc->pad_len = sg->length & 3;
  3212. if (qc->pad_len) {
  3213. void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  3214. struct scatterlist *psg = &qc->pad_sgent;
  3215. WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
  3216. memset(pad_buf, 0, ATA_DMA_PAD_SZ);
  3217. if (qc->tf.flags & ATA_TFLAG_WRITE)
  3218. memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
  3219. qc->pad_len);
  3220. sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
  3221. sg_dma_len(psg) = ATA_DMA_PAD_SZ;
  3222. /* trim sg */
  3223. sg->length -= qc->pad_len;
  3224. if (sg->length == 0)
  3225. trim_sg = 1;
  3226. DPRINTK("padding done, sg->length=%u pad_len=%u\n",
  3227. sg->length, qc->pad_len);
  3228. }
  3229. if (trim_sg) {
  3230. qc->n_elem--;
  3231. goto skip_map;
  3232. }
  3233. dma_address = dma_map_single(ap->dev, qc->buf_virt,
  3234. sg->length, dir);
  3235. if (dma_mapping_error(dma_address)) {
  3236. /* restore sg */
  3237. sg->length += qc->pad_len;
  3238. return -1;
  3239. }
  3240. sg_dma_address(sg) = dma_address;
  3241. sg_dma_len(sg) = sg->length;
  3242. skip_map:
  3243. DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
  3244. qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  3245. return 0;
  3246. }
  3247. /**
  3248. * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
  3249. * @qc: Command with scatter-gather table to be mapped.
  3250. *
  3251. * DMA-map the scatter-gather table associated with queued_cmd @qc.
  3252. *
  3253. * LOCKING:
  3254. * spin_lock_irqsave(host lock)
  3255. *
  3256. * RETURNS:
  3257. * Zero on success, negative on error.
  3258. *
  3259. */
  3260. static int ata_sg_setup(struct ata_queued_cmd *qc)
  3261. {
  3262. struct ata_port *ap = qc->ap;
  3263. struct scatterlist *sg = qc->__sg;
  3264. struct scatterlist *lsg = &sg[qc->n_elem - 1];
  3265. int n_elem, pre_n_elem, dir, trim_sg = 0;
  3266. VPRINTK("ENTER, ata%u\n", ap->print_id);
  3267. WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
  3268. /* we must lengthen transfers to end on a 32-bit boundary */
  3269. qc->pad_len = lsg->length & 3;
  3270. if (qc->pad_len) {
  3271. void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  3272. struct scatterlist *psg = &qc->pad_sgent;
  3273. unsigned int offset;
  3274. WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
  3275. memset(pad_buf, 0, ATA_DMA_PAD_SZ);
  3276. /*
  3277. * psg->page/offset are used to copy to-be-written
  3278. * data in this function or read data in ata_sg_clean.
  3279. */
  3280. offset = lsg->offset + lsg->length - qc->pad_len;
  3281. psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
  3282. psg->offset = offset_in_page(offset);
  3283. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  3284. void *addr = kmap_atomic(psg->page, KM_IRQ0);
  3285. memcpy(pad_buf, addr + psg->offset, qc->pad_len);
  3286. kunmap_atomic(addr, KM_IRQ0);
  3287. }
  3288. sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
  3289. sg_dma_len(psg) = ATA_DMA_PAD_SZ;
  3290. /* trim last sg */
  3291. lsg->length -= qc->pad_len;
  3292. if (lsg->length == 0)
  3293. trim_sg = 1;
  3294. DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
  3295. qc->n_elem - 1, lsg->length, qc->pad_len);
  3296. }
  3297. pre_n_elem = qc->n_elem;
  3298. if (trim_sg && pre_n_elem)
  3299. pre_n_elem--;
  3300. if (!pre_n_elem) {
  3301. n_elem = 0;
  3302. goto skip_map;
  3303. }
  3304. dir = qc->dma_dir;
  3305. n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
  3306. if (n_elem < 1) {
  3307. /* restore last sg */
  3308. lsg->length += qc->pad_len;
  3309. return -1;
  3310. }
  3311. DPRINTK("%d sg elements mapped\n", n_elem);
  3312. skip_map:
  3313. qc->n_elem = n_elem;
  3314. return 0;
  3315. }
  3316. /**
  3317. * swap_buf_le16 - swap halves of 16-bit words in place
  3318. * @buf: Buffer to swap
  3319. * @buf_words: Number of 16-bit words in buffer.
  3320. *
  3321. * Swap halves of 16-bit words if needed to convert from
  3322. * little-endian byte order to native cpu byte order, or
  3323. * vice-versa.
  3324. *
  3325. * LOCKING:
  3326. * Inherited from caller.
  3327. */
  3328. void swap_buf_le16(u16 *buf, unsigned int buf_words)
  3329. {
  3330. #ifdef __BIG_ENDIAN
  3331. unsigned int i;
  3332. for (i = 0; i < buf_words; i++)
  3333. buf[i] = le16_to_cpu(buf[i]);
  3334. #endif /* __BIG_ENDIAN */
  3335. }
  3336. /**
  3337. * ata_data_xfer - Transfer data by PIO
  3338. * @adev: device to target
  3339. * @buf: data buffer
  3340. * @buflen: buffer length
  3341. * @write_data: read/write
  3342. *
  3343. * Transfer data from/to the device data register by PIO.
  3344. *
  3345. * LOCKING:
  3346. * Inherited from caller.
  3347. */
  3348. void ata_data_xfer(struct ata_device *adev, unsigned char *buf,
  3349. unsigned int buflen, int write_data)
  3350. {
  3351. struct ata_port *ap = adev->ap;
  3352. unsigned int words = buflen >> 1;
  3353. /* Transfer multiple of 2 bytes */
  3354. if (write_data)
  3355. iowrite16_rep(ap->ioaddr.data_addr, buf, words);
  3356. else
  3357. ioread16_rep(ap->ioaddr.data_addr, buf, words);
  3358. /* Transfer trailing 1 byte, if any. */
  3359. if (unlikely(buflen & 0x01)) {
  3360. u16 align_buf[1] = { 0 };
  3361. unsigned char *trailing_buf = buf + buflen - 1;
  3362. if (write_data) {
  3363. memcpy(align_buf, trailing_buf, 1);
  3364. iowrite16(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
  3365. } else {
  3366. align_buf[0] = cpu_to_le16(ioread16(ap->ioaddr.data_addr));
  3367. memcpy(trailing_buf, align_buf, 1);
  3368. }
  3369. }
  3370. }
  3371. /**
  3372. * ata_data_xfer_noirq - Transfer data by PIO
  3373. * @adev: device to target
  3374. * @buf: data buffer
  3375. * @buflen: buffer length
  3376. * @write_data: read/write
  3377. *
  3378. * Transfer data from/to the device data register by PIO. Do the
  3379. * transfer with interrupts disabled.
  3380. *
  3381. * LOCKING:
  3382. * Inherited from caller.
  3383. */
  3384. void ata_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
  3385. unsigned int buflen, int write_data)
  3386. {
  3387. unsigned long flags;
  3388. local_irq_save(flags);
  3389. ata_data_xfer(adev, buf, buflen, write_data);
  3390. local_irq_restore(flags);
  3391. }
  3392. /**
  3393. * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
  3394. * @qc: Command on going
  3395. *
  3396. * Transfer ATA_SECT_SIZE of data from/to the ATA device.
  3397. *
  3398. * LOCKING:
  3399. * Inherited from caller.
  3400. */
  3401. static void ata_pio_sector(struct ata_queued_cmd *qc)
  3402. {
  3403. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  3404. struct scatterlist *sg = qc->__sg;
  3405. struct ata_port *ap = qc->ap;
  3406. struct page *page;
  3407. unsigned int offset;
  3408. unsigned char *buf;
  3409. if (qc->curbytes == qc->nbytes - ATA_SECT_SIZE)
  3410. ap->hsm_task_state = HSM_ST_LAST;
  3411. page = sg[qc->cursg].page;
  3412. offset = sg[qc->cursg].offset + qc->cursg_ofs;
  3413. /* get the current page and offset */
  3414. page = nth_page(page, (offset >> PAGE_SHIFT));
  3415. offset %= PAGE_SIZE;
  3416. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  3417. if (PageHighMem(page)) {
  3418. unsigned long flags;
  3419. /* FIXME: use a bounce buffer */
  3420. local_irq_save(flags);
  3421. buf = kmap_atomic(page, KM_IRQ0);
  3422. /* do the actual data transfer */
  3423. ap->ops->data_xfer(qc->dev, buf + offset, ATA_SECT_SIZE, do_write);
  3424. kunmap_atomic(buf, KM_IRQ0);
  3425. local_irq_restore(flags);
  3426. } else {
  3427. buf = page_address(page);
  3428. ap->ops->data_xfer(qc->dev, buf + offset, ATA_SECT_SIZE, do_write);
  3429. }
  3430. qc->curbytes += ATA_SECT_SIZE;
  3431. qc->cursg_ofs += ATA_SECT_SIZE;
  3432. if (qc->cursg_ofs == (&sg[qc->cursg])->length) {
  3433. qc->cursg++;
  3434. qc->cursg_ofs = 0;
  3435. }
  3436. }
  3437. /**
  3438. * ata_pio_sectors - Transfer one or many 512-byte sectors.
  3439. * @qc: Command on going
  3440. *
  3441. * Transfer one or many ATA_SECT_SIZE of data from/to the
  3442. * ATA device for the DRQ request.
  3443. *
  3444. * LOCKING:
  3445. * Inherited from caller.
  3446. */
  3447. static void ata_pio_sectors(struct ata_queued_cmd *qc)
  3448. {
  3449. if (is_multi_taskfile(&qc->tf)) {
  3450. /* READ/WRITE MULTIPLE */
  3451. unsigned int nsect;
  3452. WARN_ON(qc->dev->multi_count == 0);
  3453. nsect = min((qc->nbytes - qc->curbytes) / ATA_SECT_SIZE,
  3454. qc->dev->multi_count);
  3455. while (nsect--)
  3456. ata_pio_sector(qc);
  3457. } else
  3458. ata_pio_sector(qc);
  3459. }
  3460. /**
  3461. * atapi_send_cdb - Write CDB bytes to hardware
  3462. * @ap: Port to which ATAPI device is attached.
  3463. * @qc: Taskfile currently active
  3464. *
  3465. * When device has indicated its readiness to accept
  3466. * a CDB, this function is called. Send the CDB.
  3467. *
  3468. * LOCKING:
  3469. * caller.
  3470. */
  3471. static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
  3472. {
  3473. /* send SCSI cdb */
  3474. DPRINTK("send cdb\n");
  3475. WARN_ON(qc->dev->cdb_len < 12);
  3476. ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
  3477. ata_altstatus(ap); /* flush */
  3478. switch (qc->tf.protocol) {
  3479. case ATA_PROT_ATAPI:
  3480. ap->hsm_task_state = HSM_ST;
  3481. break;
  3482. case ATA_PROT_ATAPI_NODATA:
  3483. ap->hsm_task_state = HSM_ST_LAST;
  3484. break;
  3485. case ATA_PROT_ATAPI_DMA:
  3486. ap->hsm_task_state = HSM_ST_LAST;
  3487. /* initiate bmdma */
  3488. ap->ops->bmdma_start(qc);
  3489. break;
  3490. }
  3491. }
  3492. /**
  3493. * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
  3494. * @qc: Command on going
  3495. * @bytes: number of bytes
  3496. *
  3497. * Transfer Transfer data from/to the ATAPI device.
  3498. *
  3499. * LOCKING:
  3500. * Inherited from caller.
  3501. *
  3502. */
  3503. static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
  3504. {
  3505. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  3506. struct scatterlist *sg = qc->__sg;
  3507. struct ata_port *ap = qc->ap;
  3508. struct page *page;
  3509. unsigned char *buf;
  3510. unsigned int offset, count;
  3511. if (qc->curbytes + bytes >= qc->nbytes)
  3512. ap->hsm_task_state = HSM_ST_LAST;
  3513. next_sg:
  3514. if (unlikely(qc->cursg >= qc->n_elem)) {
  3515. /*
  3516. * The end of qc->sg is reached and the device expects
  3517. * more data to transfer. In order not to overrun qc->sg
  3518. * and fulfill length specified in the byte count register,
  3519. * - for read case, discard trailing data from the device
  3520. * - for write case, padding zero data to the device
  3521. */
  3522. u16 pad_buf[1] = { 0 };
  3523. unsigned int words = bytes >> 1;
  3524. unsigned int i;
  3525. if (words) /* warning if bytes > 1 */
  3526. ata_dev_printk(qc->dev, KERN_WARNING,
  3527. "%u bytes trailing data\n", bytes);
  3528. for (i = 0; i < words; i++)
  3529. ap->ops->data_xfer(qc->dev, (unsigned char*)pad_buf, 2, do_write);
  3530. ap->hsm_task_state = HSM_ST_LAST;
  3531. return;
  3532. }
  3533. sg = &qc->__sg[qc->cursg];
  3534. page = sg->page;
  3535. offset = sg->offset + qc->cursg_ofs;
  3536. /* get the current page and offset */
  3537. page = nth_page(page, (offset >> PAGE_SHIFT));
  3538. offset %= PAGE_SIZE;
  3539. /* don't overrun current sg */
  3540. count = min(sg->length - qc->cursg_ofs, bytes);
  3541. /* don't cross page boundaries */
  3542. count = min(count, (unsigned int)PAGE_SIZE - offset);
  3543. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  3544. if (PageHighMem(page)) {
  3545. unsigned long flags;
  3546. /* FIXME: use bounce buffer */
  3547. local_irq_save(flags);
  3548. buf = kmap_atomic(page, KM_IRQ0);
  3549. /* do the actual data transfer */
  3550. ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
  3551. kunmap_atomic(buf, KM_IRQ0);
  3552. local_irq_restore(flags);
  3553. } else {
  3554. buf = page_address(page);
  3555. ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
  3556. }
  3557. bytes -= count;
  3558. qc->curbytes += count;
  3559. qc->cursg_ofs += count;
  3560. if (qc->cursg_ofs == sg->length) {
  3561. qc->cursg++;
  3562. qc->cursg_ofs = 0;
  3563. }
  3564. if (bytes)
  3565. goto next_sg;
  3566. }
  3567. /**
  3568. * atapi_pio_bytes - Transfer data from/to the ATAPI device.
  3569. * @qc: Command on going
  3570. *
  3571. * Transfer Transfer data from/to the ATAPI device.
  3572. *
  3573. * LOCKING:
  3574. * Inherited from caller.
  3575. */
  3576. static void atapi_pio_bytes(struct ata_queued_cmd *qc)
  3577. {
  3578. struct ata_port *ap = qc->ap;
  3579. struct ata_device *dev = qc->dev;
  3580. unsigned int ireason, bc_lo, bc_hi, bytes;
  3581. int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
  3582. /* Abuse qc->result_tf for temp storage of intermediate TF
  3583. * here to save some kernel stack usage.
  3584. * For normal completion, qc->result_tf is not relevant. For
  3585. * error, qc->result_tf is later overwritten by ata_qc_complete().
  3586. * So, the correctness of qc->result_tf is not affected.
  3587. */
  3588. ap->ops->tf_read(ap, &qc->result_tf);
  3589. ireason = qc->result_tf.nsect;
  3590. bc_lo = qc->result_tf.lbam;
  3591. bc_hi = qc->result_tf.lbah;
  3592. bytes = (bc_hi << 8) | bc_lo;
  3593. /* shall be cleared to zero, indicating xfer of data */
  3594. if (ireason & (1 << 0))
  3595. goto err_out;
  3596. /* make sure transfer direction matches expected */
  3597. i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
  3598. if (do_write != i_write)
  3599. goto err_out;
  3600. VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
  3601. __atapi_pio_bytes(qc, bytes);
  3602. return;
  3603. err_out:
  3604. ata_dev_printk(dev, KERN_INFO, "ATAPI check failed\n");
  3605. qc->err_mask |= AC_ERR_HSM;
  3606. ap->hsm_task_state = HSM_ST_ERR;
  3607. }
  3608. /**
  3609. * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
  3610. * @ap: the target ata_port
  3611. * @qc: qc on going
  3612. *
  3613. * RETURNS:
  3614. * 1 if ok in workqueue, 0 otherwise.
  3615. */
  3616. static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
  3617. {
  3618. if (qc->tf.flags & ATA_TFLAG_POLLING)
  3619. return 1;
  3620. if (ap->hsm_task_state == HSM_ST_FIRST) {
  3621. if (qc->tf.protocol == ATA_PROT_PIO &&
  3622. (qc->tf.flags & ATA_TFLAG_WRITE))
  3623. return 1;
  3624. if (is_atapi_taskfile(&qc->tf) &&
  3625. !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  3626. return 1;
  3627. }
  3628. return 0;
  3629. }
  3630. /**
  3631. * ata_hsm_qc_complete - finish a qc running on standard HSM
  3632. * @qc: Command to complete
  3633. * @in_wq: 1 if called from workqueue, 0 otherwise
  3634. *
  3635. * Finish @qc which is running on standard HSM.
  3636. *
  3637. * LOCKING:
  3638. * If @in_wq is zero, spin_lock_irqsave(host lock).
  3639. * Otherwise, none on entry and grabs host lock.
  3640. */
  3641. static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
  3642. {
  3643. struct ata_port *ap = qc->ap;
  3644. unsigned long flags;
  3645. if (ap->ops->error_handler) {
  3646. if (in_wq) {
  3647. spin_lock_irqsave(ap->lock, flags);
  3648. /* EH might have kicked in while host lock is
  3649. * released.
  3650. */
  3651. qc = ata_qc_from_tag(ap, qc->tag);
  3652. if (qc) {
  3653. if (likely(!(qc->err_mask & AC_ERR_HSM))) {
  3654. ap->ops->irq_on(ap);
  3655. ata_qc_complete(qc);
  3656. } else
  3657. ata_port_freeze(ap);
  3658. }
  3659. spin_unlock_irqrestore(ap->lock, flags);
  3660. } else {
  3661. if (likely(!(qc->err_mask & AC_ERR_HSM)))
  3662. ata_qc_complete(qc);
  3663. else
  3664. ata_port_freeze(ap);
  3665. }
  3666. } else {
  3667. if (in_wq) {
  3668. spin_lock_irqsave(ap->lock, flags);
  3669. ap->ops->irq_on(ap);
  3670. ata_qc_complete(qc);
  3671. spin_unlock_irqrestore(ap->lock, flags);
  3672. } else
  3673. ata_qc_complete(qc);
  3674. }
  3675. ata_altstatus(ap); /* flush */
  3676. }
  3677. /**
  3678. * ata_hsm_move - move the HSM to the next state.
  3679. * @ap: the target ata_port
  3680. * @qc: qc on going
  3681. * @status: current device status
  3682. * @in_wq: 1 if called from workqueue, 0 otherwise
  3683. *
  3684. * RETURNS:
  3685. * 1 when poll next status needed, 0 otherwise.
  3686. */
  3687. int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
  3688. u8 status, int in_wq)
  3689. {
  3690. unsigned long flags = 0;
  3691. int poll_next;
  3692. WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
  3693. /* Make sure ata_qc_issue_prot() does not throw things
  3694. * like DMA polling into the workqueue. Notice that
  3695. * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
  3696. */
  3697. WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
  3698. fsm_start:
  3699. DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
  3700. ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
  3701. switch (ap->hsm_task_state) {
  3702. case HSM_ST_FIRST:
  3703. /* Send first data block or PACKET CDB */
  3704. /* If polling, we will stay in the work queue after
  3705. * sending the data. Otherwise, interrupt handler
  3706. * takes over after sending the data.
  3707. */
  3708. poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
  3709. /* check device status */
  3710. if (unlikely((status & ATA_DRQ) == 0)) {
  3711. /* handle BSY=0, DRQ=0 as error */
  3712. if (likely(status & (ATA_ERR | ATA_DF)))
  3713. /* device stops HSM for abort/error */
  3714. qc->err_mask |= AC_ERR_DEV;
  3715. else
  3716. /* HSM violation. Let EH handle this */
  3717. qc->err_mask |= AC_ERR_HSM;
  3718. ap->hsm_task_state = HSM_ST_ERR;
  3719. goto fsm_start;
  3720. }
  3721. /* Device should not ask for data transfer (DRQ=1)
  3722. * when it finds something wrong.
  3723. * We ignore DRQ here and stop the HSM by
  3724. * changing hsm_task_state to HSM_ST_ERR and
  3725. * let the EH abort the command or reset the device.
  3726. */
  3727. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  3728. ata_port_printk(ap, KERN_WARNING, "DRQ=1 with device "
  3729. "error, dev_stat 0x%X\n", status);
  3730. qc->err_mask |= AC_ERR_HSM;
  3731. ap->hsm_task_state = HSM_ST_ERR;
  3732. goto fsm_start;
  3733. }
  3734. /* Send the CDB (atapi) or the first data block (ata pio out).
  3735. * During the state transition, interrupt handler shouldn't
  3736. * be invoked before the data transfer is complete and
  3737. * hsm_task_state is changed. Hence, the following locking.
  3738. */
  3739. if (in_wq)
  3740. spin_lock_irqsave(ap->lock, flags);
  3741. if (qc->tf.protocol == ATA_PROT_PIO) {
  3742. /* PIO data out protocol.
  3743. * send first data block.
  3744. */
  3745. /* ata_pio_sectors() might change the state
  3746. * to HSM_ST_LAST. so, the state is changed here
  3747. * before ata_pio_sectors().
  3748. */
  3749. ap->hsm_task_state = HSM_ST;
  3750. ata_pio_sectors(qc);
  3751. ata_altstatus(ap); /* flush */
  3752. } else
  3753. /* send CDB */
  3754. atapi_send_cdb(ap, qc);
  3755. if (in_wq)
  3756. spin_unlock_irqrestore(ap->lock, flags);
  3757. /* if polling, ata_pio_task() handles the rest.
  3758. * otherwise, interrupt handler takes over from here.
  3759. */
  3760. break;
  3761. case HSM_ST:
  3762. /* complete command or read/write the data register */
  3763. if (qc->tf.protocol == ATA_PROT_ATAPI) {
  3764. /* ATAPI PIO protocol */
  3765. if ((status & ATA_DRQ) == 0) {
  3766. /* No more data to transfer or device error.
  3767. * Device error will be tagged in HSM_ST_LAST.
  3768. */
  3769. ap->hsm_task_state = HSM_ST_LAST;
  3770. goto fsm_start;
  3771. }
  3772. /* Device should not ask for data transfer (DRQ=1)
  3773. * when it finds something wrong.
  3774. * We ignore DRQ here and stop the HSM by
  3775. * changing hsm_task_state to HSM_ST_ERR and
  3776. * let the EH abort the command or reset the device.
  3777. */
  3778. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  3779. ata_port_printk(ap, KERN_WARNING, "DRQ=1 with "
  3780. "device error, dev_stat 0x%X\n",
  3781. status);
  3782. qc->err_mask |= AC_ERR_HSM;
  3783. ap->hsm_task_state = HSM_ST_ERR;
  3784. goto fsm_start;
  3785. }
  3786. atapi_pio_bytes(qc);
  3787. if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
  3788. /* bad ireason reported by device */
  3789. goto fsm_start;
  3790. } else {
  3791. /* ATA PIO protocol */
  3792. if (unlikely((status & ATA_DRQ) == 0)) {
  3793. /* handle BSY=0, DRQ=0 as error */
  3794. if (likely(status & (ATA_ERR | ATA_DF)))
  3795. /* device stops HSM for abort/error */
  3796. qc->err_mask |= AC_ERR_DEV;
  3797. else
  3798. /* HSM violation. Let EH handle this.
  3799. * Phantom devices also trigger this
  3800. * condition. Mark hint.
  3801. */
  3802. qc->err_mask |= AC_ERR_HSM |
  3803. AC_ERR_NODEV_HINT;
  3804. ap->hsm_task_state = HSM_ST_ERR;
  3805. goto fsm_start;
  3806. }
  3807. /* For PIO reads, some devices may ask for
  3808. * data transfer (DRQ=1) alone with ERR=1.
  3809. * We respect DRQ here and transfer one
  3810. * block of junk data before changing the
  3811. * hsm_task_state to HSM_ST_ERR.
  3812. *
  3813. * For PIO writes, ERR=1 DRQ=1 doesn't make
  3814. * sense since the data block has been
  3815. * transferred to the device.
  3816. */
  3817. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  3818. /* data might be corrputed */
  3819. qc->err_mask |= AC_ERR_DEV;
  3820. if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
  3821. ata_pio_sectors(qc);
  3822. ata_altstatus(ap);
  3823. status = ata_wait_idle(ap);
  3824. }
  3825. if (status & (ATA_BUSY | ATA_DRQ))
  3826. qc->err_mask |= AC_ERR_HSM;
  3827. /* ata_pio_sectors() might change the
  3828. * state to HSM_ST_LAST. so, the state
  3829. * is changed after ata_pio_sectors().
  3830. */
  3831. ap->hsm_task_state = HSM_ST_ERR;
  3832. goto fsm_start;
  3833. }
  3834. ata_pio_sectors(qc);
  3835. if (ap->hsm_task_state == HSM_ST_LAST &&
  3836. (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
  3837. /* all data read */
  3838. ata_altstatus(ap);
  3839. status = ata_wait_idle(ap);
  3840. goto fsm_start;
  3841. }
  3842. }
  3843. ata_altstatus(ap); /* flush */
  3844. poll_next = 1;
  3845. break;
  3846. case HSM_ST_LAST:
  3847. if (unlikely(!ata_ok(status))) {
  3848. qc->err_mask |= __ac_err_mask(status);
  3849. ap->hsm_task_state = HSM_ST_ERR;
  3850. goto fsm_start;
  3851. }
  3852. /* no more data to transfer */
  3853. DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
  3854. ap->print_id, qc->dev->devno, status);
  3855. WARN_ON(qc->err_mask);
  3856. ap->hsm_task_state = HSM_ST_IDLE;
  3857. /* complete taskfile transaction */
  3858. ata_hsm_qc_complete(qc, in_wq);
  3859. poll_next = 0;
  3860. break;
  3861. case HSM_ST_ERR:
  3862. /* make sure qc->err_mask is available to
  3863. * know what's wrong and recover
  3864. */
  3865. WARN_ON(qc->err_mask == 0);
  3866. ap->hsm_task_state = HSM_ST_IDLE;
  3867. /* complete taskfile transaction */
  3868. ata_hsm_qc_complete(qc, in_wq);
  3869. poll_next = 0;
  3870. break;
  3871. default:
  3872. poll_next = 0;
  3873. BUG();
  3874. }
  3875. return poll_next;
  3876. }
  3877. static void ata_pio_task(struct work_struct *work)
  3878. {
  3879. struct ata_port *ap =
  3880. container_of(work, struct ata_port, port_task.work);
  3881. struct ata_queued_cmd *qc = ap->port_task_data;
  3882. u8 status;
  3883. int poll_next;
  3884. fsm_start:
  3885. WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
  3886. /*
  3887. * This is purely heuristic. This is a fast path.
  3888. * Sometimes when we enter, BSY will be cleared in
  3889. * a chk-status or two. If not, the drive is probably seeking
  3890. * or something. Snooze for a couple msecs, then
  3891. * chk-status again. If still busy, queue delayed work.
  3892. */
  3893. status = ata_busy_wait(ap, ATA_BUSY, 5);
  3894. if (status & ATA_BUSY) {
  3895. msleep(2);
  3896. status = ata_busy_wait(ap, ATA_BUSY, 10);
  3897. if (status & ATA_BUSY) {
  3898. ata_port_queue_task(ap, ata_pio_task, qc, ATA_SHORT_PAUSE);
  3899. return;
  3900. }
  3901. }
  3902. /* move the HSM */
  3903. poll_next = ata_hsm_move(ap, qc, status, 1);
  3904. /* another command or interrupt handler
  3905. * may be running at this point.
  3906. */
  3907. if (poll_next)
  3908. goto fsm_start;
  3909. }
  3910. /**
  3911. * ata_qc_new - Request an available ATA command, for queueing
  3912. * @ap: Port associated with device @dev
  3913. * @dev: Device from whom we request an available command structure
  3914. *
  3915. * LOCKING:
  3916. * None.
  3917. */
  3918. static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
  3919. {
  3920. struct ata_queued_cmd *qc = NULL;
  3921. unsigned int i;
  3922. /* no command while frozen */
  3923. if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
  3924. return NULL;
  3925. /* the last tag is reserved for internal command. */
  3926. for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
  3927. if (!test_and_set_bit(i, &ap->qc_allocated)) {
  3928. qc = __ata_qc_from_tag(ap, i);
  3929. break;
  3930. }
  3931. if (qc)
  3932. qc->tag = i;
  3933. return qc;
  3934. }
  3935. /**
  3936. * ata_qc_new_init - Request an available ATA command, and initialize it
  3937. * @dev: Device from whom we request an available command structure
  3938. *
  3939. * LOCKING:
  3940. * None.
  3941. */
  3942. struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
  3943. {
  3944. struct ata_port *ap = dev->ap;
  3945. struct ata_queued_cmd *qc;
  3946. qc = ata_qc_new(ap);
  3947. if (qc) {
  3948. qc->scsicmd = NULL;
  3949. qc->ap = ap;
  3950. qc->dev = dev;
  3951. ata_qc_reinit(qc);
  3952. }
  3953. return qc;
  3954. }
  3955. /**
  3956. * ata_qc_free - free unused ata_queued_cmd
  3957. * @qc: Command to complete
  3958. *
  3959. * Designed to free unused ata_queued_cmd object
  3960. * in case something prevents using it.
  3961. *
  3962. * LOCKING:
  3963. * spin_lock_irqsave(host lock)
  3964. */
  3965. void ata_qc_free(struct ata_queued_cmd *qc)
  3966. {
  3967. struct ata_port *ap = qc->ap;
  3968. unsigned int tag;
  3969. WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  3970. qc->flags = 0;
  3971. tag = qc->tag;
  3972. if (likely(ata_tag_valid(tag))) {
  3973. qc->tag = ATA_TAG_POISON;
  3974. clear_bit(tag, &ap->qc_allocated);
  3975. }
  3976. }
  3977. void __ata_qc_complete(struct ata_queued_cmd *qc)
  3978. {
  3979. struct ata_port *ap = qc->ap;
  3980. WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  3981. WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
  3982. if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
  3983. ata_sg_clean(qc);
  3984. /* command should be marked inactive atomically with qc completion */
  3985. if (qc->tf.protocol == ATA_PROT_NCQ)
  3986. ap->sactive &= ~(1 << qc->tag);
  3987. else
  3988. ap->active_tag = ATA_TAG_POISON;
  3989. /* atapi: mark qc as inactive to prevent the interrupt handler
  3990. * from completing the command twice later, before the error handler
  3991. * is called. (when rc != 0 and atapi request sense is needed)
  3992. */
  3993. qc->flags &= ~ATA_QCFLAG_ACTIVE;
  3994. ap->qc_active &= ~(1 << qc->tag);
  3995. /* call completion callback */
  3996. qc->complete_fn(qc);
  3997. }
  3998. static void fill_result_tf(struct ata_queued_cmd *qc)
  3999. {
  4000. struct ata_port *ap = qc->ap;
  4001. ap->ops->tf_read(ap, &qc->result_tf);
  4002. qc->result_tf.flags = qc->tf.flags;
  4003. }
  4004. /**
  4005. * ata_qc_complete - Complete an active ATA command
  4006. * @qc: Command to complete
  4007. * @err_mask: ATA Status register contents
  4008. *
  4009. * Indicate to the mid and upper layers that an ATA
  4010. * command has completed, with either an ok or not-ok status.
  4011. *
  4012. * LOCKING:
  4013. * spin_lock_irqsave(host lock)
  4014. */
  4015. void ata_qc_complete(struct ata_queued_cmd *qc)
  4016. {
  4017. struct ata_port *ap = qc->ap;
  4018. /* XXX: New EH and old EH use different mechanisms to
  4019. * synchronize EH with regular execution path.
  4020. *
  4021. * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
  4022. * Normal execution path is responsible for not accessing a
  4023. * failed qc. libata core enforces the rule by returning NULL
  4024. * from ata_qc_from_tag() for failed qcs.
  4025. *
  4026. * Old EH depends on ata_qc_complete() nullifying completion
  4027. * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
  4028. * not synchronize with interrupt handler. Only PIO task is
  4029. * taken care of.
  4030. */
  4031. if (ap->ops->error_handler) {
  4032. WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
  4033. if (unlikely(qc->err_mask))
  4034. qc->flags |= ATA_QCFLAG_FAILED;
  4035. if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
  4036. if (!ata_tag_internal(qc->tag)) {
  4037. /* always fill result TF for failed qc */
  4038. fill_result_tf(qc);
  4039. ata_qc_schedule_eh(qc);
  4040. return;
  4041. }
  4042. }
  4043. /* read result TF if requested */
  4044. if (qc->flags & ATA_QCFLAG_RESULT_TF)
  4045. fill_result_tf(qc);
  4046. __ata_qc_complete(qc);
  4047. } else {
  4048. if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
  4049. return;
  4050. /* read result TF if failed or requested */
  4051. if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
  4052. fill_result_tf(qc);
  4053. __ata_qc_complete(qc);
  4054. }
  4055. }
  4056. /**
  4057. * ata_qc_complete_multiple - Complete multiple qcs successfully
  4058. * @ap: port in question
  4059. * @qc_active: new qc_active mask
  4060. * @finish_qc: LLDD callback invoked before completing a qc
  4061. *
  4062. * Complete in-flight commands. This functions is meant to be
  4063. * called from low-level driver's interrupt routine to complete
  4064. * requests normally. ap->qc_active and @qc_active is compared
  4065. * and commands are completed accordingly.
  4066. *
  4067. * LOCKING:
  4068. * spin_lock_irqsave(host lock)
  4069. *
  4070. * RETURNS:
  4071. * Number of completed commands on success, -errno otherwise.
  4072. */
  4073. int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
  4074. void (*finish_qc)(struct ata_queued_cmd *))
  4075. {
  4076. int nr_done = 0;
  4077. u32 done_mask;
  4078. int i;
  4079. done_mask = ap->qc_active ^ qc_active;
  4080. if (unlikely(done_mask & qc_active)) {
  4081. ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
  4082. "(%08x->%08x)\n", ap->qc_active, qc_active);
  4083. return -EINVAL;
  4084. }
  4085. for (i = 0; i < ATA_MAX_QUEUE; i++) {
  4086. struct ata_queued_cmd *qc;
  4087. if (!(done_mask & (1 << i)))
  4088. continue;
  4089. if ((qc = ata_qc_from_tag(ap, i))) {
  4090. if (finish_qc)
  4091. finish_qc(qc);
  4092. ata_qc_complete(qc);
  4093. nr_done++;
  4094. }
  4095. }
  4096. return nr_done;
  4097. }
  4098. static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
  4099. {
  4100. struct ata_port *ap = qc->ap;
  4101. switch (qc->tf.protocol) {
  4102. case ATA_PROT_NCQ:
  4103. case ATA_PROT_DMA:
  4104. case ATA_PROT_ATAPI_DMA:
  4105. return 1;
  4106. case ATA_PROT_ATAPI:
  4107. case ATA_PROT_PIO:
  4108. if (ap->flags & ATA_FLAG_PIO_DMA)
  4109. return 1;
  4110. /* fall through */
  4111. default:
  4112. return 0;
  4113. }
  4114. /* never reached */
  4115. }
  4116. /**
  4117. * ata_qc_issue - issue taskfile to device
  4118. * @qc: command to issue to device
  4119. *
  4120. * Prepare an ATA command to submission to device.
  4121. * This includes mapping the data into a DMA-able
  4122. * area, filling in the S/G table, and finally
  4123. * writing the taskfile to hardware, starting the command.
  4124. *
  4125. * LOCKING:
  4126. * spin_lock_irqsave(host lock)
  4127. */
  4128. void ata_qc_issue(struct ata_queued_cmd *qc)
  4129. {
  4130. struct ata_port *ap = qc->ap;
  4131. /* Make sure only one non-NCQ command is outstanding. The
  4132. * check is skipped for old EH because it reuses active qc to
  4133. * request ATAPI sense.
  4134. */
  4135. WARN_ON(ap->ops->error_handler && ata_tag_valid(ap->active_tag));
  4136. if (qc->tf.protocol == ATA_PROT_NCQ) {
  4137. WARN_ON(ap->sactive & (1 << qc->tag));
  4138. ap->sactive |= 1 << qc->tag;
  4139. } else {
  4140. WARN_ON(ap->sactive);
  4141. ap->active_tag = qc->tag;
  4142. }
  4143. qc->flags |= ATA_QCFLAG_ACTIVE;
  4144. ap->qc_active |= 1 << qc->tag;
  4145. if (ata_should_dma_map(qc)) {
  4146. if (qc->flags & ATA_QCFLAG_SG) {
  4147. if (ata_sg_setup(qc))
  4148. goto sg_err;
  4149. } else if (qc->flags & ATA_QCFLAG_SINGLE) {
  4150. if (ata_sg_setup_one(qc))
  4151. goto sg_err;
  4152. }
  4153. } else {
  4154. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  4155. }
  4156. ap->ops->qc_prep(qc);
  4157. qc->err_mask |= ap->ops->qc_issue(qc);
  4158. if (unlikely(qc->err_mask))
  4159. goto err;
  4160. return;
  4161. sg_err:
  4162. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  4163. qc->err_mask |= AC_ERR_SYSTEM;
  4164. err:
  4165. ata_qc_complete(qc);
  4166. }
  4167. /**
  4168. * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
  4169. * @qc: command to issue to device
  4170. *
  4171. * Using various libata functions and hooks, this function
  4172. * starts an ATA command. ATA commands are grouped into
  4173. * classes called "protocols", and issuing each type of protocol
  4174. * is slightly different.
  4175. *
  4176. * May be used as the qc_issue() entry in ata_port_operations.
  4177. *
  4178. * LOCKING:
  4179. * spin_lock_irqsave(host lock)
  4180. *
  4181. * RETURNS:
  4182. * Zero on success, AC_ERR_* mask on failure
  4183. */
  4184. unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
  4185. {
  4186. struct ata_port *ap = qc->ap;
  4187. /* Use polling pio if the LLD doesn't handle
  4188. * interrupt driven pio and atapi CDB interrupt.
  4189. */
  4190. if (ap->flags & ATA_FLAG_PIO_POLLING) {
  4191. switch (qc->tf.protocol) {
  4192. case ATA_PROT_PIO:
  4193. case ATA_PROT_NODATA:
  4194. case ATA_PROT_ATAPI:
  4195. case ATA_PROT_ATAPI_NODATA:
  4196. qc->tf.flags |= ATA_TFLAG_POLLING;
  4197. break;
  4198. case ATA_PROT_ATAPI_DMA:
  4199. if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
  4200. /* see ata_dma_blacklisted() */
  4201. BUG();
  4202. break;
  4203. default:
  4204. break;
  4205. }
  4206. }
  4207. /* Some controllers show flaky interrupt behavior after
  4208. * setting xfer mode. Use polling instead.
  4209. */
  4210. if (unlikely(qc->tf.command == ATA_CMD_SET_FEATURES &&
  4211. qc->tf.feature == SETFEATURES_XFER) &&
  4212. (ap->flags & ATA_FLAG_SETXFER_POLLING))
  4213. qc->tf.flags |= ATA_TFLAG_POLLING;
  4214. /* select the device */
  4215. ata_dev_select(ap, qc->dev->devno, 1, 0);
  4216. /* start the command */
  4217. switch (qc->tf.protocol) {
  4218. case ATA_PROT_NODATA:
  4219. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4220. ata_qc_set_polling(qc);
  4221. ata_tf_to_host(ap, &qc->tf);
  4222. ap->hsm_task_state = HSM_ST_LAST;
  4223. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4224. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4225. break;
  4226. case ATA_PROT_DMA:
  4227. WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
  4228. ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
  4229. ap->ops->bmdma_setup(qc); /* set up bmdma */
  4230. ap->ops->bmdma_start(qc); /* initiate bmdma */
  4231. ap->hsm_task_state = HSM_ST_LAST;
  4232. break;
  4233. case ATA_PROT_PIO:
  4234. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4235. ata_qc_set_polling(qc);
  4236. ata_tf_to_host(ap, &qc->tf);
  4237. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  4238. /* PIO data out protocol */
  4239. ap->hsm_task_state = HSM_ST_FIRST;
  4240. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4241. /* always send first data block using
  4242. * the ata_pio_task() codepath.
  4243. */
  4244. } else {
  4245. /* PIO data in protocol */
  4246. ap->hsm_task_state = HSM_ST;
  4247. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4248. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4249. /* if polling, ata_pio_task() handles the rest.
  4250. * otherwise, interrupt handler takes over from here.
  4251. */
  4252. }
  4253. break;
  4254. case ATA_PROT_ATAPI:
  4255. case ATA_PROT_ATAPI_NODATA:
  4256. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4257. ata_qc_set_polling(qc);
  4258. ata_tf_to_host(ap, &qc->tf);
  4259. ap->hsm_task_state = HSM_ST_FIRST;
  4260. /* send cdb by polling if no cdb interrupt */
  4261. if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
  4262. (qc->tf.flags & ATA_TFLAG_POLLING))
  4263. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4264. break;
  4265. case ATA_PROT_ATAPI_DMA:
  4266. WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
  4267. ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
  4268. ap->ops->bmdma_setup(qc); /* set up bmdma */
  4269. ap->hsm_task_state = HSM_ST_FIRST;
  4270. /* send cdb by polling if no cdb interrupt */
  4271. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  4272. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4273. break;
  4274. default:
  4275. WARN_ON(1);
  4276. return AC_ERR_SYSTEM;
  4277. }
  4278. return 0;
  4279. }
  4280. /**
  4281. * ata_host_intr - Handle host interrupt for given (port, task)
  4282. * @ap: Port on which interrupt arrived (possibly...)
  4283. * @qc: Taskfile currently active in engine
  4284. *
  4285. * Handle host interrupt for given queued command. Currently,
  4286. * only DMA interrupts are handled. All other commands are
  4287. * handled via polling with interrupts disabled (nIEN bit).
  4288. *
  4289. * LOCKING:
  4290. * spin_lock_irqsave(host lock)
  4291. *
  4292. * RETURNS:
  4293. * One if interrupt was handled, zero if not (shared irq).
  4294. */
  4295. inline unsigned int ata_host_intr (struct ata_port *ap,
  4296. struct ata_queued_cmd *qc)
  4297. {
  4298. struct ata_eh_info *ehi = &ap->eh_info;
  4299. u8 status, host_stat = 0;
  4300. VPRINTK("ata%u: protocol %d task_state %d\n",
  4301. ap->print_id, qc->tf.protocol, ap->hsm_task_state);
  4302. /* Check whether we are expecting interrupt in this state */
  4303. switch (ap->hsm_task_state) {
  4304. case HSM_ST_FIRST:
  4305. /* Some pre-ATAPI-4 devices assert INTRQ
  4306. * at this state when ready to receive CDB.
  4307. */
  4308. /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
  4309. * The flag was turned on only for atapi devices.
  4310. * No need to check is_atapi_taskfile(&qc->tf) again.
  4311. */
  4312. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  4313. goto idle_irq;
  4314. break;
  4315. case HSM_ST_LAST:
  4316. if (qc->tf.protocol == ATA_PROT_DMA ||
  4317. qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
  4318. /* check status of DMA engine */
  4319. host_stat = ap->ops->bmdma_status(ap);
  4320. VPRINTK("ata%u: host_stat 0x%X\n",
  4321. ap->print_id, host_stat);
  4322. /* if it's not our irq... */
  4323. if (!(host_stat & ATA_DMA_INTR))
  4324. goto idle_irq;
  4325. /* before we do anything else, clear DMA-Start bit */
  4326. ap->ops->bmdma_stop(qc);
  4327. if (unlikely(host_stat & ATA_DMA_ERR)) {
  4328. /* error when transfering data to/from memory */
  4329. qc->err_mask |= AC_ERR_HOST_BUS;
  4330. ap->hsm_task_state = HSM_ST_ERR;
  4331. }
  4332. }
  4333. break;
  4334. case HSM_ST:
  4335. break;
  4336. default:
  4337. goto idle_irq;
  4338. }
  4339. /* check altstatus */
  4340. status = ata_altstatus(ap);
  4341. if (status & ATA_BUSY)
  4342. goto idle_irq;
  4343. /* check main status, clearing INTRQ */
  4344. status = ata_chk_status(ap);
  4345. if (unlikely(status & ATA_BUSY))
  4346. goto idle_irq;
  4347. /* ack bmdma irq events */
  4348. ap->ops->irq_clear(ap);
  4349. ata_hsm_move(ap, qc, status, 0);
  4350. if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
  4351. qc->tf.protocol == ATA_PROT_ATAPI_DMA))
  4352. ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
  4353. return 1; /* irq handled */
  4354. idle_irq:
  4355. ap->stats.idle_irq++;
  4356. #ifdef ATA_IRQ_TRAP
  4357. if ((ap->stats.idle_irq % 1000) == 0) {
  4358. ap->ops->irq_ack(ap, 0); /* debug trap */
  4359. ata_port_printk(ap, KERN_WARNING, "irq trap\n");
  4360. return 1;
  4361. }
  4362. #endif
  4363. return 0; /* irq not handled */
  4364. }
  4365. /**
  4366. * ata_interrupt - Default ATA host interrupt handler
  4367. * @irq: irq line (unused)
  4368. * @dev_instance: pointer to our ata_host information structure
  4369. *
  4370. * Default interrupt handler for PCI IDE devices. Calls
  4371. * ata_host_intr() for each port that is not disabled.
  4372. *
  4373. * LOCKING:
  4374. * Obtains host lock during operation.
  4375. *
  4376. * RETURNS:
  4377. * IRQ_NONE or IRQ_HANDLED.
  4378. */
  4379. irqreturn_t ata_interrupt (int irq, void *dev_instance)
  4380. {
  4381. struct ata_host *host = dev_instance;
  4382. unsigned int i;
  4383. unsigned int handled = 0;
  4384. unsigned long flags;
  4385. /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
  4386. spin_lock_irqsave(&host->lock, flags);
  4387. for (i = 0; i < host->n_ports; i++) {
  4388. struct ata_port *ap;
  4389. ap = host->ports[i];
  4390. if (ap &&
  4391. !(ap->flags & ATA_FLAG_DISABLED)) {
  4392. struct ata_queued_cmd *qc;
  4393. qc = ata_qc_from_tag(ap, ap->active_tag);
  4394. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
  4395. (qc->flags & ATA_QCFLAG_ACTIVE))
  4396. handled |= ata_host_intr(ap, qc);
  4397. }
  4398. }
  4399. spin_unlock_irqrestore(&host->lock, flags);
  4400. return IRQ_RETVAL(handled);
  4401. }
  4402. /**
  4403. * sata_scr_valid - test whether SCRs are accessible
  4404. * @ap: ATA port to test SCR accessibility for
  4405. *
  4406. * Test whether SCRs are accessible for @ap.
  4407. *
  4408. * LOCKING:
  4409. * None.
  4410. *
  4411. * RETURNS:
  4412. * 1 if SCRs are accessible, 0 otherwise.
  4413. */
  4414. int sata_scr_valid(struct ata_port *ap)
  4415. {
  4416. return ap->cbl == ATA_CBL_SATA && ap->ops->scr_read;
  4417. }
  4418. /**
  4419. * sata_scr_read - read SCR register of the specified port
  4420. * @ap: ATA port to read SCR for
  4421. * @reg: SCR to read
  4422. * @val: Place to store read value
  4423. *
  4424. * Read SCR register @reg of @ap into *@val. This function is
  4425. * guaranteed to succeed if the cable type of the port is SATA
  4426. * and the port implements ->scr_read.
  4427. *
  4428. * LOCKING:
  4429. * None.
  4430. *
  4431. * RETURNS:
  4432. * 0 on success, negative errno on failure.
  4433. */
  4434. int sata_scr_read(struct ata_port *ap, int reg, u32 *val)
  4435. {
  4436. if (sata_scr_valid(ap)) {
  4437. *val = ap->ops->scr_read(ap, reg);
  4438. return 0;
  4439. }
  4440. return -EOPNOTSUPP;
  4441. }
  4442. /**
  4443. * sata_scr_write - write SCR register of the specified port
  4444. * @ap: ATA port to write SCR for
  4445. * @reg: SCR to write
  4446. * @val: value to write
  4447. *
  4448. * Write @val to SCR register @reg of @ap. This function is
  4449. * guaranteed to succeed if the cable type of the port is SATA
  4450. * and the port implements ->scr_read.
  4451. *
  4452. * LOCKING:
  4453. * None.
  4454. *
  4455. * RETURNS:
  4456. * 0 on success, negative errno on failure.
  4457. */
  4458. int sata_scr_write(struct ata_port *ap, int reg, u32 val)
  4459. {
  4460. if (sata_scr_valid(ap)) {
  4461. ap->ops->scr_write(ap, reg, val);
  4462. return 0;
  4463. }
  4464. return -EOPNOTSUPP;
  4465. }
  4466. /**
  4467. * sata_scr_write_flush - write SCR register of the specified port and flush
  4468. * @ap: ATA port to write SCR for
  4469. * @reg: SCR to write
  4470. * @val: value to write
  4471. *
  4472. * This function is identical to sata_scr_write() except that this
  4473. * function performs flush after writing to the register.
  4474. *
  4475. * LOCKING:
  4476. * None.
  4477. *
  4478. * RETURNS:
  4479. * 0 on success, negative errno on failure.
  4480. */
  4481. int sata_scr_write_flush(struct ata_port *ap, int reg, u32 val)
  4482. {
  4483. if (sata_scr_valid(ap)) {
  4484. ap->ops->scr_write(ap, reg, val);
  4485. ap->ops->scr_read(ap, reg);
  4486. return 0;
  4487. }
  4488. return -EOPNOTSUPP;
  4489. }
  4490. /**
  4491. * ata_port_online - test whether the given port is online
  4492. * @ap: ATA port to test
  4493. *
  4494. * Test whether @ap is online. Note that this function returns 0
  4495. * if online status of @ap cannot be obtained, so
  4496. * ata_port_online(ap) != !ata_port_offline(ap).
  4497. *
  4498. * LOCKING:
  4499. * None.
  4500. *
  4501. * RETURNS:
  4502. * 1 if the port online status is available and online.
  4503. */
  4504. int ata_port_online(struct ata_port *ap)
  4505. {
  4506. u32 sstatus;
  4507. if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) == 0x3)
  4508. return 1;
  4509. return 0;
  4510. }
  4511. /**
  4512. * ata_port_offline - test whether the given port is offline
  4513. * @ap: ATA port to test
  4514. *
  4515. * Test whether @ap is offline. Note that this function returns
  4516. * 0 if offline status of @ap cannot be obtained, so
  4517. * ata_port_online(ap) != !ata_port_offline(ap).
  4518. *
  4519. * LOCKING:
  4520. * None.
  4521. *
  4522. * RETURNS:
  4523. * 1 if the port offline status is available and offline.
  4524. */
  4525. int ata_port_offline(struct ata_port *ap)
  4526. {
  4527. u32 sstatus;
  4528. if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) != 0x3)
  4529. return 1;
  4530. return 0;
  4531. }
  4532. int ata_flush_cache(struct ata_device *dev)
  4533. {
  4534. unsigned int err_mask;
  4535. u8 cmd;
  4536. if (!ata_try_flush_cache(dev))
  4537. return 0;
  4538. if (dev->flags & ATA_DFLAG_FLUSH_EXT)
  4539. cmd = ATA_CMD_FLUSH_EXT;
  4540. else
  4541. cmd = ATA_CMD_FLUSH;
  4542. err_mask = ata_do_simple_cmd(dev, cmd);
  4543. if (err_mask) {
  4544. ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
  4545. return -EIO;
  4546. }
  4547. return 0;
  4548. }
  4549. #ifdef CONFIG_PM
  4550. static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
  4551. unsigned int action, unsigned int ehi_flags,
  4552. int wait)
  4553. {
  4554. unsigned long flags;
  4555. int i, rc;
  4556. for (i = 0; i < host->n_ports; i++) {
  4557. struct ata_port *ap = host->ports[i];
  4558. /* Previous resume operation might still be in
  4559. * progress. Wait for PM_PENDING to clear.
  4560. */
  4561. if (ap->pflags & ATA_PFLAG_PM_PENDING) {
  4562. ata_port_wait_eh(ap);
  4563. WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
  4564. }
  4565. /* request PM ops to EH */
  4566. spin_lock_irqsave(ap->lock, flags);
  4567. ap->pm_mesg = mesg;
  4568. if (wait) {
  4569. rc = 0;
  4570. ap->pm_result = &rc;
  4571. }
  4572. ap->pflags |= ATA_PFLAG_PM_PENDING;
  4573. ap->eh_info.action |= action;
  4574. ap->eh_info.flags |= ehi_flags;
  4575. ata_port_schedule_eh(ap);
  4576. spin_unlock_irqrestore(ap->lock, flags);
  4577. /* wait and check result */
  4578. if (wait) {
  4579. ata_port_wait_eh(ap);
  4580. WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
  4581. if (rc)
  4582. return rc;
  4583. }
  4584. }
  4585. return 0;
  4586. }
  4587. /**
  4588. * ata_host_suspend - suspend host
  4589. * @host: host to suspend
  4590. * @mesg: PM message
  4591. *
  4592. * Suspend @host. Actual operation is performed by EH. This
  4593. * function requests EH to perform PM operations and waits for EH
  4594. * to finish.
  4595. *
  4596. * LOCKING:
  4597. * Kernel thread context (may sleep).
  4598. *
  4599. * RETURNS:
  4600. * 0 on success, -errno on failure.
  4601. */
  4602. int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
  4603. {
  4604. int i, j, rc;
  4605. rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
  4606. if (rc)
  4607. goto fail;
  4608. /* EH is quiescent now. Fail if we have any ready device.
  4609. * This happens if hotplug occurs between completion of device
  4610. * suspension and here.
  4611. */
  4612. for (i = 0; i < host->n_ports; i++) {
  4613. struct ata_port *ap = host->ports[i];
  4614. for (j = 0; j < ATA_MAX_DEVICES; j++) {
  4615. struct ata_device *dev = &ap->device[j];
  4616. if (ata_dev_ready(dev)) {
  4617. ata_port_printk(ap, KERN_WARNING,
  4618. "suspend failed, device %d "
  4619. "still active\n", dev->devno);
  4620. rc = -EBUSY;
  4621. goto fail;
  4622. }
  4623. }
  4624. }
  4625. host->dev->power.power_state = mesg;
  4626. return 0;
  4627. fail:
  4628. ata_host_resume(host);
  4629. return rc;
  4630. }
  4631. /**
  4632. * ata_host_resume - resume host
  4633. * @host: host to resume
  4634. *
  4635. * Resume @host. Actual operation is performed by EH. This
  4636. * function requests EH to perform PM operations and returns.
  4637. * Note that all resume operations are performed parallely.
  4638. *
  4639. * LOCKING:
  4640. * Kernel thread context (may sleep).
  4641. */
  4642. void ata_host_resume(struct ata_host *host)
  4643. {
  4644. ata_host_request_pm(host, PMSG_ON, ATA_EH_SOFTRESET,
  4645. ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
  4646. host->dev->power.power_state = PMSG_ON;
  4647. }
  4648. #endif
  4649. /**
  4650. * ata_port_start - Set port up for dma.
  4651. * @ap: Port to initialize
  4652. *
  4653. * Called just after data structures for each port are
  4654. * initialized. Allocates space for PRD table.
  4655. *
  4656. * May be used as the port_start() entry in ata_port_operations.
  4657. *
  4658. * LOCKING:
  4659. * Inherited from caller.
  4660. */
  4661. int ata_port_start(struct ata_port *ap)
  4662. {
  4663. struct device *dev = ap->dev;
  4664. int rc;
  4665. ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
  4666. GFP_KERNEL);
  4667. if (!ap->prd)
  4668. return -ENOMEM;
  4669. rc = ata_pad_alloc(ap, dev);
  4670. if (rc)
  4671. return rc;
  4672. DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd,
  4673. (unsigned long long)ap->prd_dma);
  4674. return 0;
  4675. }
  4676. /**
  4677. * ata_dev_init - Initialize an ata_device structure
  4678. * @dev: Device structure to initialize
  4679. *
  4680. * Initialize @dev in preparation for probing.
  4681. *
  4682. * LOCKING:
  4683. * Inherited from caller.
  4684. */
  4685. void ata_dev_init(struct ata_device *dev)
  4686. {
  4687. struct ata_port *ap = dev->ap;
  4688. unsigned long flags;
  4689. /* SATA spd limit is bound to the first device */
  4690. ap->sata_spd_limit = ap->hw_sata_spd_limit;
  4691. /* High bits of dev->flags are used to record warm plug
  4692. * requests which occur asynchronously. Synchronize using
  4693. * host lock.
  4694. */
  4695. spin_lock_irqsave(ap->lock, flags);
  4696. dev->flags &= ~ATA_DFLAG_INIT_MASK;
  4697. spin_unlock_irqrestore(ap->lock, flags);
  4698. memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
  4699. sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
  4700. dev->pio_mask = UINT_MAX;
  4701. dev->mwdma_mask = UINT_MAX;
  4702. dev->udma_mask = UINT_MAX;
  4703. }
  4704. /**
  4705. * ata_port_init - Initialize an ata_port structure
  4706. * @ap: Structure to initialize
  4707. * @host: Collection of hosts to which @ap belongs
  4708. * @ent: Probe information provided by low-level driver
  4709. * @port_no: Port number associated with this ata_port
  4710. *
  4711. * Initialize a new ata_port structure.
  4712. *
  4713. * LOCKING:
  4714. * Inherited from caller.
  4715. */
  4716. void ata_port_init(struct ata_port *ap, struct ata_host *host,
  4717. const struct ata_probe_ent *ent, unsigned int port_no)
  4718. {
  4719. unsigned int i;
  4720. ap->lock = &host->lock;
  4721. ap->flags = ATA_FLAG_DISABLED;
  4722. ap->print_id = ata_print_id++;
  4723. ap->ctl = ATA_DEVCTL_OBS;
  4724. ap->host = host;
  4725. ap->dev = ent->dev;
  4726. ap->port_no = port_no;
  4727. if (port_no == 1 && ent->pinfo2) {
  4728. ap->pio_mask = ent->pinfo2->pio_mask;
  4729. ap->mwdma_mask = ent->pinfo2->mwdma_mask;
  4730. ap->udma_mask = ent->pinfo2->udma_mask;
  4731. ap->flags |= ent->pinfo2->flags;
  4732. ap->ops = ent->pinfo2->port_ops;
  4733. } else {
  4734. ap->pio_mask = ent->pio_mask;
  4735. ap->mwdma_mask = ent->mwdma_mask;
  4736. ap->udma_mask = ent->udma_mask;
  4737. ap->flags |= ent->port_flags;
  4738. ap->ops = ent->port_ops;
  4739. }
  4740. ap->hw_sata_spd_limit = UINT_MAX;
  4741. ap->active_tag = ATA_TAG_POISON;
  4742. ap->last_ctl = 0xFF;
  4743. #if defined(ATA_VERBOSE_DEBUG)
  4744. /* turn on all debugging levels */
  4745. ap->msg_enable = 0x00FF;
  4746. #elif defined(ATA_DEBUG)
  4747. ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
  4748. #else
  4749. ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
  4750. #endif
  4751. INIT_DELAYED_WORK(&ap->port_task, NULL);
  4752. INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
  4753. INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
  4754. INIT_LIST_HEAD(&ap->eh_done_q);
  4755. init_waitqueue_head(&ap->eh_wait_q);
  4756. /* set cable type */
  4757. ap->cbl = ATA_CBL_NONE;
  4758. if (ap->flags & ATA_FLAG_SATA)
  4759. ap->cbl = ATA_CBL_SATA;
  4760. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  4761. struct ata_device *dev = &ap->device[i];
  4762. dev->ap = ap;
  4763. dev->devno = i;
  4764. ata_dev_init(dev);
  4765. }
  4766. #ifdef ATA_IRQ_TRAP
  4767. ap->stats.unhandled_irq = 1;
  4768. ap->stats.idle_irq = 1;
  4769. #endif
  4770. memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
  4771. }
  4772. /**
  4773. * ata_port_init_shost - Initialize SCSI host associated with ATA port
  4774. * @ap: ATA port to initialize SCSI host for
  4775. * @shost: SCSI host associated with @ap
  4776. *
  4777. * Initialize SCSI host @shost associated with ATA port @ap.
  4778. *
  4779. * LOCKING:
  4780. * Inherited from caller.
  4781. */
  4782. static void ata_port_init_shost(struct ata_port *ap, struct Scsi_Host *shost)
  4783. {
  4784. ap->scsi_host = shost;
  4785. shost->unique_id = ap->print_id;
  4786. shost->max_id = 16;
  4787. shost->max_lun = 1;
  4788. shost->max_channel = 1;
  4789. shost->max_cmd_len = 12;
  4790. }
  4791. /**
  4792. * ata_port_add - Attach low-level ATA driver to system
  4793. * @ent: Information provided by low-level driver
  4794. * @host: Collections of ports to which we add
  4795. * @port_no: Port number associated with this host
  4796. *
  4797. * Attach low-level ATA driver to system.
  4798. *
  4799. * LOCKING:
  4800. * PCI/etc. bus probe sem.
  4801. *
  4802. * RETURNS:
  4803. * New ata_port on success, for NULL on error.
  4804. */
  4805. static struct ata_port * ata_port_add(const struct ata_probe_ent *ent,
  4806. struct ata_host *host,
  4807. unsigned int port_no)
  4808. {
  4809. struct Scsi_Host *shost;
  4810. struct ata_port *ap;
  4811. DPRINTK("ENTER\n");
  4812. if (!ent->port_ops->error_handler &&
  4813. !(ent->port_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) {
  4814. printk(KERN_ERR "ata%u: no reset mechanism available\n",
  4815. port_no);
  4816. return NULL;
  4817. }
  4818. shost = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
  4819. if (!shost)
  4820. return NULL;
  4821. shost->transportt = &ata_scsi_transport_template;
  4822. ap = ata_shost_to_port(shost);
  4823. ata_port_init(ap, host, ent, port_no);
  4824. ata_port_init_shost(ap, shost);
  4825. return ap;
  4826. }
  4827. static void ata_host_release(struct device *gendev, void *res)
  4828. {
  4829. struct ata_host *host = dev_get_drvdata(gendev);
  4830. int i;
  4831. for (i = 0; i < host->n_ports; i++) {
  4832. struct ata_port *ap = host->ports[i];
  4833. if (ap && ap->ops->port_stop)
  4834. ap->ops->port_stop(ap);
  4835. }
  4836. if (host->ops->host_stop)
  4837. host->ops->host_stop(host);
  4838. for (i = 0; i < host->n_ports; i++) {
  4839. struct ata_port *ap = host->ports[i];
  4840. if (ap)
  4841. scsi_host_put(ap->scsi_host);
  4842. host->ports[i] = NULL;
  4843. }
  4844. dev_set_drvdata(gendev, NULL);
  4845. }
  4846. /**
  4847. * ata_sas_host_init - Initialize a host struct
  4848. * @host: host to initialize
  4849. * @dev: device host is attached to
  4850. * @flags: host flags
  4851. * @ops: port_ops
  4852. *
  4853. * LOCKING:
  4854. * PCI/etc. bus probe sem.
  4855. *
  4856. */
  4857. void ata_host_init(struct ata_host *host, struct device *dev,
  4858. unsigned long flags, const struct ata_port_operations *ops)
  4859. {
  4860. spin_lock_init(&host->lock);
  4861. host->dev = dev;
  4862. host->flags = flags;
  4863. host->ops = ops;
  4864. }
  4865. /**
  4866. * ata_device_add - Register hardware device with ATA and SCSI layers
  4867. * @ent: Probe information describing hardware device to be registered
  4868. *
  4869. * This function processes the information provided in the probe
  4870. * information struct @ent, allocates the necessary ATA and SCSI
  4871. * host information structures, initializes them, and registers
  4872. * everything with requisite kernel subsystems.
  4873. *
  4874. * This function requests irqs, probes the ATA bus, and probes
  4875. * the SCSI bus.
  4876. *
  4877. * LOCKING:
  4878. * PCI/etc. bus probe sem.
  4879. *
  4880. * RETURNS:
  4881. * Number of ports registered. Zero on error (no ports registered).
  4882. */
  4883. int ata_device_add(const struct ata_probe_ent *ent)
  4884. {
  4885. unsigned int i;
  4886. struct device *dev = ent->dev;
  4887. struct ata_host *host;
  4888. int rc;
  4889. DPRINTK("ENTER\n");
  4890. if (ent->irq == 0) {
  4891. dev_printk(KERN_ERR, dev, "is not available: No interrupt assigned.\n");
  4892. return 0;
  4893. }
  4894. if (!devres_open_group(dev, ata_device_add, GFP_KERNEL))
  4895. return 0;
  4896. /* alloc a container for our list of ATA ports (buses) */
  4897. host = devres_alloc(ata_host_release, sizeof(struct ata_host) +
  4898. (ent->n_ports * sizeof(void *)), GFP_KERNEL);
  4899. if (!host)
  4900. goto err_out;
  4901. devres_add(dev, host);
  4902. dev_set_drvdata(dev, host);
  4903. ata_host_init(host, dev, ent->_host_flags, ent->port_ops);
  4904. host->n_ports = ent->n_ports;
  4905. host->irq = ent->irq;
  4906. host->irq2 = ent->irq2;
  4907. host->iomap = ent->iomap;
  4908. host->private_data = ent->private_data;
  4909. /* register each port bound to this device */
  4910. for (i = 0; i < host->n_ports; i++) {
  4911. struct ata_port *ap;
  4912. unsigned long xfer_mode_mask;
  4913. int irq_line = ent->irq;
  4914. ap = ata_port_add(ent, host, i);
  4915. host->ports[i] = ap;
  4916. if (!ap)
  4917. goto err_out;
  4918. /* dummy? */
  4919. if (ent->dummy_port_mask & (1 << i)) {
  4920. ata_port_printk(ap, KERN_INFO, "DUMMY\n");
  4921. ap->ops = &ata_dummy_port_ops;
  4922. continue;
  4923. }
  4924. /* start port */
  4925. rc = ap->ops->port_start(ap);
  4926. if (rc) {
  4927. host->ports[i] = NULL;
  4928. scsi_host_put(ap->scsi_host);
  4929. goto err_out;
  4930. }
  4931. /* Report the secondary IRQ for second channel legacy */
  4932. if (i == 1 && ent->irq2)
  4933. irq_line = ent->irq2;
  4934. xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
  4935. (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
  4936. (ap->pio_mask << ATA_SHIFT_PIO);
  4937. /* print per-port info to dmesg */
  4938. ata_port_printk(ap, KERN_INFO, "%cATA max %s cmd 0x%p "
  4939. "ctl 0x%p bmdma 0x%p irq %d\n",
  4940. ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
  4941. ata_mode_string(xfer_mode_mask),
  4942. ap->ioaddr.cmd_addr,
  4943. ap->ioaddr.ctl_addr,
  4944. ap->ioaddr.bmdma_addr,
  4945. irq_line);
  4946. /* freeze port before requesting IRQ */
  4947. ata_eh_freeze_port(ap);
  4948. }
  4949. /* obtain irq, that may be shared between channels */
  4950. rc = devm_request_irq(dev, ent->irq, ent->port_ops->irq_handler,
  4951. ent->irq_flags, DRV_NAME, host);
  4952. if (rc) {
  4953. dev_printk(KERN_ERR, dev, "irq %lu request failed: %d\n",
  4954. ent->irq, rc);
  4955. goto err_out;
  4956. }
  4957. /* do we have a second IRQ for the other channel, eg legacy mode */
  4958. if (ent->irq2) {
  4959. /* We will get weird core code crashes later if this is true
  4960. so trap it now */
  4961. BUG_ON(ent->irq == ent->irq2);
  4962. rc = devm_request_irq(dev, ent->irq2,
  4963. ent->port_ops->irq_handler, ent->irq_flags,
  4964. DRV_NAME, host);
  4965. if (rc) {
  4966. dev_printk(KERN_ERR, dev, "irq %lu request failed: %d\n",
  4967. ent->irq2, rc);
  4968. goto err_out;
  4969. }
  4970. }
  4971. /* resource acquisition complete */
  4972. devres_remove_group(dev, ata_device_add);
  4973. /* perform each probe synchronously */
  4974. DPRINTK("probe begin\n");
  4975. for (i = 0; i < host->n_ports; i++) {
  4976. struct ata_port *ap = host->ports[i];
  4977. u32 scontrol;
  4978. int rc;
  4979. /* init sata_spd_limit to the current value */
  4980. if (sata_scr_read(ap, SCR_CONTROL, &scontrol) == 0) {
  4981. int spd = (scontrol >> 4) & 0xf;
  4982. ap->hw_sata_spd_limit &= (1 << spd) - 1;
  4983. }
  4984. ap->sata_spd_limit = ap->hw_sata_spd_limit;
  4985. rc = scsi_add_host(ap->scsi_host, dev);
  4986. if (rc) {
  4987. ata_port_printk(ap, KERN_ERR, "scsi_add_host failed\n");
  4988. /* FIXME: do something useful here */
  4989. /* FIXME: handle unconditional calls to
  4990. * scsi_scan_host and ata_host_remove, below,
  4991. * at the very least
  4992. */
  4993. }
  4994. if (ap->ops->error_handler) {
  4995. struct ata_eh_info *ehi = &ap->eh_info;
  4996. unsigned long flags;
  4997. ata_port_probe(ap);
  4998. /* kick EH for boot probing */
  4999. spin_lock_irqsave(ap->lock, flags);
  5000. ehi->probe_mask = (1 << ATA_MAX_DEVICES) - 1;
  5001. ehi->action |= ATA_EH_SOFTRESET;
  5002. ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
  5003. ap->pflags |= ATA_PFLAG_LOADING;
  5004. ata_port_schedule_eh(ap);
  5005. spin_unlock_irqrestore(ap->lock, flags);
  5006. /* wait for EH to finish */
  5007. ata_port_wait_eh(ap);
  5008. } else {
  5009. DPRINTK("ata%u: bus probe begin\n", ap->print_id);
  5010. rc = ata_bus_probe(ap);
  5011. DPRINTK("ata%u: bus probe end\n", ap->print_id);
  5012. if (rc) {
  5013. /* FIXME: do something useful here?
  5014. * Current libata behavior will
  5015. * tear down everything when
  5016. * the module is removed
  5017. * or the h/w is unplugged.
  5018. */
  5019. }
  5020. }
  5021. }
  5022. /* probes are done, now scan each port's disk(s) */
  5023. DPRINTK("host probe begin\n");
  5024. for (i = 0; i < host->n_ports; i++) {
  5025. struct ata_port *ap = host->ports[i];
  5026. ata_scsi_scan_host(ap);
  5027. }
  5028. VPRINTK("EXIT, returning %u\n", ent->n_ports);
  5029. return ent->n_ports; /* success */
  5030. err_out:
  5031. devres_release_group(dev, ata_device_add);
  5032. VPRINTK("EXIT, returning %d\n", rc);
  5033. return 0;
  5034. }
  5035. /**
  5036. * ata_port_detach - Detach ATA port in prepration of device removal
  5037. * @ap: ATA port to be detached
  5038. *
  5039. * Detach all ATA devices and the associated SCSI devices of @ap;
  5040. * then, remove the associated SCSI host. @ap is guaranteed to
  5041. * be quiescent on return from this function.
  5042. *
  5043. * LOCKING:
  5044. * Kernel thread context (may sleep).
  5045. */
  5046. void ata_port_detach(struct ata_port *ap)
  5047. {
  5048. unsigned long flags;
  5049. int i;
  5050. if (!ap->ops->error_handler)
  5051. goto skip_eh;
  5052. /* tell EH we're leaving & flush EH */
  5053. spin_lock_irqsave(ap->lock, flags);
  5054. ap->pflags |= ATA_PFLAG_UNLOADING;
  5055. spin_unlock_irqrestore(ap->lock, flags);
  5056. ata_port_wait_eh(ap);
  5057. /* EH is now guaranteed to see UNLOADING, so no new device
  5058. * will be attached. Disable all existing devices.
  5059. */
  5060. spin_lock_irqsave(ap->lock, flags);
  5061. for (i = 0; i < ATA_MAX_DEVICES; i++)
  5062. ata_dev_disable(&ap->device[i]);
  5063. spin_unlock_irqrestore(ap->lock, flags);
  5064. /* Final freeze & EH. All in-flight commands are aborted. EH
  5065. * will be skipped and retrials will be terminated with bad
  5066. * target.
  5067. */
  5068. spin_lock_irqsave(ap->lock, flags);
  5069. ata_port_freeze(ap); /* won't be thawed */
  5070. spin_unlock_irqrestore(ap->lock, flags);
  5071. ata_port_wait_eh(ap);
  5072. /* Flush hotplug task. The sequence is similar to
  5073. * ata_port_flush_task().
  5074. */
  5075. flush_workqueue(ata_aux_wq);
  5076. cancel_delayed_work(&ap->hotplug_task);
  5077. flush_workqueue(ata_aux_wq);
  5078. skip_eh:
  5079. /* remove the associated SCSI host */
  5080. scsi_remove_host(ap->scsi_host);
  5081. }
  5082. /**
  5083. * ata_host_detach - Detach all ports of an ATA host
  5084. * @host: Host to detach
  5085. *
  5086. * Detach all ports of @host.
  5087. *
  5088. * LOCKING:
  5089. * Kernel thread context (may sleep).
  5090. */
  5091. void ata_host_detach(struct ata_host *host)
  5092. {
  5093. int i;
  5094. for (i = 0; i < host->n_ports; i++)
  5095. ata_port_detach(host->ports[i]);
  5096. }
  5097. struct ata_probe_ent *
  5098. ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
  5099. {
  5100. struct ata_probe_ent *probe_ent;
  5101. probe_ent = devm_kzalloc(dev, sizeof(*probe_ent), GFP_KERNEL);
  5102. if (!probe_ent) {
  5103. printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
  5104. kobject_name(&(dev->kobj)));
  5105. return NULL;
  5106. }
  5107. INIT_LIST_HEAD(&probe_ent->node);
  5108. probe_ent->dev = dev;
  5109. probe_ent->sht = port->sht;
  5110. probe_ent->port_flags = port->flags;
  5111. probe_ent->pio_mask = port->pio_mask;
  5112. probe_ent->mwdma_mask = port->mwdma_mask;
  5113. probe_ent->udma_mask = port->udma_mask;
  5114. probe_ent->port_ops = port->port_ops;
  5115. probe_ent->private_data = port->private_data;
  5116. return probe_ent;
  5117. }
  5118. /**
  5119. * ata_std_ports - initialize ioaddr with standard port offsets.
  5120. * @ioaddr: IO address structure to be initialized
  5121. *
  5122. * Utility function which initializes data_addr, error_addr,
  5123. * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
  5124. * device_addr, status_addr, and command_addr to standard offsets
  5125. * relative to cmd_addr.
  5126. *
  5127. * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
  5128. */
  5129. void ata_std_ports(struct ata_ioports *ioaddr)
  5130. {
  5131. ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
  5132. ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
  5133. ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
  5134. ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
  5135. ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
  5136. ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
  5137. ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
  5138. ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
  5139. ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
  5140. ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
  5141. }
  5142. #ifdef CONFIG_PCI
  5143. /**
  5144. * ata_pci_remove_one - PCI layer callback for device removal
  5145. * @pdev: PCI device that was removed
  5146. *
  5147. * PCI layer indicates to libata via this hook that hot-unplug or
  5148. * module unload event has occurred. Detach all ports. Resource
  5149. * release is handled via devres.
  5150. *
  5151. * LOCKING:
  5152. * Inherited from PCI layer (may sleep).
  5153. */
  5154. void ata_pci_remove_one(struct pci_dev *pdev)
  5155. {
  5156. struct device *dev = pci_dev_to_dev(pdev);
  5157. struct ata_host *host = dev_get_drvdata(dev);
  5158. ata_host_detach(host);
  5159. }
  5160. /* move to PCI subsystem */
  5161. int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
  5162. {
  5163. unsigned long tmp = 0;
  5164. switch (bits->width) {
  5165. case 1: {
  5166. u8 tmp8 = 0;
  5167. pci_read_config_byte(pdev, bits->reg, &tmp8);
  5168. tmp = tmp8;
  5169. break;
  5170. }
  5171. case 2: {
  5172. u16 tmp16 = 0;
  5173. pci_read_config_word(pdev, bits->reg, &tmp16);
  5174. tmp = tmp16;
  5175. break;
  5176. }
  5177. case 4: {
  5178. u32 tmp32 = 0;
  5179. pci_read_config_dword(pdev, bits->reg, &tmp32);
  5180. tmp = tmp32;
  5181. break;
  5182. }
  5183. default:
  5184. return -EINVAL;
  5185. }
  5186. tmp &= bits->mask;
  5187. return (tmp == bits->val) ? 1 : 0;
  5188. }
  5189. #ifdef CONFIG_PM
  5190. void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
  5191. {
  5192. pci_save_state(pdev);
  5193. pci_disable_device(pdev);
  5194. if (mesg.event == PM_EVENT_SUSPEND)
  5195. pci_set_power_state(pdev, PCI_D3hot);
  5196. }
  5197. int ata_pci_device_do_resume(struct pci_dev *pdev)
  5198. {
  5199. int rc;
  5200. pci_set_power_state(pdev, PCI_D0);
  5201. pci_restore_state(pdev);
  5202. rc = pcim_enable_device(pdev);
  5203. if (rc) {
  5204. dev_printk(KERN_ERR, &pdev->dev,
  5205. "failed to enable device after resume (%d)\n", rc);
  5206. return rc;
  5207. }
  5208. pci_set_master(pdev);
  5209. return 0;
  5210. }
  5211. int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
  5212. {
  5213. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  5214. int rc = 0;
  5215. rc = ata_host_suspend(host, mesg);
  5216. if (rc)
  5217. return rc;
  5218. ata_pci_device_do_suspend(pdev, mesg);
  5219. return 0;
  5220. }
  5221. int ata_pci_device_resume(struct pci_dev *pdev)
  5222. {
  5223. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  5224. int rc;
  5225. rc = ata_pci_device_do_resume(pdev);
  5226. if (rc == 0)
  5227. ata_host_resume(host);
  5228. return rc;
  5229. }
  5230. #endif /* CONFIG_PM */
  5231. #endif /* CONFIG_PCI */
  5232. static int __init ata_init(void)
  5233. {
  5234. ata_probe_timeout *= HZ;
  5235. ata_wq = create_workqueue("ata");
  5236. if (!ata_wq)
  5237. return -ENOMEM;
  5238. ata_aux_wq = create_singlethread_workqueue("ata_aux");
  5239. if (!ata_aux_wq) {
  5240. destroy_workqueue(ata_wq);
  5241. return -ENOMEM;
  5242. }
  5243. printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
  5244. return 0;
  5245. }
  5246. static void __exit ata_exit(void)
  5247. {
  5248. destroy_workqueue(ata_wq);
  5249. destroy_workqueue(ata_aux_wq);
  5250. }
  5251. subsys_initcall(ata_init);
  5252. module_exit(ata_exit);
  5253. static unsigned long ratelimit_time;
  5254. static DEFINE_SPINLOCK(ata_ratelimit_lock);
  5255. int ata_ratelimit(void)
  5256. {
  5257. int rc;
  5258. unsigned long flags;
  5259. spin_lock_irqsave(&ata_ratelimit_lock, flags);
  5260. if (time_after(jiffies, ratelimit_time)) {
  5261. rc = 1;
  5262. ratelimit_time = jiffies + (HZ/5);
  5263. } else
  5264. rc = 0;
  5265. spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
  5266. return rc;
  5267. }
  5268. /**
  5269. * ata_wait_register - wait until register value changes
  5270. * @reg: IO-mapped register
  5271. * @mask: Mask to apply to read register value
  5272. * @val: Wait condition
  5273. * @interval_msec: polling interval in milliseconds
  5274. * @timeout_msec: timeout in milliseconds
  5275. *
  5276. * Waiting for some bits of register to change is a common
  5277. * operation for ATA controllers. This function reads 32bit LE
  5278. * IO-mapped register @reg and tests for the following condition.
  5279. *
  5280. * (*@reg & mask) != val
  5281. *
  5282. * If the condition is met, it returns; otherwise, the process is
  5283. * repeated after @interval_msec until timeout.
  5284. *
  5285. * LOCKING:
  5286. * Kernel thread context (may sleep)
  5287. *
  5288. * RETURNS:
  5289. * The final register value.
  5290. */
  5291. u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
  5292. unsigned long interval_msec,
  5293. unsigned long timeout_msec)
  5294. {
  5295. unsigned long timeout;
  5296. u32 tmp;
  5297. tmp = ioread32(reg);
  5298. /* Calculate timeout _after_ the first read to make sure
  5299. * preceding writes reach the controller before starting to
  5300. * eat away the timeout.
  5301. */
  5302. timeout = jiffies + (timeout_msec * HZ) / 1000;
  5303. while ((tmp & mask) == val && time_before(jiffies, timeout)) {
  5304. msleep(interval_msec);
  5305. tmp = ioread32(reg);
  5306. }
  5307. return tmp;
  5308. }
  5309. /*
  5310. * Dummy port_ops
  5311. */
  5312. static void ata_dummy_noret(struct ata_port *ap) { }
  5313. static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
  5314. static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
  5315. static u8 ata_dummy_check_status(struct ata_port *ap)
  5316. {
  5317. return ATA_DRDY;
  5318. }
  5319. static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
  5320. {
  5321. return AC_ERR_SYSTEM;
  5322. }
  5323. const struct ata_port_operations ata_dummy_port_ops = {
  5324. .port_disable = ata_port_disable,
  5325. .check_status = ata_dummy_check_status,
  5326. .check_altstatus = ata_dummy_check_status,
  5327. .dev_select = ata_noop_dev_select,
  5328. .qc_prep = ata_noop_qc_prep,
  5329. .qc_issue = ata_dummy_qc_issue,
  5330. .freeze = ata_dummy_noret,
  5331. .thaw = ata_dummy_noret,
  5332. .error_handler = ata_dummy_noret,
  5333. .post_internal_cmd = ata_dummy_qc_noret,
  5334. .irq_clear = ata_dummy_noret,
  5335. .port_start = ata_dummy_ret0,
  5336. .port_stop = ata_dummy_noret,
  5337. };
  5338. /*
  5339. * libata is essentially a library of internal helper functions for
  5340. * low-level ATA host controller drivers. As such, the API/ABI is
  5341. * likely to change as new drivers are added and updated.
  5342. * Do not depend on ABI/API stability.
  5343. */
  5344. EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
  5345. EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
  5346. EXPORT_SYMBOL_GPL(sata_deb_timing_long);
  5347. EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
  5348. EXPORT_SYMBOL_GPL(ata_std_bios_param);
  5349. EXPORT_SYMBOL_GPL(ata_std_ports);
  5350. EXPORT_SYMBOL_GPL(ata_host_init);
  5351. EXPORT_SYMBOL_GPL(ata_device_add);
  5352. EXPORT_SYMBOL_GPL(ata_host_detach);
  5353. EXPORT_SYMBOL_GPL(ata_sg_init);
  5354. EXPORT_SYMBOL_GPL(ata_sg_init_one);
  5355. EXPORT_SYMBOL_GPL(ata_hsm_move);
  5356. EXPORT_SYMBOL_GPL(ata_qc_complete);
  5357. EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
  5358. EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
  5359. EXPORT_SYMBOL_GPL(ata_tf_load);
  5360. EXPORT_SYMBOL_GPL(ata_tf_read);
  5361. EXPORT_SYMBOL_GPL(ata_noop_dev_select);
  5362. EXPORT_SYMBOL_GPL(ata_std_dev_select);
  5363. EXPORT_SYMBOL_GPL(ata_tf_to_fis);
  5364. EXPORT_SYMBOL_GPL(ata_tf_from_fis);
  5365. EXPORT_SYMBOL_GPL(ata_check_status);
  5366. EXPORT_SYMBOL_GPL(ata_altstatus);
  5367. EXPORT_SYMBOL_GPL(ata_exec_command);
  5368. EXPORT_SYMBOL_GPL(ata_port_start);
  5369. EXPORT_SYMBOL_GPL(ata_interrupt);
  5370. EXPORT_SYMBOL_GPL(ata_data_xfer);
  5371. EXPORT_SYMBOL_GPL(ata_data_xfer_noirq);
  5372. EXPORT_SYMBOL_GPL(ata_qc_prep);
  5373. EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
  5374. EXPORT_SYMBOL_GPL(ata_bmdma_setup);
  5375. EXPORT_SYMBOL_GPL(ata_bmdma_start);
  5376. EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
  5377. EXPORT_SYMBOL_GPL(ata_bmdma_status);
  5378. EXPORT_SYMBOL_GPL(ata_bmdma_stop);
  5379. EXPORT_SYMBOL_GPL(ata_bmdma_freeze);
  5380. EXPORT_SYMBOL_GPL(ata_bmdma_thaw);
  5381. EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh);
  5382. EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
  5383. EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
  5384. EXPORT_SYMBOL_GPL(ata_port_probe);
  5385. EXPORT_SYMBOL_GPL(ata_dev_disable);
  5386. EXPORT_SYMBOL_GPL(sata_set_spd);
  5387. EXPORT_SYMBOL_GPL(sata_phy_debounce);
  5388. EXPORT_SYMBOL_GPL(sata_phy_resume);
  5389. EXPORT_SYMBOL_GPL(sata_phy_reset);
  5390. EXPORT_SYMBOL_GPL(__sata_phy_reset);
  5391. EXPORT_SYMBOL_GPL(ata_bus_reset);
  5392. EXPORT_SYMBOL_GPL(ata_std_prereset);
  5393. EXPORT_SYMBOL_GPL(ata_std_softreset);
  5394. EXPORT_SYMBOL_GPL(sata_port_hardreset);
  5395. EXPORT_SYMBOL_GPL(sata_std_hardreset);
  5396. EXPORT_SYMBOL_GPL(ata_std_postreset);
  5397. EXPORT_SYMBOL_GPL(ata_dev_classify);
  5398. EXPORT_SYMBOL_GPL(ata_dev_pair);
  5399. EXPORT_SYMBOL_GPL(ata_port_disable);
  5400. EXPORT_SYMBOL_GPL(ata_ratelimit);
  5401. EXPORT_SYMBOL_GPL(ata_wait_register);
  5402. EXPORT_SYMBOL_GPL(ata_busy_sleep);
  5403. EXPORT_SYMBOL_GPL(ata_port_queue_task);
  5404. EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
  5405. EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
  5406. EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
  5407. EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
  5408. EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
  5409. EXPORT_SYMBOL_GPL(ata_host_intr);
  5410. EXPORT_SYMBOL_GPL(sata_scr_valid);
  5411. EXPORT_SYMBOL_GPL(sata_scr_read);
  5412. EXPORT_SYMBOL_GPL(sata_scr_write);
  5413. EXPORT_SYMBOL_GPL(sata_scr_write_flush);
  5414. EXPORT_SYMBOL_GPL(ata_port_online);
  5415. EXPORT_SYMBOL_GPL(ata_port_offline);
  5416. #ifdef CONFIG_PM
  5417. EXPORT_SYMBOL_GPL(ata_host_suspend);
  5418. EXPORT_SYMBOL_GPL(ata_host_resume);
  5419. #endif /* CONFIG_PM */
  5420. EXPORT_SYMBOL_GPL(ata_id_string);
  5421. EXPORT_SYMBOL_GPL(ata_id_c_string);
  5422. EXPORT_SYMBOL_GPL(ata_id_to_dma_mode);
  5423. EXPORT_SYMBOL_GPL(ata_device_blacklisted);
  5424. EXPORT_SYMBOL_GPL(ata_scsi_simulate);
  5425. EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
  5426. EXPORT_SYMBOL_GPL(ata_timing_compute);
  5427. EXPORT_SYMBOL_GPL(ata_timing_merge);
  5428. #ifdef CONFIG_PCI
  5429. EXPORT_SYMBOL_GPL(pci_test_config_bits);
  5430. EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
  5431. EXPORT_SYMBOL_GPL(ata_pci_init_one);
  5432. EXPORT_SYMBOL_GPL(ata_pci_remove_one);
  5433. #ifdef CONFIG_PM
  5434. EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
  5435. EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
  5436. EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
  5437. EXPORT_SYMBOL_GPL(ata_pci_device_resume);
  5438. #endif /* CONFIG_PM */
  5439. EXPORT_SYMBOL_GPL(ata_pci_default_filter);
  5440. EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
  5441. #endif /* CONFIG_PCI */
  5442. #ifdef CONFIG_PM
  5443. EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
  5444. EXPORT_SYMBOL_GPL(ata_scsi_device_resume);
  5445. #endif /* CONFIG_PM */
  5446. EXPORT_SYMBOL_GPL(ata_eng_timeout);
  5447. EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
  5448. EXPORT_SYMBOL_GPL(ata_port_abort);
  5449. EXPORT_SYMBOL_GPL(ata_port_freeze);
  5450. EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
  5451. EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
  5452. EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
  5453. EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
  5454. EXPORT_SYMBOL_GPL(ata_do_eh);
  5455. EXPORT_SYMBOL_GPL(ata_irq_on);
  5456. EXPORT_SYMBOL_GPL(ata_dummy_irq_on);
  5457. EXPORT_SYMBOL_GPL(ata_irq_ack);
  5458. EXPORT_SYMBOL_GPL(ata_dummy_irq_ack);
  5459. EXPORT_SYMBOL_GPL(ata_dev_try_classify);