libata-core.c 170 KB

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