libata-core.c 170 KB

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