libata-core.c 157 KB

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