libata-core.c 169 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * libata-core.c - helper library for ATA
  4. *
  5. * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
  6. * Copyright 2003-2004 Jeff Garzik
  7. *
  8. * libata documentation is available via 'make {ps|pdf}docs',
  9. * as Documentation/driver-api/libata.rst
  10. *
  11. * Hardware documentation available from http://www.t13.org/ and
  12. * http://www.sata-io.org/
  13. *
  14. * Standards documents from:
  15. * http://www.t13.org (ATA standards, PCI DMA IDE spec)
  16. * http://www.t10.org (SCSI MMC - for ATAPI MMC)
  17. * http://www.sata-io.org (SATA)
  18. * http://www.compactflash.org (CF)
  19. * http://www.qic.org (QIC157 - Tape and DSC)
  20. * http://www.ce-ata.org (CE-ATA: not supported)
  21. *
  22. * libata is essentially a library of internal helper functions for
  23. * low-level ATA host controller drivers. As such, the API/ABI is
  24. * likely to change as new drivers are added and updated.
  25. * Do not depend on ABI/API stability.
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/pci.h>
  30. #include <linux/init.h>
  31. #include <linux/list.h>
  32. #include <linux/mm.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/blkdev.h>
  35. #include <linux/delay.h>
  36. #include <linux/timer.h>
  37. #include <linux/time.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/completion.h>
  40. #include <linux/suspend.h>
  41. #include <linux/workqueue.h>
  42. #include <linux/scatterlist.h>
  43. #include <linux/io.h>
  44. #include <linux/log2.h>
  45. #include <linux/slab.h>
  46. #include <linux/glob.h>
  47. #include <scsi/scsi.h>
  48. #include <scsi/scsi_cmnd.h>
  49. #include <scsi/scsi_host.h>
  50. #include <linux/libata.h>
  51. #include <asm/byteorder.h>
  52. #include <asm/unaligned.h>
  53. #include <linux/cdrom.h>
  54. #include <linux/ratelimit.h>
  55. #include <linux/leds.h>
  56. #include <linux/pm_runtime.h>
  57. #include <linux/platform_device.h>
  58. #include <asm/setup.h>
  59. #define CREATE_TRACE_POINTS
  60. #include <trace/events/libata.h>
  61. #include "libata.h"
  62. #include "libata-transport.h"
  63. const struct ata_port_operations ata_base_port_ops = {
  64. .prereset = ata_std_prereset,
  65. .postreset = ata_std_postreset,
  66. .error_handler = ata_std_error_handler,
  67. .sched_eh = ata_std_sched_eh,
  68. .end_eh = ata_std_end_eh,
  69. };
  70. const struct ata_port_operations sata_port_ops = {
  71. .inherits = &ata_base_port_ops,
  72. .qc_defer = ata_std_qc_defer,
  73. .hardreset = sata_std_hardreset,
  74. };
  75. EXPORT_SYMBOL_GPL(sata_port_ops);
  76. static unsigned int ata_dev_init_params(struct ata_device *dev,
  77. u16 heads, u16 sectors);
  78. static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
  79. static void ata_dev_xfermask(struct ata_device *dev);
  80. static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
  81. atomic_t ata_print_id = ATOMIC_INIT(0);
  82. #ifdef CONFIG_ATA_FORCE
  83. struct ata_force_param {
  84. const char *name;
  85. u8 cbl;
  86. u8 spd_limit;
  87. unsigned int xfer_mask;
  88. unsigned int horkage_on;
  89. unsigned int horkage_off;
  90. u16 lflags_on;
  91. u16 lflags_off;
  92. };
  93. struct ata_force_ent {
  94. int port;
  95. int device;
  96. struct ata_force_param param;
  97. };
  98. static struct ata_force_ent *ata_force_tbl;
  99. static int ata_force_tbl_size;
  100. static char ata_force_param_buf[COMMAND_LINE_SIZE] __initdata;
  101. /* param_buf is thrown away after initialization, disallow read */
  102. module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
  103. MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/admin-guide/kernel-parameters.rst for details)");
  104. #endif
  105. static int atapi_enabled = 1;
  106. module_param(atapi_enabled, int, 0444);
  107. MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
  108. static int atapi_dmadir = 0;
  109. module_param(atapi_dmadir, int, 0444);
  110. MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
  111. int atapi_passthru16 = 1;
  112. module_param(atapi_passthru16, int, 0444);
  113. MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
  114. int libata_fua = 0;
  115. module_param_named(fua, libata_fua, int, 0444);
  116. MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
  117. static int ata_ignore_hpa;
  118. module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
  119. MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
  120. static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
  121. module_param_named(dma, libata_dma_mask, int, 0444);
  122. MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
  123. static int ata_probe_timeout;
  124. module_param(ata_probe_timeout, int, 0444);
  125. MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
  126. int libata_noacpi = 0;
  127. module_param_named(noacpi, libata_noacpi, int, 0444);
  128. MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
  129. int libata_allow_tpm = 0;
  130. module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
  131. MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
  132. static int atapi_an;
  133. module_param(atapi_an, int, 0444);
  134. MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
  135. MODULE_AUTHOR("Jeff Garzik");
  136. MODULE_DESCRIPTION("Library module for ATA devices");
  137. MODULE_LICENSE("GPL");
  138. MODULE_VERSION(DRV_VERSION);
  139. static inline bool ata_dev_print_info(struct ata_device *dev)
  140. {
  141. struct ata_eh_context *ehc = &dev->link->eh_context;
  142. return ehc->i.flags & ATA_EHI_PRINTINFO;
  143. }
  144. static bool ata_sstatus_online(u32 sstatus)
  145. {
  146. return (sstatus & 0xf) == 0x3;
  147. }
  148. /**
  149. * ata_link_next - link iteration helper
  150. * @link: the previous link, NULL to start
  151. * @ap: ATA port containing links to iterate
  152. * @mode: iteration mode, one of ATA_LITER_*
  153. *
  154. * LOCKING:
  155. * Host lock or EH context.
  156. *
  157. * RETURNS:
  158. * Pointer to the next link.
  159. */
  160. struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
  161. enum ata_link_iter_mode mode)
  162. {
  163. BUG_ON(mode != ATA_LITER_EDGE &&
  164. mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
  165. /* NULL link indicates start of iteration */
  166. if (!link)
  167. switch (mode) {
  168. case ATA_LITER_EDGE:
  169. case ATA_LITER_PMP_FIRST:
  170. if (sata_pmp_attached(ap))
  171. return ap->pmp_link;
  172. fallthrough;
  173. case ATA_LITER_HOST_FIRST:
  174. return &ap->link;
  175. }
  176. /* we just iterated over the host link, what's next? */
  177. if (link == &ap->link)
  178. switch (mode) {
  179. case ATA_LITER_HOST_FIRST:
  180. if (sata_pmp_attached(ap))
  181. return ap->pmp_link;
  182. fallthrough;
  183. case ATA_LITER_PMP_FIRST:
  184. if (unlikely(ap->slave_link))
  185. return ap->slave_link;
  186. fallthrough;
  187. case ATA_LITER_EDGE:
  188. return NULL;
  189. }
  190. /* slave_link excludes PMP */
  191. if (unlikely(link == ap->slave_link))
  192. return NULL;
  193. /* we were over a PMP link */
  194. if (++link < ap->pmp_link + ap->nr_pmp_links)
  195. return link;
  196. if (mode == ATA_LITER_PMP_FIRST)
  197. return &ap->link;
  198. return NULL;
  199. }
  200. EXPORT_SYMBOL_GPL(ata_link_next);
  201. /**
  202. * ata_dev_next - device iteration helper
  203. * @dev: the previous device, NULL to start
  204. * @link: ATA link containing devices to iterate
  205. * @mode: iteration mode, one of ATA_DITER_*
  206. *
  207. * LOCKING:
  208. * Host lock or EH context.
  209. *
  210. * RETURNS:
  211. * Pointer to the next device.
  212. */
  213. struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
  214. enum ata_dev_iter_mode mode)
  215. {
  216. BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
  217. mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
  218. /* NULL dev indicates start of iteration */
  219. if (!dev)
  220. switch (mode) {
  221. case ATA_DITER_ENABLED:
  222. case ATA_DITER_ALL:
  223. dev = link->device;
  224. goto check;
  225. case ATA_DITER_ENABLED_REVERSE:
  226. case ATA_DITER_ALL_REVERSE:
  227. dev = link->device + ata_link_max_devices(link) - 1;
  228. goto check;
  229. }
  230. next:
  231. /* move to the next one */
  232. switch (mode) {
  233. case ATA_DITER_ENABLED:
  234. case ATA_DITER_ALL:
  235. if (++dev < link->device + ata_link_max_devices(link))
  236. goto check;
  237. return NULL;
  238. case ATA_DITER_ENABLED_REVERSE:
  239. case ATA_DITER_ALL_REVERSE:
  240. if (--dev >= link->device)
  241. goto check;
  242. return NULL;
  243. }
  244. check:
  245. if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
  246. !ata_dev_enabled(dev))
  247. goto next;
  248. return dev;
  249. }
  250. EXPORT_SYMBOL_GPL(ata_dev_next);
  251. /**
  252. * ata_dev_phys_link - find physical link for a device
  253. * @dev: ATA device to look up physical link for
  254. *
  255. * Look up physical link which @dev is attached to. Note that
  256. * this is different from @dev->link only when @dev is on slave
  257. * link. For all other cases, it's the same as @dev->link.
  258. *
  259. * LOCKING:
  260. * Don't care.
  261. *
  262. * RETURNS:
  263. * Pointer to the found physical link.
  264. */
  265. struct ata_link *ata_dev_phys_link(struct ata_device *dev)
  266. {
  267. struct ata_port *ap = dev->link->ap;
  268. if (!ap->slave_link)
  269. return dev->link;
  270. if (!dev->devno)
  271. return &ap->link;
  272. return ap->slave_link;
  273. }
  274. #ifdef CONFIG_ATA_FORCE
  275. /**
  276. * ata_force_cbl - force cable type according to libata.force
  277. * @ap: ATA port of interest
  278. *
  279. * Force cable type according to libata.force and whine about it.
  280. * The last entry which has matching port number is used, so it
  281. * can be specified as part of device force parameters. For
  282. * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
  283. * same effect.
  284. *
  285. * LOCKING:
  286. * EH context.
  287. */
  288. void ata_force_cbl(struct ata_port *ap)
  289. {
  290. int i;
  291. for (i = ata_force_tbl_size - 1; i >= 0; i--) {
  292. const struct ata_force_ent *fe = &ata_force_tbl[i];
  293. if (fe->port != -1 && fe->port != ap->print_id)
  294. continue;
  295. if (fe->param.cbl == ATA_CBL_NONE)
  296. continue;
  297. ap->cbl = fe->param.cbl;
  298. ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
  299. return;
  300. }
  301. }
  302. /**
  303. * ata_force_link_limits - force link limits according to libata.force
  304. * @link: ATA link of interest
  305. *
  306. * Force link flags and SATA spd limit according to libata.force
  307. * and whine about it. When only the port part is specified
  308. * (e.g. 1:), the limit applies to all links connected to both
  309. * the host link and all fan-out ports connected via PMP. If the
  310. * device part is specified as 0 (e.g. 1.00:), it specifies the
  311. * first fan-out link not the host link. Device number 15 always
  312. * points to the host link whether PMP is attached or not. If the
  313. * controller has slave link, device number 16 points to it.
  314. *
  315. * LOCKING:
  316. * EH context.
  317. */
  318. static void ata_force_link_limits(struct ata_link *link)
  319. {
  320. bool did_spd = false;
  321. int linkno = link->pmp;
  322. int i;
  323. if (ata_is_host_link(link))
  324. linkno += 15;
  325. for (i = ata_force_tbl_size - 1; i >= 0; i--) {
  326. const struct ata_force_ent *fe = &ata_force_tbl[i];
  327. if (fe->port != -1 && fe->port != link->ap->print_id)
  328. continue;
  329. if (fe->device != -1 && fe->device != linkno)
  330. continue;
  331. /* only honor the first spd limit */
  332. if (!did_spd && fe->param.spd_limit) {
  333. link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
  334. ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
  335. fe->param.name);
  336. did_spd = true;
  337. }
  338. /* let lflags stack */
  339. if (fe->param.lflags_on) {
  340. link->flags |= fe->param.lflags_on;
  341. ata_link_notice(link,
  342. "FORCE: link flag 0x%x forced -> 0x%x\n",
  343. fe->param.lflags_on, link->flags);
  344. }
  345. if (fe->param.lflags_off) {
  346. link->flags &= ~fe->param.lflags_off;
  347. ata_link_notice(link,
  348. "FORCE: link flag 0x%x cleared -> 0x%x\n",
  349. fe->param.lflags_off, link->flags);
  350. }
  351. }
  352. }
  353. /**
  354. * ata_force_xfermask - force xfermask according to libata.force
  355. * @dev: ATA device of interest
  356. *
  357. * Force xfer_mask according to libata.force and whine about it.
  358. * For consistency with link selection, device number 15 selects
  359. * the first device connected to the host link.
  360. *
  361. * LOCKING:
  362. * EH context.
  363. */
  364. static void ata_force_xfermask(struct ata_device *dev)
  365. {
  366. int devno = dev->link->pmp + dev->devno;
  367. int alt_devno = devno;
  368. int i;
  369. /* allow n.15/16 for devices attached to host port */
  370. if (ata_is_host_link(dev->link))
  371. alt_devno += 15;
  372. for (i = ata_force_tbl_size - 1; i >= 0; i--) {
  373. const struct ata_force_ent *fe = &ata_force_tbl[i];
  374. unsigned int pio_mask, mwdma_mask, udma_mask;
  375. if (fe->port != -1 && fe->port != dev->link->ap->print_id)
  376. continue;
  377. if (fe->device != -1 && fe->device != devno &&
  378. fe->device != alt_devno)
  379. continue;
  380. if (!fe->param.xfer_mask)
  381. continue;
  382. ata_unpack_xfermask(fe->param.xfer_mask,
  383. &pio_mask, &mwdma_mask, &udma_mask);
  384. if (udma_mask)
  385. dev->udma_mask = udma_mask;
  386. else if (mwdma_mask) {
  387. dev->udma_mask = 0;
  388. dev->mwdma_mask = mwdma_mask;
  389. } else {
  390. dev->udma_mask = 0;
  391. dev->mwdma_mask = 0;
  392. dev->pio_mask = pio_mask;
  393. }
  394. ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n",
  395. fe->param.name);
  396. return;
  397. }
  398. }
  399. /**
  400. * ata_force_horkage - force horkage according to libata.force
  401. * @dev: ATA device of interest
  402. *
  403. * Force horkage according to libata.force and whine about it.
  404. * For consistency with link selection, device number 15 selects
  405. * the first device connected to the host link.
  406. *
  407. * LOCKING:
  408. * EH context.
  409. */
  410. static void ata_force_horkage(struct ata_device *dev)
  411. {
  412. int devno = dev->link->pmp + dev->devno;
  413. int alt_devno = devno;
  414. int i;
  415. /* allow n.15/16 for devices attached to host port */
  416. if (ata_is_host_link(dev->link))
  417. alt_devno += 15;
  418. for (i = 0; i < ata_force_tbl_size; i++) {
  419. const struct ata_force_ent *fe = &ata_force_tbl[i];
  420. if (fe->port != -1 && fe->port != dev->link->ap->print_id)
  421. continue;
  422. if (fe->device != -1 && fe->device != devno &&
  423. fe->device != alt_devno)
  424. continue;
  425. if (!(~dev->horkage & fe->param.horkage_on) &&
  426. !(dev->horkage & fe->param.horkage_off))
  427. continue;
  428. dev->horkage |= fe->param.horkage_on;
  429. dev->horkage &= ~fe->param.horkage_off;
  430. ata_dev_notice(dev, "FORCE: horkage modified (%s)\n",
  431. fe->param.name);
  432. }
  433. }
  434. #else
  435. static inline void ata_force_link_limits(struct ata_link *link) { }
  436. static inline void ata_force_xfermask(struct ata_device *dev) { }
  437. static inline void ata_force_horkage(struct ata_device *dev) { }
  438. #endif
  439. /**
  440. * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
  441. * @opcode: SCSI opcode
  442. *
  443. * Determine ATAPI command type from @opcode.
  444. *
  445. * LOCKING:
  446. * None.
  447. *
  448. * RETURNS:
  449. * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
  450. */
  451. int atapi_cmd_type(u8 opcode)
  452. {
  453. switch (opcode) {
  454. case GPCMD_READ_10:
  455. case GPCMD_READ_12:
  456. return ATAPI_READ;
  457. case GPCMD_WRITE_10:
  458. case GPCMD_WRITE_12:
  459. case GPCMD_WRITE_AND_VERIFY_10:
  460. return ATAPI_WRITE;
  461. case GPCMD_READ_CD:
  462. case GPCMD_READ_CD_MSF:
  463. return ATAPI_READ_CD;
  464. case ATA_16:
  465. case ATA_12:
  466. if (atapi_passthru16)
  467. return ATAPI_PASS_THRU;
  468. fallthrough;
  469. default:
  470. return ATAPI_MISC;
  471. }
  472. }
  473. EXPORT_SYMBOL_GPL(atapi_cmd_type);
  474. static const u8 ata_rw_cmds[] = {
  475. /* pio multi */
  476. ATA_CMD_READ_MULTI,
  477. ATA_CMD_WRITE_MULTI,
  478. ATA_CMD_READ_MULTI_EXT,
  479. ATA_CMD_WRITE_MULTI_EXT,
  480. 0,
  481. 0,
  482. 0,
  483. ATA_CMD_WRITE_MULTI_FUA_EXT,
  484. /* pio */
  485. ATA_CMD_PIO_READ,
  486. ATA_CMD_PIO_WRITE,
  487. ATA_CMD_PIO_READ_EXT,
  488. ATA_CMD_PIO_WRITE_EXT,
  489. 0,
  490. 0,
  491. 0,
  492. 0,
  493. /* dma */
  494. ATA_CMD_READ,
  495. ATA_CMD_WRITE,
  496. ATA_CMD_READ_EXT,
  497. ATA_CMD_WRITE_EXT,
  498. 0,
  499. 0,
  500. 0,
  501. ATA_CMD_WRITE_FUA_EXT
  502. };
  503. /**
  504. * ata_rwcmd_protocol - set taskfile r/w commands and protocol
  505. * @tf: command to examine and configure
  506. * @dev: device tf belongs to
  507. *
  508. * Examine the device configuration and tf->flags to calculate
  509. * the proper read/write commands and protocol to use.
  510. *
  511. * LOCKING:
  512. * caller.
  513. */
  514. static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
  515. {
  516. u8 cmd;
  517. int index, fua, lba48, write;
  518. fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
  519. lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
  520. write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
  521. if (dev->flags & ATA_DFLAG_PIO) {
  522. tf->protocol = ATA_PROT_PIO;
  523. index = dev->multi_count ? 0 : 8;
  524. } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
  525. /* Unable to use DMA due to host limitation */
  526. tf->protocol = ATA_PROT_PIO;
  527. index = dev->multi_count ? 0 : 8;
  528. } else {
  529. tf->protocol = ATA_PROT_DMA;
  530. index = 16;
  531. }
  532. cmd = ata_rw_cmds[index + fua + lba48 + write];
  533. if (cmd) {
  534. tf->command = cmd;
  535. return 0;
  536. }
  537. return -1;
  538. }
  539. /**
  540. * ata_tf_read_block - Read block address from ATA taskfile
  541. * @tf: ATA taskfile of interest
  542. * @dev: ATA device @tf belongs to
  543. *
  544. * LOCKING:
  545. * None.
  546. *
  547. * Read block address from @tf. This function can handle all
  548. * three address formats - LBA, LBA48 and CHS. tf->protocol and
  549. * flags select the address format to use.
  550. *
  551. * RETURNS:
  552. * Block address read from @tf.
  553. */
  554. u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev)
  555. {
  556. u64 block = 0;
  557. if (tf->flags & ATA_TFLAG_LBA) {
  558. if (tf->flags & ATA_TFLAG_LBA48) {
  559. block |= (u64)tf->hob_lbah << 40;
  560. block |= (u64)tf->hob_lbam << 32;
  561. block |= (u64)tf->hob_lbal << 24;
  562. } else
  563. block |= (tf->device & 0xf) << 24;
  564. block |= tf->lbah << 16;
  565. block |= tf->lbam << 8;
  566. block |= tf->lbal;
  567. } else {
  568. u32 cyl, head, sect;
  569. cyl = tf->lbam | (tf->lbah << 8);
  570. head = tf->device & 0xf;
  571. sect = tf->lbal;
  572. if (!sect) {
  573. ata_dev_warn(dev,
  574. "device reported invalid CHS sector 0\n");
  575. return U64_MAX;
  576. }
  577. block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
  578. }
  579. return block;
  580. }
  581. /**
  582. * ata_build_rw_tf - Build ATA taskfile for given read/write request
  583. * @qc: Metadata associated with the taskfile to build
  584. * @block: Block address
  585. * @n_block: Number of blocks
  586. * @tf_flags: RW/FUA etc...
  587. * @class: IO priority class
  588. *
  589. * LOCKING:
  590. * None.
  591. *
  592. * Build ATA taskfile for the command @qc for read/write request described
  593. * by @block, @n_block, @tf_flags and @class.
  594. *
  595. * RETURNS:
  596. *
  597. * 0 on success, -ERANGE if the request is too large for @dev,
  598. * -EINVAL if the request is invalid.
  599. */
  600. int ata_build_rw_tf(struct ata_queued_cmd *qc, u64 block, u32 n_block,
  601. unsigned int tf_flags, int class)
  602. {
  603. struct ata_taskfile *tf = &qc->tf;
  604. struct ata_device *dev = qc->dev;
  605. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  606. tf->flags |= tf_flags;
  607. if (ata_ncq_enabled(dev)) {
  608. /* yay, NCQ */
  609. if (!lba_48_ok(block, n_block))
  610. return -ERANGE;
  611. tf->protocol = ATA_PROT_NCQ;
  612. tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
  613. if (tf->flags & ATA_TFLAG_WRITE)
  614. tf->command = ATA_CMD_FPDMA_WRITE;
  615. else
  616. tf->command = ATA_CMD_FPDMA_READ;
  617. tf->nsect = qc->hw_tag << 3;
  618. tf->hob_feature = (n_block >> 8) & 0xff;
  619. tf->feature = n_block & 0xff;
  620. tf->hob_lbah = (block >> 40) & 0xff;
  621. tf->hob_lbam = (block >> 32) & 0xff;
  622. tf->hob_lbal = (block >> 24) & 0xff;
  623. tf->lbah = (block >> 16) & 0xff;
  624. tf->lbam = (block >> 8) & 0xff;
  625. tf->lbal = block & 0xff;
  626. tf->device = ATA_LBA;
  627. if (tf->flags & ATA_TFLAG_FUA)
  628. tf->device |= 1 << 7;
  629. if (dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLED &&
  630. class == IOPRIO_CLASS_RT)
  631. tf->hob_nsect |= ATA_PRIO_HIGH << ATA_SHIFT_PRIO;
  632. } else if (dev->flags & ATA_DFLAG_LBA) {
  633. tf->flags |= ATA_TFLAG_LBA;
  634. if (lba_28_ok(block, n_block)) {
  635. /* use LBA28 */
  636. tf->device |= (block >> 24) & 0xf;
  637. } else if (lba_48_ok(block, n_block)) {
  638. if (!(dev->flags & ATA_DFLAG_LBA48))
  639. return -ERANGE;
  640. /* use LBA48 */
  641. tf->flags |= ATA_TFLAG_LBA48;
  642. tf->hob_nsect = (n_block >> 8) & 0xff;
  643. tf->hob_lbah = (block >> 40) & 0xff;
  644. tf->hob_lbam = (block >> 32) & 0xff;
  645. tf->hob_lbal = (block >> 24) & 0xff;
  646. } else
  647. /* request too large even for LBA48 */
  648. return -ERANGE;
  649. if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
  650. return -EINVAL;
  651. tf->nsect = n_block & 0xff;
  652. tf->lbah = (block >> 16) & 0xff;
  653. tf->lbam = (block >> 8) & 0xff;
  654. tf->lbal = block & 0xff;
  655. tf->device |= ATA_LBA;
  656. } else {
  657. /* CHS */
  658. u32 sect, head, cyl, track;
  659. /* The request -may- be too large for CHS addressing. */
  660. if (!lba_28_ok(block, n_block))
  661. return -ERANGE;
  662. if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
  663. return -EINVAL;
  664. /* Convert LBA to CHS */
  665. track = (u32)block / dev->sectors;
  666. cyl = track / dev->heads;
  667. head = track % dev->heads;
  668. sect = (u32)block % dev->sectors + 1;
  669. /* Check whether the converted CHS can fit.
  670. Cylinder: 0-65535
  671. Head: 0-15
  672. Sector: 1-255*/
  673. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  674. return -ERANGE;
  675. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  676. tf->lbal = sect;
  677. tf->lbam = cyl;
  678. tf->lbah = cyl >> 8;
  679. tf->device |= head;
  680. }
  681. return 0;
  682. }
  683. /**
  684. * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
  685. * @pio_mask: pio_mask
  686. * @mwdma_mask: mwdma_mask
  687. * @udma_mask: udma_mask
  688. *
  689. * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
  690. * unsigned int xfer_mask.
  691. *
  692. * LOCKING:
  693. * None.
  694. *
  695. * RETURNS:
  696. * Packed xfer_mask.
  697. */
  698. unsigned int ata_pack_xfermask(unsigned int pio_mask,
  699. unsigned int mwdma_mask,
  700. unsigned int udma_mask)
  701. {
  702. return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
  703. ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
  704. ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
  705. }
  706. EXPORT_SYMBOL_GPL(ata_pack_xfermask);
  707. /**
  708. * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
  709. * @xfer_mask: xfer_mask to unpack
  710. * @pio_mask: resulting pio_mask
  711. * @mwdma_mask: resulting mwdma_mask
  712. * @udma_mask: resulting udma_mask
  713. *
  714. * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
  715. * Any NULL destination masks will be ignored.
  716. */
  717. void ata_unpack_xfermask(unsigned int xfer_mask, unsigned int *pio_mask,
  718. unsigned int *mwdma_mask, unsigned int *udma_mask)
  719. {
  720. if (pio_mask)
  721. *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
  722. if (mwdma_mask)
  723. *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
  724. if (udma_mask)
  725. *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
  726. }
  727. static const struct ata_xfer_ent {
  728. int shift, bits;
  729. u8 base;
  730. } ata_xfer_tbl[] = {
  731. { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
  732. { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
  733. { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
  734. { -1, },
  735. };
  736. /**
  737. * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
  738. * @xfer_mask: xfer_mask of interest
  739. *
  740. * Return matching XFER_* value for @xfer_mask. Only the highest
  741. * bit of @xfer_mask is considered.
  742. *
  743. * LOCKING:
  744. * None.
  745. *
  746. * RETURNS:
  747. * Matching XFER_* value, 0xff if no match found.
  748. */
  749. u8 ata_xfer_mask2mode(unsigned int xfer_mask)
  750. {
  751. int highbit = fls(xfer_mask) - 1;
  752. const struct ata_xfer_ent *ent;
  753. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  754. if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
  755. return ent->base + highbit - ent->shift;
  756. return 0xff;
  757. }
  758. EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
  759. /**
  760. * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
  761. * @xfer_mode: XFER_* of interest
  762. *
  763. * Return matching xfer_mask for @xfer_mode.
  764. *
  765. * LOCKING:
  766. * None.
  767. *
  768. * RETURNS:
  769. * Matching xfer_mask, 0 if no match found.
  770. */
  771. unsigned int ata_xfer_mode2mask(u8 xfer_mode)
  772. {
  773. const struct ata_xfer_ent *ent;
  774. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  775. if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
  776. return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
  777. & ~((1 << ent->shift) - 1);
  778. return 0;
  779. }
  780. EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
  781. /**
  782. * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
  783. * @xfer_mode: XFER_* of interest
  784. *
  785. * Return matching xfer_shift for @xfer_mode.
  786. *
  787. * LOCKING:
  788. * None.
  789. *
  790. * RETURNS:
  791. * Matching xfer_shift, -1 if no match found.
  792. */
  793. int ata_xfer_mode2shift(u8 xfer_mode)
  794. {
  795. const struct ata_xfer_ent *ent;
  796. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  797. if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
  798. return ent->shift;
  799. return -1;
  800. }
  801. EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
  802. /**
  803. * ata_mode_string - convert xfer_mask to string
  804. * @xfer_mask: mask of bits supported; only highest bit counts.
  805. *
  806. * Determine string which represents the highest speed
  807. * (highest bit in @modemask).
  808. *
  809. * LOCKING:
  810. * None.
  811. *
  812. * RETURNS:
  813. * Constant C string representing highest speed listed in
  814. * @mode_mask, or the constant C string "<n/a>".
  815. */
  816. const char *ata_mode_string(unsigned int xfer_mask)
  817. {
  818. static const char * const xfer_mode_str[] = {
  819. "PIO0",
  820. "PIO1",
  821. "PIO2",
  822. "PIO3",
  823. "PIO4",
  824. "PIO5",
  825. "PIO6",
  826. "MWDMA0",
  827. "MWDMA1",
  828. "MWDMA2",
  829. "MWDMA3",
  830. "MWDMA4",
  831. "UDMA/16",
  832. "UDMA/25",
  833. "UDMA/33",
  834. "UDMA/44",
  835. "UDMA/66",
  836. "UDMA/100",
  837. "UDMA/133",
  838. "UDMA7",
  839. };
  840. int highbit;
  841. highbit = fls(xfer_mask) - 1;
  842. if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
  843. return xfer_mode_str[highbit];
  844. return "<n/a>";
  845. }
  846. EXPORT_SYMBOL_GPL(ata_mode_string);
  847. const char *sata_spd_string(unsigned int spd)
  848. {
  849. static const char * const spd_str[] = {
  850. "1.5 Gbps",
  851. "3.0 Gbps",
  852. "6.0 Gbps",
  853. };
  854. if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
  855. return "<unknown>";
  856. return spd_str[spd - 1];
  857. }
  858. /**
  859. * ata_dev_classify - determine device type based on ATA-spec signature
  860. * @tf: ATA taskfile register set for device to be identified
  861. *
  862. * Determine from taskfile register contents whether a device is
  863. * ATA or ATAPI, as per "Signature and persistence" section
  864. * of ATA/PI spec (volume 1, sect 5.14).
  865. *
  866. * LOCKING:
  867. * None.
  868. *
  869. * RETURNS:
  870. * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
  871. * %ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
  872. */
  873. unsigned int ata_dev_classify(const struct ata_taskfile *tf)
  874. {
  875. /* Apple's open source Darwin code hints that some devices only
  876. * put a proper signature into the LBA mid/high registers,
  877. * So, we only check those. It's sufficient for uniqueness.
  878. *
  879. * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
  880. * signatures for ATA and ATAPI devices attached on SerialATA,
  881. * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
  882. * spec has never mentioned about using different signatures
  883. * for ATA/ATAPI devices. Then, Serial ATA II: Port
  884. * Multiplier specification began to use 0x69/0x96 to identify
  885. * port multpliers and 0x3c/0xc3 to identify SEMB device.
  886. * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
  887. * 0x69/0x96 shortly and described them as reserved for
  888. * SerialATA.
  889. *
  890. * We follow the current spec and consider that 0x69/0x96
  891. * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
  892. * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
  893. * SEMB signature. This is worked around in
  894. * ata_dev_read_id().
  895. */
  896. if (tf->lbam == 0 && tf->lbah == 0)
  897. return ATA_DEV_ATA;
  898. if (tf->lbam == 0x14 && tf->lbah == 0xeb)
  899. return ATA_DEV_ATAPI;
  900. if (tf->lbam == 0x69 && tf->lbah == 0x96)
  901. return ATA_DEV_PMP;
  902. if (tf->lbam == 0x3c && tf->lbah == 0xc3)
  903. return ATA_DEV_SEMB;
  904. if (tf->lbam == 0xcd && tf->lbah == 0xab)
  905. return ATA_DEV_ZAC;
  906. return ATA_DEV_UNKNOWN;
  907. }
  908. EXPORT_SYMBOL_GPL(ata_dev_classify);
  909. /**
  910. * ata_id_string - Convert IDENTIFY DEVICE page into string
  911. * @id: IDENTIFY DEVICE results we will examine
  912. * @s: string into which data is output
  913. * @ofs: offset into identify device page
  914. * @len: length of string to return. must be an even number.
  915. *
  916. * The strings in the IDENTIFY DEVICE page are broken up into
  917. * 16-bit chunks. Run through the string, and output each
  918. * 8-bit chunk linearly, regardless of platform.
  919. *
  920. * LOCKING:
  921. * caller.
  922. */
  923. void ata_id_string(const u16 *id, unsigned char *s,
  924. unsigned int ofs, unsigned int len)
  925. {
  926. unsigned int c;
  927. BUG_ON(len & 1);
  928. while (len > 0) {
  929. c = id[ofs] >> 8;
  930. *s = c;
  931. s++;
  932. c = id[ofs] & 0xff;
  933. *s = c;
  934. s++;
  935. ofs++;
  936. len -= 2;
  937. }
  938. }
  939. EXPORT_SYMBOL_GPL(ata_id_string);
  940. /**
  941. * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
  942. * @id: IDENTIFY DEVICE results we will examine
  943. * @s: string into which data is output
  944. * @ofs: offset into identify device page
  945. * @len: length of string to return. must be an odd number.
  946. *
  947. * This function is identical to ata_id_string except that it
  948. * trims trailing spaces and terminates the resulting string with
  949. * null. @len must be actual maximum length (even number) + 1.
  950. *
  951. * LOCKING:
  952. * caller.
  953. */
  954. void ata_id_c_string(const u16 *id, unsigned char *s,
  955. unsigned int ofs, unsigned int len)
  956. {
  957. unsigned char *p;
  958. ata_id_string(id, s, ofs, len - 1);
  959. p = s + strnlen(s, len - 1);
  960. while (p > s && p[-1] == ' ')
  961. p--;
  962. *p = '\0';
  963. }
  964. EXPORT_SYMBOL_GPL(ata_id_c_string);
  965. static u64 ata_id_n_sectors(const u16 *id)
  966. {
  967. if (ata_id_has_lba(id)) {
  968. if (ata_id_has_lba48(id))
  969. return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
  970. return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
  971. }
  972. if (ata_id_current_chs_valid(id))
  973. return (u32)id[ATA_ID_CUR_CYLS] * (u32)id[ATA_ID_CUR_HEADS] *
  974. (u32)id[ATA_ID_CUR_SECTORS];
  975. return (u32)id[ATA_ID_CYLS] * (u32)id[ATA_ID_HEADS] *
  976. (u32)id[ATA_ID_SECTORS];
  977. }
  978. u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
  979. {
  980. u64 sectors = 0;
  981. sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
  982. sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
  983. sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
  984. sectors |= (tf->lbah & 0xff) << 16;
  985. sectors |= (tf->lbam & 0xff) << 8;
  986. sectors |= (tf->lbal & 0xff);
  987. return sectors;
  988. }
  989. u64 ata_tf_to_lba(const struct ata_taskfile *tf)
  990. {
  991. u64 sectors = 0;
  992. sectors |= (tf->device & 0x0f) << 24;
  993. sectors |= (tf->lbah & 0xff) << 16;
  994. sectors |= (tf->lbam & 0xff) << 8;
  995. sectors |= (tf->lbal & 0xff);
  996. return sectors;
  997. }
  998. /**
  999. * ata_read_native_max_address - Read native max address
  1000. * @dev: target device
  1001. * @max_sectors: out parameter for the result native max address
  1002. *
  1003. * Perform an LBA48 or LBA28 native size query upon the device in
  1004. * question.
  1005. *
  1006. * RETURNS:
  1007. * 0 on success, -EACCES if command is aborted by the drive.
  1008. * -EIO on other errors.
  1009. */
  1010. static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
  1011. {
  1012. unsigned int err_mask;
  1013. struct ata_taskfile tf;
  1014. int lba48 = ata_id_has_lba48(dev->id);
  1015. ata_tf_init(dev, &tf);
  1016. /* always clear all address registers */
  1017. tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  1018. if (lba48) {
  1019. tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
  1020. tf.flags |= ATA_TFLAG_LBA48;
  1021. } else
  1022. tf.command = ATA_CMD_READ_NATIVE_MAX;
  1023. tf.protocol = ATA_PROT_NODATA;
  1024. tf.device |= ATA_LBA;
  1025. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  1026. if (err_mask) {
  1027. ata_dev_warn(dev,
  1028. "failed to read native max address (err_mask=0x%x)\n",
  1029. err_mask);
  1030. if (err_mask == AC_ERR_DEV && (tf.error & ATA_ABORTED))
  1031. return -EACCES;
  1032. return -EIO;
  1033. }
  1034. if (lba48)
  1035. *max_sectors = ata_tf_to_lba48(&tf) + 1;
  1036. else
  1037. *max_sectors = ata_tf_to_lba(&tf) + 1;
  1038. if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
  1039. (*max_sectors)--;
  1040. return 0;
  1041. }
  1042. /**
  1043. * ata_set_max_sectors - Set max sectors
  1044. * @dev: target device
  1045. * @new_sectors: new max sectors value to set for the device
  1046. *
  1047. * Set max sectors of @dev to @new_sectors.
  1048. *
  1049. * RETURNS:
  1050. * 0 on success, -EACCES if command is aborted or denied (due to
  1051. * previous non-volatile SET_MAX) by the drive. -EIO on other
  1052. * errors.
  1053. */
  1054. static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
  1055. {
  1056. unsigned int err_mask;
  1057. struct ata_taskfile tf;
  1058. int lba48 = ata_id_has_lba48(dev->id);
  1059. new_sectors--;
  1060. ata_tf_init(dev, &tf);
  1061. tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  1062. if (lba48) {
  1063. tf.command = ATA_CMD_SET_MAX_EXT;
  1064. tf.flags |= ATA_TFLAG_LBA48;
  1065. tf.hob_lbal = (new_sectors >> 24) & 0xff;
  1066. tf.hob_lbam = (new_sectors >> 32) & 0xff;
  1067. tf.hob_lbah = (new_sectors >> 40) & 0xff;
  1068. } else {
  1069. tf.command = ATA_CMD_SET_MAX;
  1070. tf.device |= (new_sectors >> 24) & 0xf;
  1071. }
  1072. tf.protocol = ATA_PROT_NODATA;
  1073. tf.device |= ATA_LBA;
  1074. tf.lbal = (new_sectors >> 0) & 0xff;
  1075. tf.lbam = (new_sectors >> 8) & 0xff;
  1076. tf.lbah = (new_sectors >> 16) & 0xff;
  1077. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  1078. if (err_mask) {
  1079. ata_dev_warn(dev,
  1080. "failed to set max address (err_mask=0x%x)\n",
  1081. err_mask);
  1082. if (err_mask == AC_ERR_DEV &&
  1083. (tf.error & (ATA_ABORTED | ATA_IDNF)))
  1084. return -EACCES;
  1085. return -EIO;
  1086. }
  1087. return 0;
  1088. }
  1089. /**
  1090. * ata_hpa_resize - Resize a device with an HPA set
  1091. * @dev: Device to resize
  1092. *
  1093. * Read the size of an LBA28 or LBA48 disk with HPA features and resize
  1094. * it if required to the full size of the media. The caller must check
  1095. * the drive has the HPA feature set enabled.
  1096. *
  1097. * RETURNS:
  1098. * 0 on success, -errno on failure.
  1099. */
  1100. static int ata_hpa_resize(struct ata_device *dev)
  1101. {
  1102. bool print_info = ata_dev_print_info(dev);
  1103. bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
  1104. u64 sectors = ata_id_n_sectors(dev->id);
  1105. u64 native_sectors;
  1106. int rc;
  1107. /* do we need to do it? */
  1108. if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) ||
  1109. !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
  1110. (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
  1111. return 0;
  1112. /* read native max address */
  1113. rc = ata_read_native_max_address(dev, &native_sectors);
  1114. if (rc) {
  1115. /* If device aborted the command or HPA isn't going to
  1116. * be unlocked, skip HPA resizing.
  1117. */
  1118. if (rc == -EACCES || !unlock_hpa) {
  1119. ata_dev_warn(dev,
  1120. "HPA support seems broken, skipping HPA handling\n");
  1121. dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
  1122. /* we can continue if device aborted the command */
  1123. if (rc == -EACCES)
  1124. rc = 0;
  1125. }
  1126. return rc;
  1127. }
  1128. dev->n_native_sectors = native_sectors;
  1129. /* nothing to do? */
  1130. if (native_sectors <= sectors || !unlock_hpa) {
  1131. if (!print_info || native_sectors == sectors)
  1132. return 0;
  1133. if (native_sectors > sectors)
  1134. ata_dev_info(dev,
  1135. "HPA detected: current %llu, native %llu\n",
  1136. (unsigned long long)sectors,
  1137. (unsigned long long)native_sectors);
  1138. else if (native_sectors < sectors)
  1139. ata_dev_warn(dev,
  1140. "native sectors (%llu) is smaller than sectors (%llu)\n",
  1141. (unsigned long long)native_sectors,
  1142. (unsigned long long)sectors);
  1143. return 0;
  1144. }
  1145. /* let's unlock HPA */
  1146. rc = ata_set_max_sectors(dev, native_sectors);
  1147. if (rc == -EACCES) {
  1148. /* if device aborted the command, skip HPA resizing */
  1149. ata_dev_warn(dev,
  1150. "device aborted resize (%llu -> %llu), skipping HPA handling\n",
  1151. (unsigned long long)sectors,
  1152. (unsigned long long)native_sectors);
  1153. dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
  1154. return 0;
  1155. } else if (rc)
  1156. return rc;
  1157. /* re-read IDENTIFY data */
  1158. rc = ata_dev_reread_id(dev, 0);
  1159. if (rc) {
  1160. ata_dev_err(dev,
  1161. "failed to re-read IDENTIFY data after HPA resizing\n");
  1162. return rc;
  1163. }
  1164. if (print_info) {
  1165. u64 new_sectors = ata_id_n_sectors(dev->id);
  1166. ata_dev_info(dev,
  1167. "HPA unlocked: %llu -> %llu, native %llu\n",
  1168. (unsigned long long)sectors,
  1169. (unsigned long long)new_sectors,
  1170. (unsigned long long)native_sectors);
  1171. }
  1172. return 0;
  1173. }
  1174. /**
  1175. * ata_dump_id - IDENTIFY DEVICE info debugging output
  1176. * @dev: device from which the information is fetched
  1177. * @id: IDENTIFY DEVICE page to dump
  1178. *
  1179. * Dump selected 16-bit words from the given IDENTIFY DEVICE
  1180. * page.
  1181. *
  1182. * LOCKING:
  1183. * caller.
  1184. */
  1185. static inline void ata_dump_id(struct ata_device *dev, const u16 *id)
  1186. {
  1187. ata_dev_dbg(dev,
  1188. "49==0x%04x 53==0x%04x 63==0x%04x 64==0x%04x 75==0x%04x\n"
  1189. "80==0x%04x 81==0x%04x 82==0x%04x 83==0x%04x 84==0x%04x\n"
  1190. "88==0x%04x 93==0x%04x\n",
  1191. id[49], id[53], id[63], id[64], id[75], id[80],
  1192. id[81], id[82], id[83], id[84], id[88], id[93]);
  1193. }
  1194. /**
  1195. * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
  1196. * @id: IDENTIFY data to compute xfer mask from
  1197. *
  1198. * Compute the xfermask for this device. This is not as trivial
  1199. * as it seems if we must consider early devices correctly.
  1200. *
  1201. * FIXME: pre IDE drive timing (do we care ?).
  1202. *
  1203. * LOCKING:
  1204. * None.
  1205. *
  1206. * RETURNS:
  1207. * Computed xfermask
  1208. */
  1209. unsigned int ata_id_xfermask(const u16 *id)
  1210. {
  1211. unsigned int pio_mask, mwdma_mask, udma_mask;
  1212. /* Usual case. Word 53 indicates word 64 is valid */
  1213. if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
  1214. pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
  1215. pio_mask <<= 3;
  1216. pio_mask |= 0x7;
  1217. } else {
  1218. /* If word 64 isn't valid then Word 51 high byte holds
  1219. * the PIO timing number for the maximum. Turn it into
  1220. * a mask.
  1221. */
  1222. u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
  1223. if (mode < 5) /* Valid PIO range */
  1224. pio_mask = (2 << mode) - 1;
  1225. else
  1226. pio_mask = 1;
  1227. /* But wait.. there's more. Design your standards by
  1228. * committee and you too can get a free iordy field to
  1229. * process. However it is the speeds not the modes that
  1230. * are supported... Note drivers using the timing API
  1231. * will get this right anyway
  1232. */
  1233. }
  1234. mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
  1235. if (ata_id_is_cfa(id)) {
  1236. /*
  1237. * Process compact flash extended modes
  1238. */
  1239. int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
  1240. int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
  1241. if (pio)
  1242. pio_mask |= (1 << 5);
  1243. if (pio > 1)
  1244. pio_mask |= (1 << 6);
  1245. if (dma)
  1246. mwdma_mask |= (1 << 3);
  1247. if (dma > 1)
  1248. mwdma_mask |= (1 << 4);
  1249. }
  1250. udma_mask = 0;
  1251. if (id[ATA_ID_FIELD_VALID] & (1 << 2))
  1252. udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
  1253. return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
  1254. }
  1255. EXPORT_SYMBOL_GPL(ata_id_xfermask);
  1256. static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
  1257. {
  1258. struct completion *waiting = qc->private_data;
  1259. complete(waiting);
  1260. }
  1261. /**
  1262. * ata_exec_internal_sg - execute libata internal command
  1263. * @dev: Device to which the command is sent
  1264. * @tf: Taskfile registers for the command and the result
  1265. * @cdb: CDB for packet command
  1266. * @dma_dir: Data transfer direction of the command
  1267. * @sgl: sg list for the data buffer of the command
  1268. * @n_elem: Number of sg entries
  1269. * @timeout: Timeout in msecs (0 for default)
  1270. *
  1271. * Executes libata internal command with timeout. @tf contains
  1272. * command on entry and result on return. Timeout and error
  1273. * conditions are reported via return value. No recovery action
  1274. * is taken after a command times out. It's caller's duty to
  1275. * clean up after timeout.
  1276. *
  1277. * LOCKING:
  1278. * None. Should be called with kernel context, might sleep.
  1279. *
  1280. * RETURNS:
  1281. * Zero on success, AC_ERR_* mask on failure
  1282. */
  1283. static unsigned ata_exec_internal_sg(struct ata_device *dev,
  1284. struct ata_taskfile *tf, const u8 *cdb,
  1285. int dma_dir, struct scatterlist *sgl,
  1286. unsigned int n_elem, unsigned int timeout)
  1287. {
  1288. struct ata_link *link = dev->link;
  1289. struct ata_port *ap = link->ap;
  1290. u8 command = tf->command;
  1291. int auto_timeout = 0;
  1292. struct ata_queued_cmd *qc;
  1293. unsigned int preempted_tag;
  1294. u32 preempted_sactive;
  1295. u64 preempted_qc_active;
  1296. int preempted_nr_active_links;
  1297. DECLARE_COMPLETION_ONSTACK(wait);
  1298. unsigned long flags;
  1299. unsigned int err_mask;
  1300. int rc;
  1301. spin_lock_irqsave(ap->lock, flags);
  1302. /* no internal command while frozen */
  1303. if (ap->pflags & ATA_PFLAG_FROZEN) {
  1304. spin_unlock_irqrestore(ap->lock, flags);
  1305. return AC_ERR_SYSTEM;
  1306. }
  1307. /* initialize internal qc */
  1308. qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
  1309. qc->tag = ATA_TAG_INTERNAL;
  1310. qc->hw_tag = 0;
  1311. qc->scsicmd = NULL;
  1312. qc->ap = ap;
  1313. qc->dev = dev;
  1314. ata_qc_reinit(qc);
  1315. preempted_tag = link->active_tag;
  1316. preempted_sactive = link->sactive;
  1317. preempted_qc_active = ap->qc_active;
  1318. preempted_nr_active_links = ap->nr_active_links;
  1319. link->active_tag = ATA_TAG_POISON;
  1320. link->sactive = 0;
  1321. ap->qc_active = 0;
  1322. ap->nr_active_links = 0;
  1323. /* prepare & issue qc */
  1324. qc->tf = *tf;
  1325. if (cdb)
  1326. memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
  1327. /* some SATA bridges need us to indicate data xfer direction */
  1328. if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
  1329. dma_dir == DMA_FROM_DEVICE)
  1330. qc->tf.feature |= ATAPI_DMADIR;
  1331. qc->flags |= ATA_QCFLAG_RESULT_TF;
  1332. qc->dma_dir = dma_dir;
  1333. if (dma_dir != DMA_NONE) {
  1334. unsigned int i, buflen = 0;
  1335. struct scatterlist *sg;
  1336. for_each_sg(sgl, sg, n_elem, i)
  1337. buflen += sg->length;
  1338. ata_sg_init(qc, sgl, n_elem);
  1339. qc->nbytes = buflen;
  1340. }
  1341. qc->private_data = &wait;
  1342. qc->complete_fn = ata_qc_complete_internal;
  1343. ata_qc_issue(qc);
  1344. spin_unlock_irqrestore(ap->lock, flags);
  1345. if (!timeout) {
  1346. if (ata_probe_timeout)
  1347. timeout = ata_probe_timeout * 1000;
  1348. else {
  1349. timeout = ata_internal_cmd_timeout(dev, command);
  1350. auto_timeout = 1;
  1351. }
  1352. }
  1353. if (ap->ops->error_handler)
  1354. ata_eh_release(ap);
  1355. rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
  1356. if (ap->ops->error_handler)
  1357. ata_eh_acquire(ap);
  1358. ata_sff_flush_pio_task(ap);
  1359. if (!rc) {
  1360. spin_lock_irqsave(ap->lock, flags);
  1361. /* We're racing with irq here. If we lose, the
  1362. * following test prevents us from completing the qc
  1363. * twice. If we win, the port is frozen and will be
  1364. * cleaned up by ->post_internal_cmd().
  1365. */
  1366. if (qc->flags & ATA_QCFLAG_ACTIVE) {
  1367. qc->err_mask |= AC_ERR_TIMEOUT;
  1368. if (ap->ops->error_handler)
  1369. ata_port_freeze(ap);
  1370. else
  1371. ata_qc_complete(qc);
  1372. ata_dev_warn(dev, "qc timeout after %u msecs (cmd 0x%x)\n",
  1373. timeout, command);
  1374. }
  1375. spin_unlock_irqrestore(ap->lock, flags);
  1376. }
  1377. /* do post_internal_cmd */
  1378. if (ap->ops->post_internal_cmd)
  1379. ap->ops->post_internal_cmd(qc);
  1380. /* perform minimal error analysis */
  1381. if (qc->flags & ATA_QCFLAG_FAILED) {
  1382. if (qc->result_tf.status & (ATA_ERR | ATA_DF))
  1383. qc->err_mask |= AC_ERR_DEV;
  1384. if (!qc->err_mask)
  1385. qc->err_mask |= AC_ERR_OTHER;
  1386. if (qc->err_mask & ~AC_ERR_OTHER)
  1387. qc->err_mask &= ~AC_ERR_OTHER;
  1388. } else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) {
  1389. qc->result_tf.status |= ATA_SENSE;
  1390. }
  1391. /* finish up */
  1392. spin_lock_irqsave(ap->lock, flags);
  1393. *tf = qc->result_tf;
  1394. err_mask = qc->err_mask;
  1395. ata_qc_free(qc);
  1396. link->active_tag = preempted_tag;
  1397. link->sactive = preempted_sactive;
  1398. ap->qc_active = preempted_qc_active;
  1399. ap->nr_active_links = preempted_nr_active_links;
  1400. spin_unlock_irqrestore(ap->lock, flags);
  1401. if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
  1402. ata_internal_cmd_timed_out(dev, command);
  1403. return err_mask;
  1404. }
  1405. /**
  1406. * ata_exec_internal - execute libata internal command
  1407. * @dev: Device to which the command is sent
  1408. * @tf: Taskfile registers for the command and the result
  1409. * @cdb: CDB for packet command
  1410. * @dma_dir: Data transfer direction of the command
  1411. * @buf: Data buffer of the command
  1412. * @buflen: Length of data buffer
  1413. * @timeout: Timeout in msecs (0 for default)
  1414. *
  1415. * Wrapper around ata_exec_internal_sg() which takes simple
  1416. * buffer instead of sg list.
  1417. *
  1418. * LOCKING:
  1419. * None. Should be called with kernel context, might sleep.
  1420. *
  1421. * RETURNS:
  1422. * Zero on success, AC_ERR_* mask on failure
  1423. */
  1424. unsigned ata_exec_internal(struct ata_device *dev,
  1425. struct ata_taskfile *tf, const u8 *cdb,
  1426. int dma_dir, void *buf, unsigned int buflen,
  1427. unsigned int timeout)
  1428. {
  1429. struct scatterlist *psg = NULL, sg;
  1430. unsigned int n_elem = 0;
  1431. if (dma_dir != DMA_NONE) {
  1432. WARN_ON(!buf);
  1433. sg_init_one(&sg, buf, buflen);
  1434. psg = &sg;
  1435. n_elem++;
  1436. }
  1437. return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
  1438. timeout);
  1439. }
  1440. /**
  1441. * ata_pio_need_iordy - check if iordy needed
  1442. * @adev: ATA device
  1443. *
  1444. * Check if the current speed of the device requires IORDY. Used
  1445. * by various controllers for chip configuration.
  1446. */
  1447. unsigned int ata_pio_need_iordy(const struct ata_device *adev)
  1448. {
  1449. /* Don't set IORDY if we're preparing for reset. IORDY may
  1450. * lead to controller lock up on certain controllers if the
  1451. * port is not occupied. See bko#11703 for details.
  1452. */
  1453. if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
  1454. return 0;
  1455. /* Controller doesn't support IORDY. Probably a pointless
  1456. * check as the caller should know this.
  1457. */
  1458. if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
  1459. return 0;
  1460. /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */
  1461. if (ata_id_is_cfa(adev->id)
  1462. && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
  1463. return 0;
  1464. /* PIO3 and higher it is mandatory */
  1465. if (adev->pio_mode > XFER_PIO_2)
  1466. return 1;
  1467. /* We turn it on when possible */
  1468. if (ata_id_has_iordy(adev->id))
  1469. return 1;
  1470. return 0;
  1471. }
  1472. EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
  1473. /**
  1474. * ata_pio_mask_no_iordy - Return the non IORDY mask
  1475. * @adev: ATA device
  1476. *
  1477. * Compute the highest mode possible if we are not using iordy. Return
  1478. * -1 if no iordy mode is available.
  1479. */
  1480. static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
  1481. {
  1482. /* If we have no drive specific rule, then PIO 2 is non IORDY */
  1483. if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
  1484. u16 pio = adev->id[ATA_ID_EIDE_PIO];
  1485. /* Is the speed faster than the drive allows non IORDY ? */
  1486. if (pio) {
  1487. /* This is cycle times not frequency - watch the logic! */
  1488. if (pio > 240) /* PIO2 is 240nS per cycle */
  1489. return 3 << ATA_SHIFT_PIO;
  1490. return 7 << ATA_SHIFT_PIO;
  1491. }
  1492. }
  1493. return 3 << ATA_SHIFT_PIO;
  1494. }
  1495. /**
  1496. * ata_do_dev_read_id - default ID read method
  1497. * @dev: device
  1498. * @tf: proposed taskfile
  1499. * @id: data buffer
  1500. *
  1501. * Issue the identify taskfile and hand back the buffer containing
  1502. * identify data. For some RAID controllers and for pre ATA devices
  1503. * this function is wrapped or replaced by the driver
  1504. */
  1505. unsigned int ata_do_dev_read_id(struct ata_device *dev,
  1506. struct ata_taskfile *tf, __le16 *id)
  1507. {
  1508. return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
  1509. id, sizeof(id[0]) * ATA_ID_WORDS, 0);
  1510. }
  1511. EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
  1512. /**
  1513. * ata_dev_read_id - Read ID data from the specified device
  1514. * @dev: target device
  1515. * @p_class: pointer to class of the target device (may be changed)
  1516. * @flags: ATA_READID_* flags
  1517. * @id: buffer to read IDENTIFY data into
  1518. *
  1519. * Read ID data from the specified device. ATA_CMD_ID_ATA is
  1520. * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
  1521. * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
  1522. * for pre-ATA4 drives.
  1523. *
  1524. * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
  1525. * now we abort if we hit that case.
  1526. *
  1527. * LOCKING:
  1528. * Kernel thread context (may sleep)
  1529. *
  1530. * RETURNS:
  1531. * 0 on success, -errno otherwise.
  1532. */
  1533. int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
  1534. unsigned int flags, u16 *id)
  1535. {
  1536. struct ata_port *ap = dev->link->ap;
  1537. unsigned int class = *p_class;
  1538. struct ata_taskfile tf;
  1539. unsigned int err_mask = 0;
  1540. const char *reason;
  1541. bool is_semb = class == ATA_DEV_SEMB;
  1542. int may_fallback = 1, tried_spinup = 0;
  1543. int rc;
  1544. retry:
  1545. ata_tf_init(dev, &tf);
  1546. switch (class) {
  1547. case ATA_DEV_SEMB:
  1548. class = ATA_DEV_ATA; /* some hard drives report SEMB sig */
  1549. fallthrough;
  1550. case ATA_DEV_ATA:
  1551. case ATA_DEV_ZAC:
  1552. tf.command = ATA_CMD_ID_ATA;
  1553. break;
  1554. case ATA_DEV_ATAPI:
  1555. tf.command = ATA_CMD_ID_ATAPI;
  1556. break;
  1557. default:
  1558. rc = -ENODEV;
  1559. reason = "unsupported class";
  1560. goto err_out;
  1561. }
  1562. tf.protocol = ATA_PROT_PIO;
  1563. /* Some devices choke if TF registers contain garbage. Make
  1564. * sure those are properly initialized.
  1565. */
  1566. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1567. /* Device presence detection is unreliable on some
  1568. * controllers. Always poll IDENTIFY if available.
  1569. */
  1570. tf.flags |= ATA_TFLAG_POLLING;
  1571. if (ap->ops->read_id)
  1572. err_mask = ap->ops->read_id(dev, &tf, (__le16 *)id);
  1573. else
  1574. err_mask = ata_do_dev_read_id(dev, &tf, (__le16 *)id);
  1575. if (err_mask) {
  1576. if (err_mask & AC_ERR_NODEV_HINT) {
  1577. ata_dev_dbg(dev, "NODEV after polling detection\n");
  1578. return -ENOENT;
  1579. }
  1580. if (is_semb) {
  1581. ata_dev_info(dev,
  1582. "IDENTIFY failed on device w/ SEMB sig, disabled\n");
  1583. /* SEMB is not supported yet */
  1584. *p_class = ATA_DEV_SEMB_UNSUP;
  1585. return 0;
  1586. }
  1587. if ((err_mask == AC_ERR_DEV) && (tf.error & ATA_ABORTED)) {
  1588. /* Device or controller might have reported
  1589. * the wrong device class. Give a shot at the
  1590. * other IDENTIFY if the current one is
  1591. * aborted by the device.
  1592. */
  1593. if (may_fallback) {
  1594. may_fallback = 0;
  1595. if (class == ATA_DEV_ATA)
  1596. class = ATA_DEV_ATAPI;
  1597. else
  1598. class = ATA_DEV_ATA;
  1599. goto retry;
  1600. }
  1601. /* Control reaches here iff the device aborted
  1602. * both flavors of IDENTIFYs which happens
  1603. * sometimes with phantom devices.
  1604. */
  1605. ata_dev_dbg(dev,
  1606. "both IDENTIFYs aborted, assuming NODEV\n");
  1607. return -ENOENT;
  1608. }
  1609. rc = -EIO;
  1610. reason = "I/O error";
  1611. goto err_out;
  1612. }
  1613. if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
  1614. ata_dev_info(dev, "dumping IDENTIFY data, "
  1615. "class=%d may_fallback=%d tried_spinup=%d\n",
  1616. class, may_fallback, tried_spinup);
  1617. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
  1618. 16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
  1619. }
  1620. /* Falling back doesn't make sense if ID data was read
  1621. * successfully at least once.
  1622. */
  1623. may_fallback = 0;
  1624. swap_buf_le16(id, ATA_ID_WORDS);
  1625. /* sanity check */
  1626. rc = -EINVAL;
  1627. reason = "device reports invalid type";
  1628. if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
  1629. if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
  1630. goto err_out;
  1631. if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
  1632. ata_id_is_ata(id)) {
  1633. ata_dev_dbg(dev,
  1634. "host indicates ignore ATA devices, ignored\n");
  1635. return -ENOENT;
  1636. }
  1637. } else {
  1638. if (ata_id_is_ata(id))
  1639. goto err_out;
  1640. }
  1641. if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
  1642. tried_spinup = 1;
  1643. /*
  1644. * Drive powered-up in standby mode, and requires a specific
  1645. * SET_FEATURES spin-up subcommand before it will accept
  1646. * anything other than the original IDENTIFY command.
  1647. */
  1648. err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
  1649. if (err_mask && id[2] != 0x738c) {
  1650. rc = -EIO;
  1651. reason = "SPINUP failed";
  1652. goto err_out;
  1653. }
  1654. /*
  1655. * If the drive initially returned incomplete IDENTIFY info,
  1656. * we now must reissue the IDENTIFY command.
  1657. */
  1658. if (id[2] == 0x37c8)
  1659. goto retry;
  1660. }
  1661. if ((flags & ATA_READID_POSTRESET) &&
  1662. (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
  1663. /*
  1664. * The exact sequence expected by certain pre-ATA4 drives is:
  1665. * SRST RESET
  1666. * IDENTIFY (optional in early ATA)
  1667. * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
  1668. * anything else..
  1669. * Some drives were very specific about that exact sequence.
  1670. *
  1671. * Note that ATA4 says lba is mandatory so the second check
  1672. * should never trigger.
  1673. */
  1674. if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
  1675. err_mask = ata_dev_init_params(dev, id[3], id[6]);
  1676. if (err_mask) {
  1677. rc = -EIO;
  1678. reason = "INIT_DEV_PARAMS failed";
  1679. goto err_out;
  1680. }
  1681. /* current CHS translation info (id[53-58]) might be
  1682. * changed. reread the identify device info.
  1683. */
  1684. flags &= ~ATA_READID_POSTRESET;
  1685. goto retry;
  1686. }
  1687. }
  1688. *p_class = class;
  1689. return 0;
  1690. err_out:
  1691. ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
  1692. reason, err_mask);
  1693. return rc;
  1694. }
  1695. /**
  1696. * ata_dev_power_set_standby - Set a device power mode to standby
  1697. * @dev: target device
  1698. *
  1699. * Issue a STANDBY IMMEDIATE command to set a device power mode to standby.
  1700. * For an HDD device, this spins down the disks.
  1701. *
  1702. * LOCKING:
  1703. * Kernel thread context (may sleep).
  1704. */
  1705. void ata_dev_power_set_standby(struct ata_device *dev)
  1706. {
  1707. unsigned long ap_flags = dev->link->ap->flags;
  1708. struct ata_taskfile tf;
  1709. unsigned int err_mask;
  1710. /* Issue STANDBY IMMEDIATE command only if supported by the device */
  1711. if (dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC)
  1712. return;
  1713. /*
  1714. * Some odd clown BIOSes issue spindown on power off (ACPI S4 or S5)
  1715. * causing some drives to spin up and down again. For these, do nothing
  1716. * if we are being called on shutdown.
  1717. */
  1718. if ((ap_flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
  1719. system_state == SYSTEM_POWER_OFF)
  1720. return;
  1721. if ((ap_flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
  1722. system_entering_hibernation())
  1723. return;
  1724. ata_tf_init(dev, &tf);
  1725. tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  1726. tf.protocol = ATA_PROT_NODATA;
  1727. tf.command = ATA_CMD_STANDBYNOW1;
  1728. ata_dev_notice(dev, "Entering standby power mode\n");
  1729. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  1730. if (err_mask)
  1731. ata_dev_err(dev, "STANDBY IMMEDIATE failed (err_mask=0x%x)\n",
  1732. err_mask);
  1733. }
  1734. /**
  1735. * ata_dev_power_set_active - Set a device power mode to active
  1736. * @dev: target device
  1737. *
  1738. * Issue a VERIFY command to enter to ensure that the device is in the
  1739. * active power mode. For a spun-down HDD (standby or idle power mode),
  1740. * the VERIFY command will complete after the disk spins up.
  1741. *
  1742. * LOCKING:
  1743. * Kernel thread context (may sleep).
  1744. */
  1745. void ata_dev_power_set_active(struct ata_device *dev)
  1746. {
  1747. struct ata_taskfile tf;
  1748. unsigned int err_mask;
  1749. /*
  1750. * Issue READ VERIFY SECTORS command for 1 sector at lba=0 only
  1751. * if supported by the device.
  1752. */
  1753. if (dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC)
  1754. return;
  1755. ata_tf_init(dev, &tf);
  1756. tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  1757. tf.protocol = ATA_PROT_NODATA;
  1758. tf.command = ATA_CMD_VERIFY;
  1759. tf.nsect = 1;
  1760. if (dev->flags & ATA_DFLAG_LBA) {
  1761. tf.flags |= ATA_TFLAG_LBA;
  1762. tf.device |= ATA_LBA;
  1763. } else {
  1764. /* CHS */
  1765. tf.lbal = 0x1; /* sect */
  1766. }
  1767. ata_dev_notice(dev, "Entering active power mode\n");
  1768. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  1769. if (err_mask)
  1770. ata_dev_err(dev, "VERIFY failed (err_mask=0x%x)\n",
  1771. err_mask);
  1772. }
  1773. /**
  1774. * ata_read_log_page - read a specific log page
  1775. * @dev: target device
  1776. * @log: log to read
  1777. * @page: page to read
  1778. * @buf: buffer to store read page
  1779. * @sectors: number of sectors to read
  1780. *
  1781. * Read log page using READ_LOG_EXT command.
  1782. *
  1783. * LOCKING:
  1784. * Kernel thread context (may sleep).
  1785. *
  1786. * RETURNS:
  1787. * 0 on success, AC_ERR_* mask otherwise.
  1788. */
  1789. unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
  1790. u8 page, void *buf, unsigned int sectors)
  1791. {
  1792. unsigned long ap_flags = dev->link->ap->flags;
  1793. struct ata_taskfile tf;
  1794. unsigned int err_mask;
  1795. bool dma = false;
  1796. ata_dev_dbg(dev, "read log page - log 0x%x, page 0x%x\n", log, page);
  1797. /*
  1798. * Return error without actually issuing the command on controllers
  1799. * which e.g. lockup on a read log page.
  1800. */
  1801. if (ap_flags & ATA_FLAG_NO_LOG_PAGE)
  1802. return AC_ERR_DEV;
  1803. retry:
  1804. ata_tf_init(dev, &tf);
  1805. if (ata_dma_enabled(dev) && ata_id_has_read_log_dma_ext(dev->id) &&
  1806. !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) {
  1807. tf.command = ATA_CMD_READ_LOG_DMA_EXT;
  1808. tf.protocol = ATA_PROT_DMA;
  1809. dma = true;
  1810. } else {
  1811. tf.command = ATA_CMD_READ_LOG_EXT;
  1812. tf.protocol = ATA_PROT_PIO;
  1813. dma = false;
  1814. }
  1815. tf.lbal = log;
  1816. tf.lbam = page;
  1817. tf.nsect = sectors;
  1818. tf.hob_nsect = sectors >> 8;
  1819. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
  1820. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
  1821. buf, sectors * ATA_SECT_SIZE, 0);
  1822. if (err_mask) {
  1823. if (dma) {
  1824. dev->horkage |= ATA_HORKAGE_NO_DMA_LOG;
  1825. goto retry;
  1826. }
  1827. ata_dev_err(dev,
  1828. "Read log 0x%02x page 0x%02x failed, Emask 0x%x\n",
  1829. (unsigned int)log, (unsigned int)page, err_mask);
  1830. }
  1831. return err_mask;
  1832. }
  1833. static int ata_log_supported(struct ata_device *dev, u8 log)
  1834. {
  1835. struct ata_port *ap = dev->link->ap;
  1836. if (dev->horkage & ATA_HORKAGE_NO_LOG_DIR)
  1837. return 0;
  1838. if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1))
  1839. return 0;
  1840. return get_unaligned_le16(&ap->sector_buf[log * 2]);
  1841. }
  1842. static bool ata_identify_page_supported(struct ata_device *dev, u8 page)
  1843. {
  1844. struct ata_port *ap = dev->link->ap;
  1845. unsigned int err, i;
  1846. if (dev->horkage & ATA_HORKAGE_NO_ID_DEV_LOG)
  1847. return false;
  1848. if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) {
  1849. /*
  1850. * IDENTIFY DEVICE data log is defined as mandatory starting
  1851. * with ACS-3 (ATA version 10). Warn about the missing log
  1852. * for drives which implement this ATA level or above.
  1853. */
  1854. if (ata_id_major_version(dev->id) >= 10)
  1855. ata_dev_warn(dev,
  1856. "ATA Identify Device Log not supported\n");
  1857. dev->horkage |= ATA_HORKAGE_NO_ID_DEV_LOG;
  1858. return false;
  1859. }
  1860. /*
  1861. * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is
  1862. * supported.
  1863. */
  1864. err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf,
  1865. 1);
  1866. if (err)
  1867. return false;
  1868. for (i = 0; i < ap->sector_buf[8]; i++) {
  1869. if (ap->sector_buf[9 + i] == page)
  1870. return true;
  1871. }
  1872. return false;
  1873. }
  1874. static int ata_do_link_spd_horkage(struct ata_device *dev)
  1875. {
  1876. struct ata_link *plink = ata_dev_phys_link(dev);
  1877. u32 target, target_limit;
  1878. if (!sata_scr_valid(plink))
  1879. return 0;
  1880. if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
  1881. target = 1;
  1882. else
  1883. return 0;
  1884. target_limit = (1 << target) - 1;
  1885. /* if already on stricter limit, no need to push further */
  1886. if (plink->sata_spd_limit <= target_limit)
  1887. return 0;
  1888. plink->sata_spd_limit = target_limit;
  1889. /* Request another EH round by returning -EAGAIN if link is
  1890. * going faster than the target speed. Forward progress is
  1891. * guaranteed by setting sata_spd_limit to target_limit above.
  1892. */
  1893. if (plink->sata_spd > target) {
  1894. ata_dev_info(dev, "applying link speed limit horkage to %s\n",
  1895. sata_spd_string(target));
  1896. return -EAGAIN;
  1897. }
  1898. return 0;
  1899. }
  1900. static inline u8 ata_dev_knobble(struct ata_device *dev)
  1901. {
  1902. struct ata_port *ap = dev->link->ap;
  1903. if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
  1904. return 0;
  1905. return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
  1906. }
  1907. static void ata_dev_config_ncq_send_recv(struct ata_device *dev)
  1908. {
  1909. struct ata_port *ap = dev->link->ap;
  1910. unsigned int err_mask;
  1911. if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) {
  1912. ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n");
  1913. return;
  1914. }
  1915. err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
  1916. 0, ap->sector_buf, 1);
  1917. if (!err_mask) {
  1918. u8 *cmds = dev->ncq_send_recv_cmds;
  1919. dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
  1920. memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
  1921. if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
  1922. ata_dev_dbg(dev, "disabling queued TRIM support\n");
  1923. cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
  1924. ~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
  1925. }
  1926. }
  1927. }
  1928. static void ata_dev_config_ncq_non_data(struct ata_device *dev)
  1929. {
  1930. struct ata_port *ap = dev->link->ap;
  1931. unsigned int err_mask;
  1932. if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) {
  1933. ata_dev_warn(dev,
  1934. "NCQ Send/Recv Log not supported\n");
  1935. return;
  1936. }
  1937. err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
  1938. 0, ap->sector_buf, 1);
  1939. if (!err_mask) {
  1940. u8 *cmds = dev->ncq_non_data_cmds;
  1941. memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
  1942. }
  1943. }
  1944. static void ata_dev_config_ncq_prio(struct ata_device *dev)
  1945. {
  1946. struct ata_port *ap = dev->link->ap;
  1947. unsigned int err_mask;
  1948. if (!ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS))
  1949. return;
  1950. err_mask = ata_read_log_page(dev,
  1951. ATA_LOG_IDENTIFY_DEVICE,
  1952. ATA_LOG_SATA_SETTINGS,
  1953. ap->sector_buf,
  1954. 1);
  1955. if (err_mask)
  1956. goto not_supported;
  1957. if (!(ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)))
  1958. goto not_supported;
  1959. dev->flags |= ATA_DFLAG_NCQ_PRIO;
  1960. return;
  1961. not_supported:
  1962. dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLED;
  1963. dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
  1964. }
  1965. static bool ata_dev_check_adapter(struct ata_device *dev,
  1966. unsigned short vendor_id)
  1967. {
  1968. struct pci_dev *pcidev = NULL;
  1969. struct device *parent_dev = NULL;
  1970. for (parent_dev = dev->tdev.parent; parent_dev != NULL;
  1971. parent_dev = parent_dev->parent) {
  1972. if (dev_is_pci(parent_dev)) {
  1973. pcidev = to_pci_dev(parent_dev);
  1974. if (pcidev->vendor == vendor_id)
  1975. return true;
  1976. break;
  1977. }
  1978. }
  1979. return false;
  1980. }
  1981. static int ata_dev_config_ncq(struct ata_device *dev,
  1982. char *desc, size_t desc_sz)
  1983. {
  1984. struct ata_port *ap = dev->link->ap;
  1985. int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
  1986. unsigned int err_mask;
  1987. char *aa_desc = "";
  1988. if (!ata_id_has_ncq(dev->id)) {
  1989. desc[0] = '\0';
  1990. return 0;
  1991. }
  1992. if (!IS_ENABLED(CONFIG_SATA_HOST))
  1993. return 0;
  1994. if (dev->horkage & ATA_HORKAGE_NONCQ) {
  1995. snprintf(desc, desc_sz, "NCQ (not used)");
  1996. return 0;
  1997. }
  1998. if (dev->horkage & ATA_HORKAGE_NO_NCQ_ON_ATI &&
  1999. ata_dev_check_adapter(dev, PCI_VENDOR_ID_ATI)) {
  2000. snprintf(desc, desc_sz, "NCQ (not used)");
  2001. return 0;
  2002. }
  2003. if (ap->flags & ATA_FLAG_NCQ) {
  2004. hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE);
  2005. dev->flags |= ATA_DFLAG_NCQ;
  2006. }
  2007. if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
  2008. (ap->flags & ATA_FLAG_FPDMA_AA) &&
  2009. ata_id_has_fpdma_aa(dev->id)) {
  2010. err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
  2011. SATA_FPDMA_AA);
  2012. if (err_mask) {
  2013. ata_dev_err(dev,
  2014. "failed to enable AA (error_mask=0x%x)\n",
  2015. err_mask);
  2016. if (err_mask != AC_ERR_DEV) {
  2017. dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
  2018. return -EIO;
  2019. }
  2020. } else
  2021. aa_desc = ", AA";
  2022. }
  2023. if (hdepth >= ddepth)
  2024. snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
  2025. else
  2026. snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
  2027. ddepth, aa_desc);
  2028. if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
  2029. if (ata_id_has_ncq_send_and_recv(dev->id))
  2030. ata_dev_config_ncq_send_recv(dev);
  2031. if (ata_id_has_ncq_non_data(dev->id))
  2032. ata_dev_config_ncq_non_data(dev);
  2033. if (ata_id_has_ncq_prio(dev->id))
  2034. ata_dev_config_ncq_prio(dev);
  2035. }
  2036. return 0;
  2037. }
  2038. static void ata_dev_config_sense_reporting(struct ata_device *dev)
  2039. {
  2040. unsigned int err_mask;
  2041. if (!ata_id_has_sense_reporting(dev->id))
  2042. return;
  2043. if (ata_id_sense_reporting_enabled(dev->id))
  2044. return;
  2045. err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
  2046. if (err_mask) {
  2047. ata_dev_dbg(dev,
  2048. "failed to enable Sense Data Reporting, Emask 0x%x\n",
  2049. err_mask);
  2050. }
  2051. }
  2052. static void ata_dev_config_zac(struct ata_device *dev)
  2053. {
  2054. struct ata_port *ap = dev->link->ap;
  2055. unsigned int err_mask;
  2056. u8 *identify_buf = ap->sector_buf;
  2057. dev->zac_zones_optimal_open = U32_MAX;
  2058. dev->zac_zones_optimal_nonseq = U32_MAX;
  2059. dev->zac_zones_max_open = U32_MAX;
  2060. /*
  2061. * Always set the 'ZAC' flag for Host-managed devices.
  2062. */
  2063. if (dev->class == ATA_DEV_ZAC)
  2064. dev->flags |= ATA_DFLAG_ZAC;
  2065. else if (ata_id_zoned_cap(dev->id) == 0x01)
  2066. /*
  2067. * Check for host-aware devices.
  2068. */
  2069. dev->flags |= ATA_DFLAG_ZAC;
  2070. if (!(dev->flags & ATA_DFLAG_ZAC))
  2071. return;
  2072. if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) {
  2073. ata_dev_warn(dev,
  2074. "ATA Zoned Information Log not supported\n");
  2075. return;
  2076. }
  2077. /*
  2078. * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
  2079. */
  2080. err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
  2081. ATA_LOG_ZONED_INFORMATION,
  2082. identify_buf, 1);
  2083. if (!err_mask) {
  2084. u64 zoned_cap, opt_open, opt_nonseq, max_open;
  2085. zoned_cap = get_unaligned_le64(&identify_buf[8]);
  2086. if ((zoned_cap >> 63))
  2087. dev->zac_zoned_cap = (zoned_cap & 1);
  2088. opt_open = get_unaligned_le64(&identify_buf[24]);
  2089. if ((opt_open >> 63))
  2090. dev->zac_zones_optimal_open = (u32)opt_open;
  2091. opt_nonseq = get_unaligned_le64(&identify_buf[32]);
  2092. if ((opt_nonseq >> 63))
  2093. dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
  2094. max_open = get_unaligned_le64(&identify_buf[40]);
  2095. if ((max_open >> 63))
  2096. dev->zac_zones_max_open = (u32)max_open;
  2097. }
  2098. }
  2099. static void ata_dev_config_trusted(struct ata_device *dev)
  2100. {
  2101. struct ata_port *ap = dev->link->ap;
  2102. u64 trusted_cap;
  2103. unsigned int err;
  2104. if (!ata_id_has_trusted(dev->id))
  2105. return;
  2106. if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) {
  2107. ata_dev_warn(dev,
  2108. "Security Log not supported\n");
  2109. return;
  2110. }
  2111. err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY,
  2112. ap->sector_buf, 1);
  2113. if (err)
  2114. return;
  2115. trusted_cap = get_unaligned_le64(&ap->sector_buf[40]);
  2116. if (!(trusted_cap & (1ULL << 63))) {
  2117. ata_dev_dbg(dev,
  2118. "Trusted Computing capability qword not valid!\n");
  2119. return;
  2120. }
  2121. if (trusted_cap & (1 << 0))
  2122. dev->flags |= ATA_DFLAG_TRUSTED;
  2123. }
  2124. static int ata_dev_config_lba(struct ata_device *dev)
  2125. {
  2126. const u16 *id = dev->id;
  2127. const char *lba_desc;
  2128. char ncq_desc[32];
  2129. int ret;
  2130. dev->flags |= ATA_DFLAG_LBA;
  2131. if (ata_id_has_lba48(id)) {
  2132. lba_desc = "LBA48";
  2133. dev->flags |= ATA_DFLAG_LBA48;
  2134. if (dev->n_sectors >= (1UL << 28) &&
  2135. ata_id_has_flush_ext(id))
  2136. dev->flags |= ATA_DFLAG_FLUSH_EXT;
  2137. } else {
  2138. lba_desc = "LBA";
  2139. }
  2140. /* config NCQ */
  2141. ret = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
  2142. /* print device info to dmesg */
  2143. if (ata_dev_print_info(dev))
  2144. ata_dev_info(dev,
  2145. "%llu sectors, multi %u: %s %s\n",
  2146. (unsigned long long)dev->n_sectors,
  2147. dev->multi_count, lba_desc, ncq_desc);
  2148. return ret;
  2149. }
  2150. static void ata_dev_config_chs(struct ata_device *dev)
  2151. {
  2152. const u16 *id = dev->id;
  2153. if (ata_id_current_chs_valid(id)) {
  2154. /* Current CHS translation is valid. */
  2155. dev->cylinders = id[54];
  2156. dev->heads = id[55];
  2157. dev->sectors = id[56];
  2158. } else {
  2159. /* Default translation */
  2160. dev->cylinders = id[1];
  2161. dev->heads = id[3];
  2162. dev->sectors = id[6];
  2163. }
  2164. /* print device info to dmesg */
  2165. if (ata_dev_print_info(dev))
  2166. ata_dev_info(dev,
  2167. "%llu sectors, multi %u, CHS %u/%u/%u\n",
  2168. (unsigned long long)dev->n_sectors,
  2169. dev->multi_count, dev->cylinders,
  2170. dev->heads, dev->sectors);
  2171. }
  2172. static void ata_dev_config_devslp(struct ata_device *dev)
  2173. {
  2174. u8 *sata_setting = dev->link->ap->sector_buf;
  2175. unsigned int err_mask;
  2176. int i, j;
  2177. /*
  2178. * Check device sleep capability. Get DevSlp timing variables
  2179. * from SATA Settings page of Identify Device Data Log.
  2180. */
  2181. if (!ata_id_has_devslp(dev->id) ||
  2182. !ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS))
  2183. return;
  2184. err_mask = ata_read_log_page(dev,
  2185. ATA_LOG_IDENTIFY_DEVICE,
  2186. ATA_LOG_SATA_SETTINGS,
  2187. sata_setting, 1);
  2188. if (err_mask)
  2189. return;
  2190. dev->flags |= ATA_DFLAG_DEVSLP;
  2191. for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
  2192. j = ATA_LOG_DEVSLP_OFFSET + i;
  2193. dev->devslp_timing[i] = sata_setting[j];
  2194. }
  2195. }
  2196. static void ata_dev_config_cpr(struct ata_device *dev)
  2197. {
  2198. unsigned int err_mask;
  2199. size_t buf_len;
  2200. int i, nr_cpr = 0;
  2201. struct ata_cpr_log *cpr_log = NULL;
  2202. u8 *desc, *buf = NULL;
  2203. if (ata_id_major_version(dev->id) < 11)
  2204. goto out;
  2205. buf_len = ata_log_supported(dev, ATA_LOG_CONCURRENT_POSITIONING_RANGES);
  2206. if (buf_len == 0)
  2207. goto out;
  2208. /*
  2209. * Read the concurrent positioning ranges log (0x47). We can have at
  2210. * most 255 32B range descriptors plus a 64B header. This log varies in
  2211. * size, so use the size reported in the GPL directory. Reading beyond
  2212. * the supported length will result in an error.
  2213. */
  2214. buf_len <<= 9;
  2215. buf = kzalloc(buf_len, GFP_KERNEL);
  2216. if (!buf)
  2217. goto out;
  2218. err_mask = ata_read_log_page(dev, ATA_LOG_CONCURRENT_POSITIONING_RANGES,
  2219. 0, buf, buf_len >> 9);
  2220. if (err_mask)
  2221. goto out;
  2222. nr_cpr = buf[0];
  2223. if (!nr_cpr)
  2224. goto out;
  2225. cpr_log = kzalloc(struct_size(cpr_log, cpr, nr_cpr), GFP_KERNEL);
  2226. if (!cpr_log)
  2227. goto out;
  2228. cpr_log->nr_cpr = nr_cpr;
  2229. desc = &buf[64];
  2230. for (i = 0; i < nr_cpr; i++, desc += 32) {
  2231. cpr_log->cpr[i].num = desc[0];
  2232. cpr_log->cpr[i].num_storage_elements = desc[1];
  2233. cpr_log->cpr[i].start_lba = get_unaligned_le64(&desc[8]);
  2234. cpr_log->cpr[i].num_lbas = get_unaligned_le64(&desc[16]);
  2235. }
  2236. out:
  2237. swap(dev->cpr_log, cpr_log);
  2238. kfree(cpr_log);
  2239. kfree(buf);
  2240. }
  2241. static void ata_dev_print_features(struct ata_device *dev)
  2242. {
  2243. if (!(dev->flags & ATA_DFLAG_FEATURES_MASK))
  2244. return;
  2245. ata_dev_info(dev,
  2246. "Features:%s%s%s%s%s%s\n",
  2247. dev->flags & ATA_DFLAG_TRUSTED ? " Trust" : "",
  2248. dev->flags & ATA_DFLAG_DA ? " Dev-Attention" : "",
  2249. dev->flags & ATA_DFLAG_DEVSLP ? " Dev-Sleep" : "",
  2250. dev->flags & ATA_DFLAG_NCQ_SEND_RECV ? " NCQ-sndrcv" : "",
  2251. dev->flags & ATA_DFLAG_NCQ_PRIO ? " NCQ-prio" : "",
  2252. dev->cpr_log ? " CPR" : "");
  2253. }
  2254. /**
  2255. * ata_dev_configure - Configure the specified ATA/ATAPI device
  2256. * @dev: Target device to configure
  2257. *
  2258. * Configure @dev according to @dev->id. Generic and low-level
  2259. * driver specific fixups are also applied.
  2260. *
  2261. * LOCKING:
  2262. * Kernel thread context (may sleep)
  2263. *
  2264. * RETURNS:
  2265. * 0 on success, -errno otherwise
  2266. */
  2267. int ata_dev_configure(struct ata_device *dev)
  2268. {
  2269. struct ata_port *ap = dev->link->ap;
  2270. bool print_info = ata_dev_print_info(dev);
  2271. const u16 *id = dev->id;
  2272. unsigned int xfer_mask;
  2273. unsigned int err_mask;
  2274. char revbuf[7]; /* XYZ-99\0 */
  2275. char fwrevbuf[ATA_ID_FW_REV_LEN+1];
  2276. char modelbuf[ATA_ID_PROD_LEN+1];
  2277. int rc;
  2278. if (!ata_dev_enabled(dev)) {
  2279. ata_dev_dbg(dev, "no device\n");
  2280. return 0;
  2281. }
  2282. /* set horkage */
  2283. dev->horkage |= ata_dev_blacklisted(dev);
  2284. ata_force_horkage(dev);
  2285. if (dev->horkage & ATA_HORKAGE_DISABLE) {
  2286. ata_dev_info(dev, "unsupported device, disabling\n");
  2287. ata_dev_disable(dev);
  2288. return 0;
  2289. }
  2290. if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
  2291. dev->class == ATA_DEV_ATAPI) {
  2292. ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
  2293. atapi_enabled ? "not supported with this driver"
  2294. : "disabled");
  2295. ata_dev_disable(dev);
  2296. return 0;
  2297. }
  2298. rc = ata_do_link_spd_horkage(dev);
  2299. if (rc)
  2300. return rc;
  2301. /* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
  2302. if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
  2303. (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
  2304. dev->horkage |= ATA_HORKAGE_NOLPM;
  2305. if (ap->flags & ATA_FLAG_NO_LPM)
  2306. dev->horkage |= ATA_HORKAGE_NOLPM;
  2307. if (dev->horkage & ATA_HORKAGE_NOLPM) {
  2308. ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
  2309. dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
  2310. }
  2311. /* let ACPI work its magic */
  2312. rc = ata_acpi_on_devcfg(dev);
  2313. if (rc)
  2314. return rc;
  2315. /* massage HPA, do it early as it might change IDENTIFY data */
  2316. rc = ata_hpa_resize(dev);
  2317. if (rc)
  2318. return rc;
  2319. /* print device capabilities */
  2320. ata_dev_dbg(dev,
  2321. "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
  2322. "85:%04x 86:%04x 87:%04x 88:%04x\n",
  2323. __func__,
  2324. id[49], id[82], id[83], id[84],
  2325. id[85], id[86], id[87], id[88]);
  2326. /* initialize to-be-configured parameters */
  2327. dev->flags &= ~ATA_DFLAG_CFG_MASK;
  2328. dev->max_sectors = 0;
  2329. dev->cdb_len = 0;
  2330. dev->n_sectors = 0;
  2331. dev->cylinders = 0;
  2332. dev->heads = 0;
  2333. dev->sectors = 0;
  2334. dev->multi_count = 0;
  2335. /*
  2336. * common ATA, ATAPI feature tests
  2337. */
  2338. /* find max transfer mode; for printk only */
  2339. xfer_mask = ata_id_xfermask(id);
  2340. ata_dump_id(dev, id);
  2341. /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
  2342. ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
  2343. sizeof(fwrevbuf));
  2344. ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
  2345. sizeof(modelbuf));
  2346. /* ATA-specific feature tests */
  2347. if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
  2348. if (ata_id_is_cfa(id)) {
  2349. /* CPRM may make this media unusable */
  2350. if (id[ATA_ID_CFA_KEY_MGMT] & 1)
  2351. ata_dev_warn(dev,
  2352. "supports DRM functions and may not be fully accessible\n");
  2353. snprintf(revbuf, 7, "CFA");
  2354. } else {
  2355. snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
  2356. /* Warn the user if the device has TPM extensions */
  2357. if (ata_id_has_tpm(id))
  2358. ata_dev_warn(dev,
  2359. "supports DRM functions and may not be fully accessible\n");
  2360. }
  2361. dev->n_sectors = ata_id_n_sectors(id);
  2362. /* get current R/W Multiple count setting */
  2363. if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
  2364. unsigned int max = dev->id[47] & 0xff;
  2365. unsigned int cnt = dev->id[59] & 0xff;
  2366. /* only recognize/allow powers of two here */
  2367. if (is_power_of_2(max) && is_power_of_2(cnt))
  2368. if (cnt <= max)
  2369. dev->multi_count = cnt;
  2370. }
  2371. /* print device info to dmesg */
  2372. if (print_info)
  2373. ata_dev_info(dev, "%s: %s, %s, max %s\n",
  2374. revbuf, modelbuf, fwrevbuf,
  2375. ata_mode_string(xfer_mask));
  2376. if (ata_id_has_lba(id)) {
  2377. rc = ata_dev_config_lba(dev);
  2378. if (rc)
  2379. return rc;
  2380. } else {
  2381. ata_dev_config_chs(dev);
  2382. }
  2383. ata_dev_config_devslp(dev);
  2384. ata_dev_config_sense_reporting(dev);
  2385. ata_dev_config_zac(dev);
  2386. ata_dev_config_trusted(dev);
  2387. ata_dev_config_cpr(dev);
  2388. dev->cdb_len = 32;
  2389. if (print_info)
  2390. ata_dev_print_features(dev);
  2391. }
  2392. /* ATAPI-specific feature tests */
  2393. else if (dev->class == ATA_DEV_ATAPI) {
  2394. const char *cdb_intr_string = "";
  2395. const char *atapi_an_string = "";
  2396. const char *dma_dir_string = "";
  2397. u32 sntf;
  2398. rc = atapi_cdb_len(id);
  2399. if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
  2400. ata_dev_warn(dev, "unsupported CDB len %d\n", rc);
  2401. rc = -EINVAL;
  2402. goto err_out_nosup;
  2403. }
  2404. dev->cdb_len = (unsigned int) rc;
  2405. /* Enable ATAPI AN if both the host and device have
  2406. * the support. If PMP is attached, SNTF is required
  2407. * to enable ATAPI AN to discern between PHY status
  2408. * changed notifications and ATAPI ANs.
  2409. */
  2410. if (atapi_an &&
  2411. (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
  2412. (!sata_pmp_attached(ap) ||
  2413. sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
  2414. /* issue SET feature command to turn this on */
  2415. err_mask = ata_dev_set_feature(dev,
  2416. SETFEATURES_SATA_ENABLE, SATA_AN);
  2417. if (err_mask)
  2418. ata_dev_err(dev,
  2419. "failed to enable ATAPI AN (err_mask=0x%x)\n",
  2420. err_mask);
  2421. else {
  2422. dev->flags |= ATA_DFLAG_AN;
  2423. atapi_an_string = ", ATAPI AN";
  2424. }
  2425. }
  2426. if (ata_id_cdb_intr(dev->id)) {
  2427. dev->flags |= ATA_DFLAG_CDB_INTR;
  2428. cdb_intr_string = ", CDB intr";
  2429. }
  2430. if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
  2431. dev->flags |= ATA_DFLAG_DMADIR;
  2432. dma_dir_string = ", DMADIR";
  2433. }
  2434. if (ata_id_has_da(dev->id)) {
  2435. dev->flags |= ATA_DFLAG_DA;
  2436. zpodd_init(dev);
  2437. }
  2438. /* print device info to dmesg */
  2439. if (print_info)
  2440. ata_dev_info(dev,
  2441. "ATAPI: %s, %s, max %s%s%s%s\n",
  2442. modelbuf, fwrevbuf,
  2443. ata_mode_string(xfer_mask),
  2444. cdb_intr_string, atapi_an_string,
  2445. dma_dir_string);
  2446. }
  2447. /* determine max_sectors */
  2448. dev->max_sectors = ATA_MAX_SECTORS;
  2449. if (dev->flags & ATA_DFLAG_LBA48)
  2450. dev->max_sectors = ATA_MAX_SECTORS_LBA48;
  2451. /* Limit PATA drive on SATA cable bridge transfers to udma5,
  2452. 200 sectors */
  2453. if (ata_dev_knobble(dev)) {
  2454. if (print_info)
  2455. ata_dev_info(dev, "applying bridge limits\n");
  2456. dev->udma_mask &= ATA_UDMA5;
  2457. dev->max_sectors = ATA_MAX_SECTORS;
  2458. }
  2459. if ((dev->class == ATA_DEV_ATAPI) &&
  2460. (atapi_command_packet_set(id) == TYPE_TAPE)) {
  2461. dev->max_sectors = ATA_MAX_SECTORS_TAPE;
  2462. dev->horkage |= ATA_HORKAGE_STUCK_ERR;
  2463. }
  2464. if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
  2465. dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
  2466. dev->max_sectors);
  2467. if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
  2468. dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
  2469. dev->max_sectors);
  2470. if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
  2471. dev->max_sectors = ATA_MAX_SECTORS_LBA48;
  2472. if (ap->ops->dev_config)
  2473. ap->ops->dev_config(dev);
  2474. if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
  2475. /* Let the user know. We don't want to disallow opens for
  2476. rescue purposes, or in case the vendor is just a blithering
  2477. idiot. Do this after the dev_config call as some controllers
  2478. with buggy firmware may want to avoid reporting false device
  2479. bugs */
  2480. if (print_info) {
  2481. ata_dev_warn(dev,
  2482. "Drive reports diagnostics failure. This may indicate a drive\n");
  2483. ata_dev_warn(dev,
  2484. "fault or invalid emulation. Contact drive vendor for information.\n");
  2485. }
  2486. }
  2487. if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
  2488. ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
  2489. ata_dev_warn(dev, " contact the vendor or visit http://ata.wiki.kernel.org\n");
  2490. }
  2491. return 0;
  2492. err_out_nosup:
  2493. return rc;
  2494. }
  2495. /**
  2496. * ata_cable_40wire - return 40 wire cable type
  2497. * @ap: port
  2498. *
  2499. * Helper method for drivers which want to hardwire 40 wire cable
  2500. * detection.
  2501. */
  2502. int ata_cable_40wire(struct ata_port *ap)
  2503. {
  2504. return ATA_CBL_PATA40;
  2505. }
  2506. EXPORT_SYMBOL_GPL(ata_cable_40wire);
  2507. /**
  2508. * ata_cable_80wire - return 80 wire cable type
  2509. * @ap: port
  2510. *
  2511. * Helper method for drivers which want to hardwire 80 wire cable
  2512. * detection.
  2513. */
  2514. int ata_cable_80wire(struct ata_port *ap)
  2515. {
  2516. return ATA_CBL_PATA80;
  2517. }
  2518. EXPORT_SYMBOL_GPL(ata_cable_80wire);
  2519. /**
  2520. * ata_cable_unknown - return unknown PATA cable.
  2521. * @ap: port
  2522. *
  2523. * Helper method for drivers which have no PATA cable detection.
  2524. */
  2525. int ata_cable_unknown(struct ata_port *ap)
  2526. {
  2527. return ATA_CBL_PATA_UNK;
  2528. }
  2529. EXPORT_SYMBOL_GPL(ata_cable_unknown);
  2530. /**
  2531. * ata_cable_ignore - return ignored PATA cable.
  2532. * @ap: port
  2533. *
  2534. * Helper method for drivers which don't use cable type to limit
  2535. * transfer mode.
  2536. */
  2537. int ata_cable_ignore(struct ata_port *ap)
  2538. {
  2539. return ATA_CBL_PATA_IGN;
  2540. }
  2541. EXPORT_SYMBOL_GPL(ata_cable_ignore);
  2542. /**
  2543. * ata_cable_sata - return SATA cable type
  2544. * @ap: port
  2545. *
  2546. * Helper method for drivers which have SATA cables
  2547. */
  2548. int ata_cable_sata(struct ata_port *ap)
  2549. {
  2550. return ATA_CBL_SATA;
  2551. }
  2552. EXPORT_SYMBOL_GPL(ata_cable_sata);
  2553. /**
  2554. * ata_bus_probe - Reset and probe ATA bus
  2555. * @ap: Bus to probe
  2556. *
  2557. * Master ATA bus probing function. Initiates a hardware-dependent
  2558. * bus reset, then attempts to identify any devices found on
  2559. * the bus.
  2560. *
  2561. * LOCKING:
  2562. * PCI/etc. bus probe sem.
  2563. *
  2564. * RETURNS:
  2565. * Zero on success, negative errno otherwise.
  2566. */
  2567. int ata_bus_probe(struct ata_port *ap)
  2568. {
  2569. unsigned int classes[ATA_MAX_DEVICES];
  2570. int tries[ATA_MAX_DEVICES];
  2571. int rc;
  2572. struct ata_device *dev;
  2573. ata_for_each_dev(dev, &ap->link, ALL)
  2574. tries[dev->devno] = ATA_PROBE_MAX_TRIES;
  2575. retry:
  2576. ata_for_each_dev(dev, &ap->link, ALL) {
  2577. /* If we issue an SRST then an ATA drive (not ATAPI)
  2578. * may change configuration and be in PIO0 timing. If
  2579. * we do a hard reset (or are coming from power on)
  2580. * this is true for ATA or ATAPI. Until we've set a
  2581. * suitable controller mode we should not touch the
  2582. * bus as we may be talking too fast.
  2583. */
  2584. dev->pio_mode = XFER_PIO_0;
  2585. dev->dma_mode = 0xff;
  2586. /* If the controller has a pio mode setup function
  2587. * then use it to set the chipset to rights. Don't
  2588. * touch the DMA setup as that will be dealt with when
  2589. * configuring devices.
  2590. */
  2591. if (ap->ops->set_piomode)
  2592. ap->ops->set_piomode(ap, dev);
  2593. }
  2594. /* reset and determine device classes */
  2595. ap->ops->phy_reset(ap);
  2596. ata_for_each_dev(dev, &ap->link, ALL) {
  2597. if (dev->class != ATA_DEV_UNKNOWN)
  2598. classes[dev->devno] = dev->class;
  2599. else
  2600. classes[dev->devno] = ATA_DEV_NONE;
  2601. dev->class = ATA_DEV_UNKNOWN;
  2602. }
  2603. /* read IDENTIFY page and configure devices. We have to do the identify
  2604. specific sequence bass-ackwards so that PDIAG- is released by
  2605. the slave device */
  2606. ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
  2607. if (tries[dev->devno])
  2608. dev->class = classes[dev->devno];
  2609. if (!ata_dev_enabled(dev))
  2610. continue;
  2611. rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
  2612. dev->id);
  2613. if (rc)
  2614. goto fail;
  2615. }
  2616. /* Now ask for the cable type as PDIAG- should have been released */
  2617. if (ap->ops->cable_detect)
  2618. ap->cbl = ap->ops->cable_detect(ap);
  2619. /* We may have SATA bridge glue hiding here irrespective of
  2620. * the reported cable types and sensed types. When SATA
  2621. * drives indicate we have a bridge, we don't know which end
  2622. * of the link the bridge is which is a problem.
  2623. */
  2624. ata_for_each_dev(dev, &ap->link, ENABLED)
  2625. if (ata_id_is_sata(dev->id))
  2626. ap->cbl = ATA_CBL_SATA;
  2627. /* After the identify sequence we can now set up the devices. We do
  2628. this in the normal order so that the user doesn't get confused */
  2629. ata_for_each_dev(dev, &ap->link, ENABLED) {
  2630. ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
  2631. rc = ata_dev_configure(dev);
  2632. ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
  2633. if (rc)
  2634. goto fail;
  2635. }
  2636. /* configure transfer mode */
  2637. rc = ata_set_mode(&ap->link, &dev);
  2638. if (rc)
  2639. goto fail;
  2640. ata_for_each_dev(dev, &ap->link, ENABLED)
  2641. return 0;
  2642. return -ENODEV;
  2643. fail:
  2644. tries[dev->devno]--;
  2645. switch (rc) {
  2646. case -EINVAL:
  2647. /* eeek, something went very wrong, give up */
  2648. tries[dev->devno] = 0;
  2649. break;
  2650. case -ENODEV:
  2651. /* give it just one more chance */
  2652. tries[dev->devno] = min(tries[dev->devno], 1);
  2653. fallthrough;
  2654. case -EIO:
  2655. if (tries[dev->devno] == 1) {
  2656. /* This is the last chance, better to slow
  2657. * down than lose it.
  2658. */
  2659. sata_down_spd_limit(&ap->link, 0);
  2660. ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
  2661. }
  2662. }
  2663. if (!tries[dev->devno])
  2664. ata_dev_disable(dev);
  2665. goto retry;
  2666. }
  2667. /**
  2668. * sata_print_link_status - Print SATA link status
  2669. * @link: SATA link to printk link status about
  2670. *
  2671. * This function prints link speed and status of a SATA link.
  2672. *
  2673. * LOCKING:
  2674. * None.
  2675. */
  2676. static void sata_print_link_status(struct ata_link *link)
  2677. {
  2678. u32 sstatus, scontrol, tmp;
  2679. if (sata_scr_read(link, SCR_STATUS, &sstatus))
  2680. return;
  2681. if (sata_scr_read(link, SCR_CONTROL, &scontrol))
  2682. return;
  2683. if (ata_phys_link_online(link)) {
  2684. tmp = (sstatus >> 4) & 0xf;
  2685. ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
  2686. sata_spd_string(tmp), sstatus, scontrol);
  2687. } else {
  2688. ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
  2689. sstatus, scontrol);
  2690. }
  2691. }
  2692. /**
  2693. * ata_dev_pair - return other device on cable
  2694. * @adev: device
  2695. *
  2696. * Obtain the other device on the same cable, or if none is
  2697. * present NULL is returned
  2698. */
  2699. struct ata_device *ata_dev_pair(struct ata_device *adev)
  2700. {
  2701. struct ata_link *link = adev->link;
  2702. struct ata_device *pair = &link->device[1 - adev->devno];
  2703. if (!ata_dev_enabled(pair))
  2704. return NULL;
  2705. return pair;
  2706. }
  2707. EXPORT_SYMBOL_GPL(ata_dev_pair);
  2708. /**
  2709. * sata_down_spd_limit - adjust SATA spd limit downward
  2710. * @link: Link to adjust SATA spd limit for
  2711. * @spd_limit: Additional limit
  2712. *
  2713. * Adjust SATA spd limit of @link downward. Note that this
  2714. * function only adjusts the limit. The change must be applied
  2715. * using sata_set_spd().
  2716. *
  2717. * If @spd_limit is non-zero, the speed is limited to equal to or
  2718. * lower than @spd_limit if such speed is supported. If
  2719. * @spd_limit is slower than any supported speed, only the lowest
  2720. * supported speed is allowed.
  2721. *
  2722. * LOCKING:
  2723. * Inherited from caller.
  2724. *
  2725. * RETURNS:
  2726. * 0 on success, negative errno on failure
  2727. */
  2728. int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
  2729. {
  2730. u32 sstatus, spd, mask;
  2731. int rc, bit;
  2732. if (!sata_scr_valid(link))
  2733. return -EOPNOTSUPP;
  2734. /* If SCR can be read, use it to determine the current SPD.
  2735. * If not, use cached value in link->sata_spd.
  2736. */
  2737. rc = sata_scr_read(link, SCR_STATUS, &sstatus);
  2738. if (rc == 0 && ata_sstatus_online(sstatus))
  2739. spd = (sstatus >> 4) & 0xf;
  2740. else
  2741. spd = link->sata_spd;
  2742. mask = link->sata_spd_limit;
  2743. if (mask <= 1)
  2744. return -EINVAL;
  2745. /* unconditionally mask off the highest bit */
  2746. bit = fls(mask) - 1;
  2747. mask &= ~(1 << bit);
  2748. /*
  2749. * Mask off all speeds higher than or equal to the current one. At
  2750. * this point, if current SPD is not available and we previously
  2751. * recorded the link speed from SStatus, the driver has already
  2752. * masked off the highest bit so mask should already be 1 or 0.
  2753. * Otherwise, we should not force 1.5Gbps on a link where we have
  2754. * not previously recorded speed from SStatus. Just return in this
  2755. * case.
  2756. */
  2757. if (spd > 1)
  2758. mask &= (1 << (spd - 1)) - 1;
  2759. else if (link->sata_spd)
  2760. return -EINVAL;
  2761. /* were we already at the bottom? */
  2762. if (!mask)
  2763. return -EINVAL;
  2764. if (spd_limit) {
  2765. if (mask & ((1 << spd_limit) - 1))
  2766. mask &= (1 << spd_limit) - 1;
  2767. else {
  2768. bit = ffs(mask) - 1;
  2769. mask = 1 << bit;
  2770. }
  2771. }
  2772. link->sata_spd_limit = mask;
  2773. ata_link_warn(link, "limiting SATA link speed to %s\n",
  2774. sata_spd_string(fls(mask)));
  2775. return 0;
  2776. }
  2777. #ifdef CONFIG_ATA_ACPI
  2778. /**
  2779. * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
  2780. * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
  2781. * @cycle: cycle duration in ns
  2782. *
  2783. * Return matching xfer mode for @cycle. The returned mode is of
  2784. * the transfer type specified by @xfer_shift. If @cycle is too
  2785. * slow for @xfer_shift, 0xff is returned. If @cycle is faster
  2786. * than the fastest known mode, the fasted mode is returned.
  2787. *
  2788. * LOCKING:
  2789. * None.
  2790. *
  2791. * RETURNS:
  2792. * Matching xfer_mode, 0xff if no match found.
  2793. */
  2794. u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
  2795. {
  2796. u8 base_mode = 0xff, last_mode = 0xff;
  2797. const struct ata_xfer_ent *ent;
  2798. const struct ata_timing *t;
  2799. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  2800. if (ent->shift == xfer_shift)
  2801. base_mode = ent->base;
  2802. for (t = ata_timing_find_mode(base_mode);
  2803. t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
  2804. unsigned short this_cycle;
  2805. switch (xfer_shift) {
  2806. case ATA_SHIFT_PIO:
  2807. case ATA_SHIFT_MWDMA:
  2808. this_cycle = t->cycle;
  2809. break;
  2810. case ATA_SHIFT_UDMA:
  2811. this_cycle = t->udma;
  2812. break;
  2813. default:
  2814. return 0xff;
  2815. }
  2816. if (cycle > this_cycle)
  2817. break;
  2818. last_mode = t->mode;
  2819. }
  2820. return last_mode;
  2821. }
  2822. #endif
  2823. /**
  2824. * ata_down_xfermask_limit - adjust dev xfer masks downward
  2825. * @dev: Device to adjust xfer masks
  2826. * @sel: ATA_DNXFER_* selector
  2827. *
  2828. * Adjust xfer masks of @dev downward. Note that this function
  2829. * does not apply the change. Invoking ata_set_mode() afterwards
  2830. * will apply the limit.
  2831. *
  2832. * LOCKING:
  2833. * Inherited from caller.
  2834. *
  2835. * RETURNS:
  2836. * 0 on success, negative errno on failure
  2837. */
  2838. int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
  2839. {
  2840. char buf[32];
  2841. unsigned int orig_mask, xfer_mask;
  2842. unsigned int pio_mask, mwdma_mask, udma_mask;
  2843. int quiet, highbit;
  2844. quiet = !!(sel & ATA_DNXFER_QUIET);
  2845. sel &= ~ATA_DNXFER_QUIET;
  2846. xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
  2847. dev->mwdma_mask,
  2848. dev->udma_mask);
  2849. ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
  2850. switch (sel) {
  2851. case ATA_DNXFER_PIO:
  2852. highbit = fls(pio_mask) - 1;
  2853. pio_mask &= ~(1 << highbit);
  2854. break;
  2855. case ATA_DNXFER_DMA:
  2856. if (udma_mask) {
  2857. highbit = fls(udma_mask) - 1;
  2858. udma_mask &= ~(1 << highbit);
  2859. if (!udma_mask)
  2860. return -ENOENT;
  2861. } else if (mwdma_mask) {
  2862. highbit = fls(mwdma_mask) - 1;
  2863. mwdma_mask &= ~(1 << highbit);
  2864. if (!mwdma_mask)
  2865. return -ENOENT;
  2866. }
  2867. break;
  2868. case ATA_DNXFER_40C:
  2869. udma_mask &= ATA_UDMA_MASK_40C;
  2870. break;
  2871. case ATA_DNXFER_FORCE_PIO0:
  2872. pio_mask &= 1;
  2873. fallthrough;
  2874. case ATA_DNXFER_FORCE_PIO:
  2875. mwdma_mask = 0;
  2876. udma_mask = 0;
  2877. break;
  2878. default:
  2879. BUG();
  2880. }
  2881. xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
  2882. if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
  2883. return -ENOENT;
  2884. if (!quiet) {
  2885. if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
  2886. snprintf(buf, sizeof(buf), "%s:%s",
  2887. ata_mode_string(xfer_mask),
  2888. ata_mode_string(xfer_mask & ATA_MASK_PIO));
  2889. else
  2890. snprintf(buf, sizeof(buf), "%s",
  2891. ata_mode_string(xfer_mask));
  2892. ata_dev_warn(dev, "limiting speed to %s\n", buf);
  2893. }
  2894. ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
  2895. &dev->udma_mask);
  2896. return 0;
  2897. }
  2898. static int ata_dev_set_mode(struct ata_device *dev)
  2899. {
  2900. struct ata_port *ap = dev->link->ap;
  2901. struct ata_eh_context *ehc = &dev->link->eh_context;
  2902. const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
  2903. const char *dev_err_whine = "";
  2904. int ign_dev_err = 0;
  2905. unsigned int err_mask = 0;
  2906. int rc;
  2907. dev->flags &= ~ATA_DFLAG_PIO;
  2908. if (dev->xfer_shift == ATA_SHIFT_PIO)
  2909. dev->flags |= ATA_DFLAG_PIO;
  2910. if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
  2911. dev_err_whine = " (SET_XFERMODE skipped)";
  2912. else {
  2913. if (nosetxfer)
  2914. ata_dev_warn(dev,
  2915. "NOSETXFER but PATA detected - can't "
  2916. "skip SETXFER, might malfunction\n");
  2917. err_mask = ata_dev_set_xfermode(dev);
  2918. }
  2919. if (err_mask & ~AC_ERR_DEV)
  2920. goto fail;
  2921. /* revalidate */
  2922. ehc->i.flags |= ATA_EHI_POST_SETMODE;
  2923. rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
  2924. ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
  2925. if (rc)
  2926. return rc;
  2927. if (dev->xfer_shift == ATA_SHIFT_PIO) {
  2928. /* Old CFA may refuse this command, which is just fine */
  2929. if (ata_id_is_cfa(dev->id))
  2930. ign_dev_err = 1;
  2931. /* Catch several broken garbage emulations plus some pre
  2932. ATA devices */
  2933. if (ata_id_major_version(dev->id) == 0 &&
  2934. dev->pio_mode <= XFER_PIO_2)
  2935. ign_dev_err = 1;
  2936. /* Some very old devices and some bad newer ones fail
  2937. any kind of SET_XFERMODE request but support PIO0-2
  2938. timings and no IORDY */
  2939. if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
  2940. ign_dev_err = 1;
  2941. }
  2942. /* Early MWDMA devices do DMA but don't allow DMA mode setting.
  2943. Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
  2944. if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
  2945. dev->dma_mode == XFER_MW_DMA_0 &&
  2946. (dev->id[63] >> 8) & 1)
  2947. ign_dev_err = 1;
  2948. /* if the device is actually configured correctly, ignore dev err */
  2949. if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
  2950. ign_dev_err = 1;
  2951. if (err_mask & AC_ERR_DEV) {
  2952. if (!ign_dev_err)
  2953. goto fail;
  2954. else
  2955. dev_err_whine = " (device error ignored)";
  2956. }
  2957. ata_dev_dbg(dev, "xfer_shift=%u, xfer_mode=0x%x\n",
  2958. dev->xfer_shift, (int)dev->xfer_mode);
  2959. if (!(ehc->i.flags & ATA_EHI_QUIET) ||
  2960. ehc->i.flags & ATA_EHI_DID_HARDRESET)
  2961. ata_dev_info(dev, "configured for %s%s\n",
  2962. ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
  2963. dev_err_whine);
  2964. return 0;
  2965. fail:
  2966. ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
  2967. return -EIO;
  2968. }
  2969. /**
  2970. * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
  2971. * @link: link on which timings will be programmed
  2972. * @r_failed_dev: out parameter for failed device
  2973. *
  2974. * Standard implementation of the function used to tune and set
  2975. * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
  2976. * ata_dev_set_mode() fails, pointer to the failing device is
  2977. * returned in @r_failed_dev.
  2978. *
  2979. * LOCKING:
  2980. * PCI/etc. bus probe sem.
  2981. *
  2982. * RETURNS:
  2983. * 0 on success, negative errno otherwise
  2984. */
  2985. int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
  2986. {
  2987. struct ata_port *ap = link->ap;
  2988. struct ata_device *dev;
  2989. int rc = 0, used_dma = 0, found = 0;
  2990. /* step 1: calculate xfer_mask */
  2991. ata_for_each_dev(dev, link, ENABLED) {
  2992. unsigned int pio_mask, dma_mask;
  2993. unsigned int mode_mask;
  2994. mode_mask = ATA_DMA_MASK_ATA;
  2995. if (dev->class == ATA_DEV_ATAPI)
  2996. mode_mask = ATA_DMA_MASK_ATAPI;
  2997. else if (ata_id_is_cfa(dev->id))
  2998. mode_mask = ATA_DMA_MASK_CFA;
  2999. ata_dev_xfermask(dev);
  3000. ata_force_xfermask(dev);
  3001. pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
  3002. if (libata_dma_mask & mode_mask)
  3003. dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
  3004. dev->udma_mask);
  3005. else
  3006. dma_mask = 0;
  3007. dev->pio_mode = ata_xfer_mask2mode(pio_mask);
  3008. dev->dma_mode = ata_xfer_mask2mode(dma_mask);
  3009. found = 1;
  3010. if (ata_dma_enabled(dev))
  3011. used_dma = 1;
  3012. }
  3013. if (!found)
  3014. goto out;
  3015. /* step 2: always set host PIO timings */
  3016. ata_for_each_dev(dev, link, ENABLED) {
  3017. if (dev->pio_mode == 0xff) {
  3018. ata_dev_warn(dev, "no PIO support\n");
  3019. rc = -EINVAL;
  3020. goto out;
  3021. }
  3022. dev->xfer_mode = dev->pio_mode;
  3023. dev->xfer_shift = ATA_SHIFT_PIO;
  3024. if (ap->ops->set_piomode)
  3025. ap->ops->set_piomode(ap, dev);
  3026. }
  3027. /* step 3: set host DMA timings */
  3028. ata_for_each_dev(dev, link, ENABLED) {
  3029. if (!ata_dma_enabled(dev))
  3030. continue;
  3031. dev->xfer_mode = dev->dma_mode;
  3032. dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
  3033. if (ap->ops->set_dmamode)
  3034. ap->ops->set_dmamode(ap, dev);
  3035. }
  3036. /* step 4: update devices' xfer mode */
  3037. ata_for_each_dev(dev, link, ENABLED) {
  3038. rc = ata_dev_set_mode(dev);
  3039. if (rc)
  3040. goto out;
  3041. }
  3042. /* Record simplex status. If we selected DMA then the other
  3043. * host channels are not permitted to do so.
  3044. */
  3045. if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
  3046. ap->host->simplex_claimed = ap;
  3047. out:
  3048. if (rc)
  3049. *r_failed_dev = dev;
  3050. return rc;
  3051. }
  3052. EXPORT_SYMBOL_GPL(ata_do_set_mode);
  3053. /**
  3054. * ata_wait_ready - wait for link to become ready
  3055. * @link: link to be waited on
  3056. * @deadline: deadline jiffies for the operation
  3057. * @check_ready: callback to check link readiness
  3058. *
  3059. * Wait for @link to become ready. @check_ready should return
  3060. * positive number if @link is ready, 0 if it isn't, -ENODEV if
  3061. * link doesn't seem to be occupied, other errno for other error
  3062. * conditions.
  3063. *
  3064. * Transient -ENODEV conditions are allowed for
  3065. * ATA_TMOUT_FF_WAIT.
  3066. *
  3067. * LOCKING:
  3068. * EH context.
  3069. *
  3070. * RETURNS:
  3071. * 0 if @link is ready before @deadline; otherwise, -errno.
  3072. */
  3073. int ata_wait_ready(struct ata_link *link, unsigned long deadline,
  3074. int (*check_ready)(struct ata_link *link))
  3075. {
  3076. unsigned long start = jiffies;
  3077. unsigned long nodev_deadline;
  3078. int warned = 0;
  3079. /* choose which 0xff timeout to use, read comment in libata.h */
  3080. if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
  3081. nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
  3082. else
  3083. nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
  3084. /* Slave readiness can't be tested separately from master. On
  3085. * M/S emulation configuration, this function should be called
  3086. * only on the master and it will handle both master and slave.
  3087. */
  3088. WARN_ON(link == link->ap->slave_link);
  3089. if (time_after(nodev_deadline, deadline))
  3090. nodev_deadline = deadline;
  3091. while (1) {
  3092. unsigned long now = jiffies;
  3093. int ready, tmp;
  3094. ready = tmp = check_ready(link);
  3095. if (ready > 0)
  3096. return 0;
  3097. /*
  3098. * -ENODEV could be transient. Ignore -ENODEV if link
  3099. * is online. Also, some SATA devices take a long
  3100. * time to clear 0xff after reset. Wait for
  3101. * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
  3102. * offline.
  3103. *
  3104. * Note that some PATA controllers (pata_ali) explode
  3105. * if status register is read more than once when
  3106. * there's no device attached.
  3107. */
  3108. if (ready == -ENODEV) {
  3109. if (ata_link_online(link))
  3110. ready = 0;
  3111. else if ((link->ap->flags & ATA_FLAG_SATA) &&
  3112. !ata_link_offline(link) &&
  3113. time_before(now, nodev_deadline))
  3114. ready = 0;
  3115. }
  3116. if (ready)
  3117. return ready;
  3118. if (time_after(now, deadline))
  3119. return -EBUSY;
  3120. if (!warned && time_after(now, start + 5 * HZ) &&
  3121. (deadline - now > 3 * HZ)) {
  3122. ata_link_warn(link,
  3123. "link is slow to respond, please be patient "
  3124. "(ready=%d)\n", tmp);
  3125. warned = 1;
  3126. }
  3127. ata_msleep(link->ap, 50);
  3128. }
  3129. }
  3130. /**
  3131. * ata_wait_after_reset - wait for link to become ready after reset
  3132. * @link: link to be waited on
  3133. * @deadline: deadline jiffies for the operation
  3134. * @check_ready: callback to check link readiness
  3135. *
  3136. * Wait for @link to become ready after reset.
  3137. *
  3138. * LOCKING:
  3139. * EH context.
  3140. *
  3141. * RETURNS:
  3142. * 0 if @link is ready before @deadline; otherwise, -errno.
  3143. */
  3144. int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
  3145. int (*check_ready)(struct ata_link *link))
  3146. {
  3147. ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
  3148. return ata_wait_ready(link, deadline, check_ready);
  3149. }
  3150. EXPORT_SYMBOL_GPL(ata_wait_after_reset);
  3151. /**
  3152. * ata_std_prereset - prepare for reset
  3153. * @link: ATA link to be reset
  3154. * @deadline: deadline jiffies for the operation
  3155. *
  3156. * @link is about to be reset. Initialize it. Failure from
  3157. * prereset makes libata abort whole reset sequence and give up
  3158. * that port, so prereset should be best-effort. It does its
  3159. * best to prepare for reset sequence but if things go wrong, it
  3160. * should just whine, not fail.
  3161. *
  3162. * LOCKING:
  3163. * Kernel thread context (may sleep)
  3164. *
  3165. * RETURNS:
  3166. * Always 0.
  3167. */
  3168. int ata_std_prereset(struct ata_link *link, unsigned long deadline)
  3169. {
  3170. struct ata_port *ap = link->ap;
  3171. struct ata_eh_context *ehc = &link->eh_context;
  3172. const unsigned long *timing = sata_ehc_deb_timing(ehc);
  3173. int rc;
  3174. /* if we're about to do hardreset, nothing more to do */
  3175. if (ehc->i.action & ATA_EH_HARDRESET)
  3176. return 0;
  3177. /* if SATA, resume link */
  3178. if (ap->flags & ATA_FLAG_SATA) {
  3179. rc = sata_link_resume(link, timing, deadline);
  3180. /* whine about phy resume failure but proceed */
  3181. if (rc && rc != -EOPNOTSUPP)
  3182. ata_link_warn(link,
  3183. "failed to resume link for reset (errno=%d)\n",
  3184. rc);
  3185. }
  3186. /* no point in trying softreset on offline link */
  3187. if (ata_phys_link_offline(link))
  3188. ehc->i.action &= ~ATA_EH_SOFTRESET;
  3189. return 0;
  3190. }
  3191. EXPORT_SYMBOL_GPL(ata_std_prereset);
  3192. /**
  3193. * sata_std_hardreset - COMRESET w/o waiting or classification
  3194. * @link: link to reset
  3195. * @class: resulting class of attached device
  3196. * @deadline: deadline jiffies for the operation
  3197. *
  3198. * Standard SATA COMRESET w/o waiting or classification.
  3199. *
  3200. * LOCKING:
  3201. * Kernel thread context (may sleep)
  3202. *
  3203. * RETURNS:
  3204. * 0 if link offline, -EAGAIN if link online, -errno on errors.
  3205. */
  3206. int sata_std_hardreset(struct ata_link *link, unsigned int *class,
  3207. unsigned long deadline)
  3208. {
  3209. const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
  3210. bool online;
  3211. int rc;
  3212. /* do hardreset */
  3213. rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
  3214. return online ? -EAGAIN : rc;
  3215. }
  3216. EXPORT_SYMBOL_GPL(sata_std_hardreset);
  3217. /**
  3218. * ata_std_postreset - standard postreset callback
  3219. * @link: the target ata_link
  3220. * @classes: classes of attached devices
  3221. *
  3222. * This function is invoked after a successful reset. Note that
  3223. * the device might have been reset more than once using
  3224. * different reset methods before postreset is invoked.
  3225. *
  3226. * LOCKING:
  3227. * Kernel thread context (may sleep)
  3228. */
  3229. void ata_std_postreset(struct ata_link *link, unsigned int *classes)
  3230. {
  3231. u32 serror;
  3232. /* reset complete, clear SError */
  3233. if (!sata_scr_read(link, SCR_ERROR, &serror))
  3234. sata_scr_write(link, SCR_ERROR, serror);
  3235. /* print link status */
  3236. sata_print_link_status(link);
  3237. }
  3238. EXPORT_SYMBOL_GPL(ata_std_postreset);
  3239. /**
  3240. * ata_dev_same_device - Determine whether new ID matches configured device
  3241. * @dev: device to compare against
  3242. * @new_class: class of the new device
  3243. * @new_id: IDENTIFY page of the new device
  3244. *
  3245. * Compare @new_class and @new_id against @dev and determine
  3246. * whether @dev is the device indicated by @new_class and
  3247. * @new_id.
  3248. *
  3249. * LOCKING:
  3250. * None.
  3251. *
  3252. * RETURNS:
  3253. * 1 if @dev matches @new_class and @new_id, 0 otherwise.
  3254. */
  3255. static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
  3256. const u16 *new_id)
  3257. {
  3258. const u16 *old_id = dev->id;
  3259. unsigned char model[2][ATA_ID_PROD_LEN + 1];
  3260. unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
  3261. if (dev->class != new_class) {
  3262. ata_dev_info(dev, "class mismatch %d != %d\n",
  3263. dev->class, new_class);
  3264. return 0;
  3265. }
  3266. ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
  3267. ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
  3268. ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
  3269. ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
  3270. if (strcmp(model[0], model[1])) {
  3271. ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
  3272. model[0], model[1]);
  3273. return 0;
  3274. }
  3275. if (strcmp(serial[0], serial[1])) {
  3276. ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
  3277. serial[0], serial[1]);
  3278. return 0;
  3279. }
  3280. return 1;
  3281. }
  3282. /**
  3283. * ata_dev_reread_id - Re-read IDENTIFY data
  3284. * @dev: target ATA device
  3285. * @readid_flags: read ID flags
  3286. *
  3287. * Re-read IDENTIFY page and make sure @dev is still attached to
  3288. * the port.
  3289. *
  3290. * LOCKING:
  3291. * Kernel thread context (may sleep)
  3292. *
  3293. * RETURNS:
  3294. * 0 on success, negative errno otherwise
  3295. */
  3296. int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
  3297. {
  3298. unsigned int class = dev->class;
  3299. u16 *id = (void *)dev->link->ap->sector_buf;
  3300. int rc;
  3301. /* read ID data */
  3302. rc = ata_dev_read_id(dev, &class, readid_flags, id);
  3303. if (rc)
  3304. return rc;
  3305. /* is the device still there? */
  3306. if (!ata_dev_same_device(dev, class, id))
  3307. return -ENODEV;
  3308. memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
  3309. return 0;
  3310. }
  3311. /**
  3312. * ata_dev_revalidate - Revalidate ATA device
  3313. * @dev: device to revalidate
  3314. * @new_class: new class code
  3315. * @readid_flags: read ID flags
  3316. *
  3317. * Re-read IDENTIFY page, make sure @dev is still attached to the
  3318. * port and reconfigure it according to the new IDENTIFY page.
  3319. *
  3320. * LOCKING:
  3321. * Kernel thread context (may sleep)
  3322. *
  3323. * RETURNS:
  3324. * 0 on success, negative errno otherwise
  3325. */
  3326. int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
  3327. unsigned int readid_flags)
  3328. {
  3329. u64 n_sectors = dev->n_sectors;
  3330. u64 n_native_sectors = dev->n_native_sectors;
  3331. int rc;
  3332. if (!ata_dev_enabled(dev))
  3333. return -ENODEV;
  3334. /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
  3335. if (ata_class_enabled(new_class) &&
  3336. new_class != ATA_DEV_ATA &&
  3337. new_class != ATA_DEV_ATAPI &&
  3338. new_class != ATA_DEV_ZAC &&
  3339. new_class != ATA_DEV_SEMB) {
  3340. ata_dev_info(dev, "class mismatch %u != %u\n",
  3341. dev->class, new_class);
  3342. rc = -ENODEV;
  3343. goto fail;
  3344. }
  3345. /* re-read ID */
  3346. rc = ata_dev_reread_id(dev, readid_flags);
  3347. if (rc)
  3348. goto fail;
  3349. /* configure device according to the new ID */
  3350. rc = ata_dev_configure(dev);
  3351. if (rc)
  3352. goto fail;
  3353. /* verify n_sectors hasn't changed */
  3354. if (dev->class != ATA_DEV_ATA || !n_sectors ||
  3355. dev->n_sectors == n_sectors)
  3356. return 0;
  3357. /* n_sectors has changed */
  3358. ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
  3359. (unsigned long long)n_sectors,
  3360. (unsigned long long)dev->n_sectors);
  3361. /*
  3362. * Something could have caused HPA to be unlocked
  3363. * involuntarily. If n_native_sectors hasn't changed and the
  3364. * new size matches it, keep the device.
  3365. */
  3366. if (dev->n_native_sectors == n_native_sectors &&
  3367. dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
  3368. ata_dev_warn(dev,
  3369. "new n_sectors matches native, probably "
  3370. "late HPA unlock, n_sectors updated\n");
  3371. /* use the larger n_sectors */
  3372. return 0;
  3373. }
  3374. /*
  3375. * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try
  3376. * unlocking HPA in those cases.
  3377. *
  3378. * https://bugzilla.kernel.org/show_bug.cgi?id=15396
  3379. */
  3380. if (dev->n_native_sectors == n_native_sectors &&
  3381. dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
  3382. !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
  3383. ata_dev_warn(dev,
  3384. "old n_sectors matches native, probably "
  3385. "late HPA lock, will try to unlock HPA\n");
  3386. /* try unlocking HPA */
  3387. dev->flags |= ATA_DFLAG_UNLOCK_HPA;
  3388. rc = -EIO;
  3389. } else
  3390. rc = -ENODEV;
  3391. /* restore original n_[native_]sectors and fail */
  3392. dev->n_native_sectors = n_native_sectors;
  3393. dev->n_sectors = n_sectors;
  3394. fail:
  3395. ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
  3396. return rc;
  3397. }
  3398. struct ata_blacklist_entry {
  3399. const char *model_num;
  3400. const char *model_rev;
  3401. unsigned long horkage;
  3402. };
  3403. static const struct ata_blacklist_entry ata_device_blacklist [] = {
  3404. /* Devices with DMA related problems under Linux */
  3405. { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
  3406. { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
  3407. { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
  3408. { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
  3409. { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
  3410. { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
  3411. { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
  3412. { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
  3413. { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
  3414. { "CRD-848[02]B", NULL, ATA_HORKAGE_NODMA },
  3415. { "CRD-84", NULL, ATA_HORKAGE_NODMA },
  3416. { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
  3417. { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
  3418. { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
  3419. { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
  3420. { "HITACHI CDR-8[34]35",NULL, ATA_HORKAGE_NODMA },
  3421. { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
  3422. { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
  3423. { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
  3424. { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
  3425. { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
  3426. { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
  3427. { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
  3428. { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
  3429. { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
  3430. { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
  3431. { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
  3432. { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
  3433. { " 2GB ATA Flash Disk", "ADMA428M", ATA_HORKAGE_NODMA },
  3434. { "VRFDFC22048UCHC-TE*", NULL, ATA_HORKAGE_NODMA },
  3435. /* Odd clown on sil3726/4726 PMPs */
  3436. { "Config Disk", NULL, ATA_HORKAGE_DISABLE },
  3437. /* Similar story with ASMedia 1092 */
  3438. { "ASMT109x- Config", NULL, ATA_HORKAGE_DISABLE },
  3439. /* Weird ATAPI devices */
  3440. { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
  3441. { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA },
  3442. { "Slimtype DVD A DS8A8SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
  3443. { "Slimtype DVD A DS8A9SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
  3444. /*
  3445. * Causes silent data corruption with higher max sects.
  3446. * http://lkml.kernel.org/g/[email protected]
  3447. */
  3448. { "ST380013AS", "3.20", ATA_HORKAGE_MAX_SEC_1024 },
  3449. /*
  3450. * These devices time out with higher max sects.
  3451. * https://bugzilla.kernel.org/show_bug.cgi?id=121671
  3452. */
  3453. { "LITEON CX1-JB*-HP", NULL, ATA_HORKAGE_MAX_SEC_1024 },
  3454. { "LITEON EP1-*", NULL, ATA_HORKAGE_MAX_SEC_1024 },
  3455. /* Devices we expect to fail diagnostics */
  3456. /* Devices where NCQ should be avoided */
  3457. /* NCQ is slow */
  3458. { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
  3459. { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ },
  3460. /* http://thread.gmane.org/gmane.linux.ide/14907 */
  3461. { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
  3462. /* NCQ is broken */
  3463. { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
  3464. { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
  3465. { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
  3466. { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
  3467. { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ },
  3468. /* Seagate NCQ + FLUSH CACHE firmware bug */
  3469. { "ST31500341AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
  3470. ATA_HORKAGE_FIRMWARE_WARN },
  3471. { "ST31000333AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
  3472. ATA_HORKAGE_FIRMWARE_WARN },
  3473. { "ST3640[36]23AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
  3474. ATA_HORKAGE_FIRMWARE_WARN },
  3475. { "ST3320[68]13AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
  3476. ATA_HORKAGE_FIRMWARE_WARN },
  3477. /* drives which fail FPDMA_AA activation (some may freeze afterwards)
  3478. the ST disks also have LPM issues */
  3479. { "ST1000LM024 HN-M101MBB", NULL, ATA_HORKAGE_BROKEN_FPDMA_AA |
  3480. ATA_HORKAGE_NOLPM },
  3481. { "VB0250EAVER", "HPG7", ATA_HORKAGE_BROKEN_FPDMA_AA },
  3482. /* Blacklist entries taken from Silicon Image 3124/3132
  3483. Windows driver .inf file - also several Linux problem reports */
  3484. { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ },
  3485. { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ },
  3486. { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ },
  3487. /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
  3488. { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ },
  3489. /* Sandisk SD7/8/9s lock up hard on large trims */
  3490. { "SanDisk SD[789]*", NULL, ATA_HORKAGE_MAX_TRIM_128M },
  3491. /* devices which puke on READ_NATIVE_MAX */
  3492. { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA },
  3493. { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
  3494. { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
  3495. { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
  3496. /* this one allows HPA unlocking but fails IOs on the area */
  3497. { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA },
  3498. /* Devices which report 1 sector over size HPA */
  3499. { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE },
  3500. { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE },
  3501. { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE },
  3502. /* Devices which get the IVB wrong */
  3503. { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB },
  3504. /* Maybe we should just blacklist TSSTcorp... */
  3505. { "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]", ATA_HORKAGE_IVB },
  3506. /* Devices that do not need bridging limits applied */
  3507. { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK },
  3508. { "BUFFALO HD-QSU2/R5", NULL, ATA_HORKAGE_BRIDGE_OK },
  3509. /* Devices which aren't very happy with higher link speeds */
  3510. { "WD My Book", NULL, ATA_HORKAGE_1_5_GBPS },
  3511. { "Seagate FreeAgent GoFlex", NULL, ATA_HORKAGE_1_5_GBPS },
  3512. /*
  3513. * Devices which choke on SETXFER. Applies only if both the
  3514. * device and controller are SATA.
  3515. */
  3516. { "PIONEER DVD-RW DVRTD08", NULL, ATA_HORKAGE_NOSETXFER },
  3517. { "PIONEER DVD-RW DVRTD08A", NULL, ATA_HORKAGE_NOSETXFER },
  3518. { "PIONEER DVD-RW DVR-215", NULL, ATA_HORKAGE_NOSETXFER },
  3519. { "PIONEER DVD-RW DVR-212D", NULL, ATA_HORKAGE_NOSETXFER },
  3520. { "PIONEER DVD-RW DVR-216D", NULL, ATA_HORKAGE_NOSETXFER },
  3521. /* These specific Pioneer models have LPM issues */
  3522. { "PIONEER BD-RW BDR-207M", NULL, ATA_HORKAGE_NOLPM },
  3523. { "PIONEER BD-RW BDR-205", NULL, ATA_HORKAGE_NOLPM },
  3524. /* Crucial BX100 SSD 500GB has broken LPM support */
  3525. { "CT500BX100SSD1", NULL, ATA_HORKAGE_NOLPM },
  3526. /* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
  3527. { "Crucial_CT512MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
  3528. ATA_HORKAGE_ZERO_AFTER_TRIM |
  3529. ATA_HORKAGE_NOLPM },
  3530. /* 512GB MX100 with newer firmware has only LPM issues */
  3531. { "Crucial_CT512MX100*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM |
  3532. ATA_HORKAGE_NOLPM },
  3533. /* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
  3534. { "Crucial_CT480M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
  3535. ATA_HORKAGE_ZERO_AFTER_TRIM |
  3536. ATA_HORKAGE_NOLPM },
  3537. { "Crucial_CT960M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
  3538. ATA_HORKAGE_ZERO_AFTER_TRIM |
  3539. ATA_HORKAGE_NOLPM },
  3540. /* These specific Samsung models/firmware-revs do not handle LPM well */
  3541. { "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM },
  3542. { "SAMSUNG SSD PM830 mSATA *", "CXM13D1Q", ATA_HORKAGE_NOLPM },
  3543. { "SAMSUNG MZ7TD256HAFV-000L9", NULL, ATA_HORKAGE_NOLPM },
  3544. { "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM },
  3545. /* devices that don't properly handle queued TRIM commands */
  3546. { "Micron_M500IT_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
  3547. ATA_HORKAGE_ZERO_AFTER_TRIM },
  3548. { "Micron_M500_*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
  3549. ATA_HORKAGE_ZERO_AFTER_TRIM },
  3550. { "Crucial_CT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
  3551. ATA_HORKAGE_ZERO_AFTER_TRIM },
  3552. { "Micron_M5[15]0_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
  3553. ATA_HORKAGE_ZERO_AFTER_TRIM },
  3554. { "Crucial_CT*M550*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
  3555. ATA_HORKAGE_ZERO_AFTER_TRIM },
  3556. { "Crucial_CT*MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
  3557. ATA_HORKAGE_ZERO_AFTER_TRIM },
  3558. { "Samsung SSD 840 EVO*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
  3559. ATA_HORKAGE_NO_DMA_LOG |
  3560. ATA_HORKAGE_ZERO_AFTER_TRIM },
  3561. { "Samsung SSD 840*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
  3562. ATA_HORKAGE_ZERO_AFTER_TRIM },
  3563. { "Samsung SSD 850*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
  3564. ATA_HORKAGE_ZERO_AFTER_TRIM },
  3565. { "Samsung SSD 860*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
  3566. ATA_HORKAGE_ZERO_AFTER_TRIM |
  3567. ATA_HORKAGE_NO_NCQ_ON_ATI },
  3568. { "Samsung SSD 870*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
  3569. ATA_HORKAGE_ZERO_AFTER_TRIM |
  3570. ATA_HORKAGE_NO_NCQ_ON_ATI },
  3571. { "SAMSUNG*MZ7LH*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
  3572. ATA_HORKAGE_ZERO_AFTER_TRIM |
  3573. ATA_HORKAGE_NO_NCQ_ON_ATI, },
  3574. { "FCCT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
  3575. ATA_HORKAGE_ZERO_AFTER_TRIM },
  3576. /* devices that don't properly handle TRIM commands */
  3577. { "SuperSSpeed S238*", NULL, ATA_HORKAGE_NOTRIM },
  3578. { "M88V29*", NULL, ATA_HORKAGE_NOTRIM },
  3579. /*
  3580. * As defined, the DRAT (Deterministic Read After Trim) and RZAT
  3581. * (Return Zero After Trim) flags in the ATA Command Set are
  3582. * unreliable in the sense that they only define what happens if
  3583. * the device successfully executed the DSM TRIM command. TRIM
  3584. * is only advisory, however, and the device is free to silently
  3585. * ignore all or parts of the request.
  3586. *
  3587. * Whitelist drives that are known to reliably return zeroes
  3588. * after TRIM.
  3589. */
  3590. /*
  3591. * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
  3592. * that model before whitelisting all other intel SSDs.
  3593. */
  3594. { "INTEL*SSDSC2MH*", NULL, 0 },
  3595. { "Micron*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
  3596. { "Crucial*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
  3597. { "INTEL*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
  3598. { "SSD*INTEL*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
  3599. { "Samsung*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
  3600. { "SAMSUNG*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
  3601. { "SAMSUNG*MZ7KM*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
  3602. { "ST[1248][0248]0[FH]*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
  3603. /*
  3604. * Some WD SATA-I drives spin up and down erratically when the link
  3605. * is put into the slumber mode. We don't have full list of the
  3606. * affected devices. Disable LPM if the device matches one of the
  3607. * known prefixes and is SATA-1. As a side effect LPM partial is
  3608. * lost too.
  3609. *
  3610. * https://bugzilla.kernel.org/show_bug.cgi?id=57211
  3611. */
  3612. { "WDC WD800JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
  3613. { "WDC WD1200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
  3614. { "WDC WD1600JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
  3615. { "WDC WD2000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
  3616. { "WDC WD2500JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
  3617. { "WDC WD3000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
  3618. { "WDC WD3200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
  3619. /*
  3620. * This sata dom device goes on a walkabout when the ATA_LOG_DIRECTORY
  3621. * log page is accessed. Ensure we never ask for this log page with
  3622. * these devices.
  3623. */
  3624. { "SATADOM-ML 3ME", NULL, ATA_HORKAGE_NO_LOG_DIR },
  3625. /* End Marker */
  3626. { }
  3627. };
  3628. static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
  3629. {
  3630. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  3631. unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
  3632. const struct ata_blacklist_entry *ad = ata_device_blacklist;
  3633. ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
  3634. ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
  3635. while (ad->model_num) {
  3636. if (glob_match(ad->model_num, model_num)) {
  3637. if (ad->model_rev == NULL)
  3638. return ad->horkage;
  3639. if (glob_match(ad->model_rev, model_rev))
  3640. return ad->horkage;
  3641. }
  3642. ad++;
  3643. }
  3644. return 0;
  3645. }
  3646. static int ata_dma_blacklisted(const struct ata_device *dev)
  3647. {
  3648. /* We don't support polling DMA.
  3649. * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
  3650. * if the LLDD handles only interrupts in the HSM_ST_LAST state.
  3651. */
  3652. if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
  3653. (dev->flags & ATA_DFLAG_CDB_INTR))
  3654. return 1;
  3655. return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
  3656. }
  3657. /**
  3658. * ata_is_40wire - check drive side detection
  3659. * @dev: device
  3660. *
  3661. * Perform drive side detection decoding, allowing for device vendors
  3662. * who can't follow the documentation.
  3663. */
  3664. static int ata_is_40wire(struct ata_device *dev)
  3665. {
  3666. if (dev->horkage & ATA_HORKAGE_IVB)
  3667. return ata_drive_40wire_relaxed(dev->id);
  3668. return ata_drive_40wire(dev->id);
  3669. }
  3670. /**
  3671. * cable_is_40wire - 40/80/SATA decider
  3672. * @ap: port to consider
  3673. *
  3674. * This function encapsulates the policy for speed management
  3675. * in one place. At the moment we don't cache the result but
  3676. * there is a good case for setting ap->cbl to the result when
  3677. * we are called with unknown cables (and figuring out if it
  3678. * impacts hotplug at all).
  3679. *
  3680. * Return 1 if the cable appears to be 40 wire.
  3681. */
  3682. static int cable_is_40wire(struct ata_port *ap)
  3683. {
  3684. struct ata_link *link;
  3685. struct ata_device *dev;
  3686. /* If the controller thinks we are 40 wire, we are. */
  3687. if (ap->cbl == ATA_CBL_PATA40)
  3688. return 1;
  3689. /* If the controller thinks we are 80 wire, we are. */
  3690. if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
  3691. return 0;
  3692. /* If the system is known to be 40 wire short cable (eg
  3693. * laptop), then we allow 80 wire modes even if the drive
  3694. * isn't sure.
  3695. */
  3696. if (ap->cbl == ATA_CBL_PATA40_SHORT)
  3697. return 0;
  3698. /* If the controller doesn't know, we scan.
  3699. *
  3700. * Note: We look for all 40 wire detects at this point. Any
  3701. * 80 wire detect is taken to be 80 wire cable because
  3702. * - in many setups only the one drive (slave if present) will
  3703. * give a valid detect
  3704. * - if you have a non detect capable drive you don't want it
  3705. * to colour the choice
  3706. */
  3707. ata_for_each_link(link, ap, EDGE) {
  3708. ata_for_each_dev(dev, link, ENABLED) {
  3709. if (!ata_is_40wire(dev))
  3710. return 0;
  3711. }
  3712. }
  3713. return 1;
  3714. }
  3715. /**
  3716. * ata_dev_xfermask - Compute supported xfermask of the given device
  3717. * @dev: Device to compute xfermask for
  3718. *
  3719. * Compute supported xfermask of @dev and store it in
  3720. * dev->*_mask. This function is responsible for applying all
  3721. * known limits including host controller limits, device
  3722. * blacklist, etc...
  3723. *
  3724. * LOCKING:
  3725. * None.
  3726. */
  3727. static void ata_dev_xfermask(struct ata_device *dev)
  3728. {
  3729. struct ata_link *link = dev->link;
  3730. struct ata_port *ap = link->ap;
  3731. struct ata_host *host = ap->host;
  3732. unsigned int xfer_mask;
  3733. /* controller modes available */
  3734. xfer_mask = ata_pack_xfermask(ap->pio_mask,
  3735. ap->mwdma_mask, ap->udma_mask);
  3736. /* drive modes available */
  3737. xfer_mask &= ata_pack_xfermask(dev->pio_mask,
  3738. dev->mwdma_mask, dev->udma_mask);
  3739. xfer_mask &= ata_id_xfermask(dev->id);
  3740. /*
  3741. * CFA Advanced TrueIDE timings are not allowed on a shared
  3742. * cable
  3743. */
  3744. if (ata_dev_pair(dev)) {
  3745. /* No PIO5 or PIO6 */
  3746. xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
  3747. /* No MWDMA3 or MWDMA 4 */
  3748. xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
  3749. }
  3750. if (ata_dma_blacklisted(dev)) {
  3751. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  3752. ata_dev_warn(dev,
  3753. "device is on DMA blacklist, disabling DMA\n");
  3754. }
  3755. if ((host->flags & ATA_HOST_SIMPLEX) &&
  3756. host->simplex_claimed && host->simplex_claimed != ap) {
  3757. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  3758. ata_dev_warn(dev,
  3759. "simplex DMA is claimed by other device, disabling DMA\n");
  3760. }
  3761. if (ap->flags & ATA_FLAG_NO_IORDY)
  3762. xfer_mask &= ata_pio_mask_no_iordy(dev);
  3763. if (ap->ops->mode_filter)
  3764. xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
  3765. /* Apply cable rule here. Don't apply it early because when
  3766. * we handle hot plug the cable type can itself change.
  3767. * Check this last so that we know if the transfer rate was
  3768. * solely limited by the cable.
  3769. * Unknown or 80 wire cables reported host side are checked
  3770. * drive side as well. Cases where we know a 40wire cable
  3771. * is used safely for 80 are not checked here.
  3772. */
  3773. if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
  3774. /* UDMA/44 or higher would be available */
  3775. if (cable_is_40wire(ap)) {
  3776. ata_dev_warn(dev,
  3777. "limited to UDMA/33 due to 40-wire cable\n");
  3778. xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
  3779. }
  3780. ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
  3781. &dev->mwdma_mask, &dev->udma_mask);
  3782. }
  3783. /**
  3784. * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
  3785. * @dev: Device to which command will be sent
  3786. *
  3787. * Issue SET FEATURES - XFER MODE command to device @dev
  3788. * on port @ap.
  3789. *
  3790. * LOCKING:
  3791. * PCI/etc. bus probe sem.
  3792. *
  3793. * RETURNS:
  3794. * 0 on success, AC_ERR_* mask otherwise.
  3795. */
  3796. static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
  3797. {
  3798. struct ata_taskfile tf;
  3799. /* set up set-features taskfile */
  3800. ata_dev_dbg(dev, "set features - xfer mode\n");
  3801. /* Some controllers and ATAPI devices show flaky interrupt
  3802. * behavior after setting xfer mode. Use polling instead.
  3803. */
  3804. ata_tf_init(dev, &tf);
  3805. tf.command = ATA_CMD_SET_FEATURES;
  3806. tf.feature = SETFEATURES_XFER;
  3807. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
  3808. tf.protocol = ATA_PROT_NODATA;
  3809. /* If we are using IORDY we must send the mode setting command */
  3810. if (ata_pio_need_iordy(dev))
  3811. tf.nsect = dev->xfer_mode;
  3812. /* If the device has IORDY and the controller does not - turn it off */
  3813. else if (ata_id_has_iordy(dev->id))
  3814. tf.nsect = 0x01;
  3815. else /* In the ancient relic department - skip all of this */
  3816. return 0;
  3817. /*
  3818. * On some disks, this command causes spin-up, so we need longer
  3819. * timeout.
  3820. */
  3821. return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
  3822. }
  3823. /**
  3824. * ata_dev_set_feature - Issue SET FEATURES
  3825. * @dev: Device to which command will be sent
  3826. * @subcmd: The SET FEATURES subcommand to be sent
  3827. * @action: The sector count represents a subcommand specific action
  3828. *
  3829. * Issue SET FEATURES command to device @dev on port @ap with sector count
  3830. *
  3831. * LOCKING:
  3832. * PCI/etc. bus probe sem.
  3833. *
  3834. * RETURNS:
  3835. * 0 on success, AC_ERR_* mask otherwise.
  3836. */
  3837. unsigned int ata_dev_set_feature(struct ata_device *dev, u8 subcmd, u8 action)
  3838. {
  3839. struct ata_taskfile tf;
  3840. unsigned int timeout = 0;
  3841. /* set up set-features taskfile */
  3842. ata_dev_dbg(dev, "set features\n");
  3843. ata_tf_init(dev, &tf);
  3844. tf.command = ATA_CMD_SET_FEATURES;
  3845. tf.feature = subcmd;
  3846. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  3847. tf.protocol = ATA_PROT_NODATA;
  3848. tf.nsect = action;
  3849. if (subcmd == SETFEATURES_SPINUP)
  3850. timeout = ata_probe_timeout ?
  3851. ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
  3852. return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
  3853. }
  3854. EXPORT_SYMBOL_GPL(ata_dev_set_feature);
  3855. /**
  3856. * ata_dev_init_params - Issue INIT DEV PARAMS command
  3857. * @dev: Device to which command will be sent
  3858. * @heads: Number of heads (taskfile parameter)
  3859. * @sectors: Number of sectors (taskfile parameter)
  3860. *
  3861. * LOCKING:
  3862. * Kernel thread context (may sleep)
  3863. *
  3864. * RETURNS:
  3865. * 0 on success, AC_ERR_* mask otherwise.
  3866. */
  3867. static unsigned int ata_dev_init_params(struct ata_device *dev,
  3868. u16 heads, u16 sectors)
  3869. {
  3870. struct ata_taskfile tf;
  3871. unsigned int err_mask;
  3872. /* Number of sectors per track 1-255. Number of heads 1-16 */
  3873. if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
  3874. return AC_ERR_INVALID;
  3875. /* set up init dev params taskfile */
  3876. ata_dev_dbg(dev, "init dev params \n");
  3877. ata_tf_init(dev, &tf);
  3878. tf.command = ATA_CMD_INIT_DEV_PARAMS;
  3879. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  3880. tf.protocol = ATA_PROT_NODATA;
  3881. tf.nsect = sectors;
  3882. tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
  3883. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  3884. /* A clean abort indicates an original or just out of spec drive
  3885. and we should continue as we issue the setup based on the
  3886. drive reported working geometry */
  3887. if (err_mask == AC_ERR_DEV && (tf.error & ATA_ABORTED))
  3888. err_mask = 0;
  3889. return err_mask;
  3890. }
  3891. /**
  3892. * atapi_check_dma - Check whether ATAPI DMA can be supported
  3893. * @qc: Metadata associated with taskfile to check
  3894. *
  3895. * Allow low-level driver to filter ATA PACKET commands, returning
  3896. * a status indicating whether or not it is OK to use DMA for the
  3897. * supplied PACKET command.
  3898. *
  3899. * LOCKING:
  3900. * spin_lock_irqsave(host lock)
  3901. *
  3902. * RETURNS: 0 when ATAPI DMA can be used
  3903. * nonzero otherwise
  3904. */
  3905. int atapi_check_dma(struct ata_queued_cmd *qc)
  3906. {
  3907. struct ata_port *ap = qc->ap;
  3908. /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
  3909. * few ATAPI devices choke on such DMA requests.
  3910. */
  3911. if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
  3912. unlikely(qc->nbytes & 15))
  3913. return 1;
  3914. if (ap->ops->check_atapi_dma)
  3915. return ap->ops->check_atapi_dma(qc);
  3916. return 0;
  3917. }
  3918. /**
  3919. * ata_std_qc_defer - Check whether a qc needs to be deferred
  3920. * @qc: ATA command in question
  3921. *
  3922. * Non-NCQ commands cannot run with any other command, NCQ or
  3923. * not. As upper layer only knows the queue depth, we are
  3924. * responsible for maintaining exclusion. This function checks
  3925. * whether a new command @qc can be issued.
  3926. *
  3927. * LOCKING:
  3928. * spin_lock_irqsave(host lock)
  3929. *
  3930. * RETURNS:
  3931. * ATA_DEFER_* if deferring is needed, 0 otherwise.
  3932. */
  3933. int ata_std_qc_defer(struct ata_queued_cmd *qc)
  3934. {
  3935. struct ata_link *link = qc->dev->link;
  3936. if (ata_is_ncq(qc->tf.protocol)) {
  3937. if (!ata_tag_valid(link->active_tag))
  3938. return 0;
  3939. } else {
  3940. if (!ata_tag_valid(link->active_tag) && !link->sactive)
  3941. return 0;
  3942. }
  3943. return ATA_DEFER_LINK;
  3944. }
  3945. EXPORT_SYMBOL_GPL(ata_std_qc_defer);
  3946. enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc)
  3947. {
  3948. return AC_ERR_OK;
  3949. }
  3950. EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
  3951. /**
  3952. * ata_sg_init - Associate command with scatter-gather table.
  3953. * @qc: Command to be associated
  3954. * @sg: Scatter-gather table.
  3955. * @n_elem: Number of elements in s/g table.
  3956. *
  3957. * Initialize the data-related elements of queued_cmd @qc
  3958. * to point to a scatter-gather table @sg, containing @n_elem
  3959. * elements.
  3960. *
  3961. * LOCKING:
  3962. * spin_lock_irqsave(host lock)
  3963. */
  3964. void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
  3965. unsigned int n_elem)
  3966. {
  3967. qc->sg = sg;
  3968. qc->n_elem = n_elem;
  3969. qc->cursg = qc->sg;
  3970. }
  3971. #ifdef CONFIG_HAS_DMA
  3972. /**
  3973. * ata_sg_clean - Unmap DMA memory associated with command
  3974. * @qc: Command containing DMA memory to be released
  3975. *
  3976. * Unmap all mapped DMA memory associated with this command.
  3977. *
  3978. * LOCKING:
  3979. * spin_lock_irqsave(host lock)
  3980. */
  3981. static void ata_sg_clean(struct ata_queued_cmd *qc)
  3982. {
  3983. struct ata_port *ap = qc->ap;
  3984. struct scatterlist *sg = qc->sg;
  3985. int dir = qc->dma_dir;
  3986. WARN_ON_ONCE(sg == NULL);
  3987. if (qc->n_elem)
  3988. dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
  3989. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  3990. qc->sg = NULL;
  3991. }
  3992. /**
  3993. * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
  3994. * @qc: Command with scatter-gather table to be mapped.
  3995. *
  3996. * DMA-map the scatter-gather table associated with queued_cmd @qc.
  3997. *
  3998. * LOCKING:
  3999. * spin_lock_irqsave(host lock)
  4000. *
  4001. * RETURNS:
  4002. * Zero on success, negative on error.
  4003. *
  4004. */
  4005. static int ata_sg_setup(struct ata_queued_cmd *qc)
  4006. {
  4007. struct ata_port *ap = qc->ap;
  4008. unsigned int n_elem;
  4009. n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
  4010. if (n_elem < 1)
  4011. return -1;
  4012. qc->orig_n_elem = qc->n_elem;
  4013. qc->n_elem = n_elem;
  4014. qc->flags |= ATA_QCFLAG_DMAMAP;
  4015. return 0;
  4016. }
  4017. #else /* !CONFIG_HAS_DMA */
  4018. static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
  4019. static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
  4020. #endif /* !CONFIG_HAS_DMA */
  4021. /**
  4022. * swap_buf_le16 - swap halves of 16-bit words in place
  4023. * @buf: Buffer to swap
  4024. * @buf_words: Number of 16-bit words in buffer.
  4025. *
  4026. * Swap halves of 16-bit words if needed to convert from
  4027. * little-endian byte order to native cpu byte order, or
  4028. * vice-versa.
  4029. *
  4030. * LOCKING:
  4031. * Inherited from caller.
  4032. */
  4033. void swap_buf_le16(u16 *buf, unsigned int buf_words)
  4034. {
  4035. #ifdef __BIG_ENDIAN
  4036. unsigned int i;
  4037. for (i = 0; i < buf_words; i++)
  4038. buf[i] = le16_to_cpu(buf[i]);
  4039. #endif /* __BIG_ENDIAN */
  4040. }
  4041. /**
  4042. * ata_qc_free - free unused ata_queued_cmd
  4043. * @qc: Command to complete
  4044. *
  4045. * Designed to free unused ata_queued_cmd object
  4046. * in case something prevents using it.
  4047. *
  4048. * LOCKING:
  4049. * spin_lock_irqsave(host lock)
  4050. */
  4051. void ata_qc_free(struct ata_queued_cmd *qc)
  4052. {
  4053. qc->flags = 0;
  4054. if (ata_tag_valid(qc->tag))
  4055. qc->tag = ATA_TAG_POISON;
  4056. }
  4057. void __ata_qc_complete(struct ata_queued_cmd *qc)
  4058. {
  4059. struct ata_port *ap;
  4060. struct ata_link *link;
  4061. WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  4062. WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
  4063. ap = qc->ap;
  4064. link = qc->dev->link;
  4065. if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
  4066. ata_sg_clean(qc);
  4067. /* command should be marked inactive atomically with qc completion */
  4068. if (ata_is_ncq(qc->tf.protocol)) {
  4069. link->sactive &= ~(1 << qc->hw_tag);
  4070. if (!link->sactive)
  4071. ap->nr_active_links--;
  4072. } else {
  4073. link->active_tag = ATA_TAG_POISON;
  4074. ap->nr_active_links--;
  4075. }
  4076. /* clear exclusive status */
  4077. if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
  4078. ap->excl_link == link))
  4079. ap->excl_link = NULL;
  4080. /* atapi: mark qc as inactive to prevent the interrupt handler
  4081. * from completing the command twice later, before the error handler
  4082. * is called. (when rc != 0 and atapi request sense is needed)
  4083. */
  4084. qc->flags &= ~ATA_QCFLAG_ACTIVE;
  4085. ap->qc_active &= ~(1ULL << qc->tag);
  4086. /* call completion callback */
  4087. qc->complete_fn(qc);
  4088. }
  4089. static void fill_result_tf(struct ata_queued_cmd *qc)
  4090. {
  4091. struct ata_port *ap = qc->ap;
  4092. qc->result_tf.flags = qc->tf.flags;
  4093. ap->ops->qc_fill_rtf(qc);
  4094. }
  4095. static void ata_verify_xfer(struct ata_queued_cmd *qc)
  4096. {
  4097. struct ata_device *dev = qc->dev;
  4098. if (!ata_is_data(qc->tf.protocol))
  4099. return;
  4100. if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
  4101. return;
  4102. dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
  4103. }
  4104. /**
  4105. * ata_qc_complete - Complete an active ATA command
  4106. * @qc: Command to complete
  4107. *
  4108. * Indicate to the mid and upper layers that an ATA command has
  4109. * completed, with either an ok or not-ok status.
  4110. *
  4111. * Refrain from calling this function multiple times when
  4112. * successfully completing multiple NCQ commands.
  4113. * ata_qc_complete_multiple() should be used instead, which will
  4114. * properly update IRQ expect state.
  4115. *
  4116. * LOCKING:
  4117. * spin_lock_irqsave(host lock)
  4118. */
  4119. void ata_qc_complete(struct ata_queued_cmd *qc)
  4120. {
  4121. struct ata_port *ap = qc->ap;
  4122. /* Trigger the LED (if available) */
  4123. ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
  4124. /* XXX: New EH and old EH use different mechanisms to
  4125. * synchronize EH with regular execution path.
  4126. *
  4127. * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
  4128. * Normal execution path is responsible for not accessing a
  4129. * failed qc. libata core enforces the rule by returning NULL
  4130. * from ata_qc_from_tag() for failed qcs.
  4131. *
  4132. * Old EH depends on ata_qc_complete() nullifying completion
  4133. * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
  4134. * not synchronize with interrupt handler. Only PIO task is
  4135. * taken care of.
  4136. */
  4137. if (ap->ops->error_handler) {
  4138. struct ata_device *dev = qc->dev;
  4139. struct ata_eh_info *ehi = &dev->link->eh_info;
  4140. if (unlikely(qc->err_mask))
  4141. qc->flags |= ATA_QCFLAG_FAILED;
  4142. /*
  4143. * Finish internal commands without any further processing
  4144. * and always with the result TF filled.
  4145. */
  4146. if (unlikely(ata_tag_internal(qc->tag))) {
  4147. fill_result_tf(qc);
  4148. trace_ata_qc_complete_internal(qc);
  4149. __ata_qc_complete(qc);
  4150. return;
  4151. }
  4152. /*
  4153. * Non-internal qc has failed. Fill the result TF and
  4154. * summon EH.
  4155. */
  4156. if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
  4157. fill_result_tf(qc);
  4158. trace_ata_qc_complete_failed(qc);
  4159. ata_qc_schedule_eh(qc);
  4160. return;
  4161. }
  4162. WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
  4163. /* read result TF if requested */
  4164. if (qc->flags & ATA_QCFLAG_RESULT_TF)
  4165. fill_result_tf(qc);
  4166. trace_ata_qc_complete_done(qc);
  4167. /* Some commands need post-processing after successful
  4168. * completion.
  4169. */
  4170. switch (qc->tf.command) {
  4171. case ATA_CMD_SET_FEATURES:
  4172. if (qc->tf.feature != SETFEATURES_WC_ON &&
  4173. qc->tf.feature != SETFEATURES_WC_OFF &&
  4174. qc->tf.feature != SETFEATURES_RA_ON &&
  4175. qc->tf.feature != SETFEATURES_RA_OFF)
  4176. break;
  4177. fallthrough;
  4178. case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
  4179. case ATA_CMD_SET_MULTI: /* multi_count changed */
  4180. /* revalidate device */
  4181. ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
  4182. ata_port_schedule_eh(ap);
  4183. break;
  4184. case ATA_CMD_SLEEP:
  4185. dev->flags |= ATA_DFLAG_SLEEPING;
  4186. break;
  4187. }
  4188. if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
  4189. ata_verify_xfer(qc);
  4190. __ata_qc_complete(qc);
  4191. } else {
  4192. if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
  4193. return;
  4194. /* read result TF if failed or requested */
  4195. if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
  4196. fill_result_tf(qc);
  4197. __ata_qc_complete(qc);
  4198. }
  4199. }
  4200. EXPORT_SYMBOL_GPL(ata_qc_complete);
  4201. /**
  4202. * ata_qc_get_active - get bitmask of active qcs
  4203. * @ap: port in question
  4204. *
  4205. * LOCKING:
  4206. * spin_lock_irqsave(host lock)
  4207. *
  4208. * RETURNS:
  4209. * Bitmask of active qcs
  4210. */
  4211. u64 ata_qc_get_active(struct ata_port *ap)
  4212. {
  4213. u64 qc_active = ap->qc_active;
  4214. /* ATA_TAG_INTERNAL is sent to hw as tag 0 */
  4215. if (qc_active & (1ULL << ATA_TAG_INTERNAL)) {
  4216. qc_active |= (1 << 0);
  4217. qc_active &= ~(1ULL << ATA_TAG_INTERNAL);
  4218. }
  4219. return qc_active;
  4220. }
  4221. EXPORT_SYMBOL_GPL(ata_qc_get_active);
  4222. /**
  4223. * ata_qc_issue - issue taskfile to device
  4224. * @qc: command to issue to device
  4225. *
  4226. * Prepare an ATA command to submission to device.
  4227. * This includes mapping the data into a DMA-able
  4228. * area, filling in the S/G table, and finally
  4229. * writing the taskfile to hardware, starting the command.
  4230. *
  4231. * LOCKING:
  4232. * spin_lock_irqsave(host lock)
  4233. */
  4234. void ata_qc_issue(struct ata_queued_cmd *qc)
  4235. {
  4236. struct ata_port *ap = qc->ap;
  4237. struct ata_link *link = qc->dev->link;
  4238. u8 prot = qc->tf.protocol;
  4239. /* Make sure only one non-NCQ command is outstanding. The
  4240. * check is skipped for old EH because it reuses active qc to
  4241. * request ATAPI sense.
  4242. */
  4243. WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
  4244. if (ata_is_ncq(prot)) {
  4245. WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag));
  4246. if (!link->sactive)
  4247. ap->nr_active_links++;
  4248. link->sactive |= 1 << qc->hw_tag;
  4249. } else {
  4250. WARN_ON_ONCE(link->sactive);
  4251. ap->nr_active_links++;
  4252. link->active_tag = qc->tag;
  4253. }
  4254. qc->flags |= ATA_QCFLAG_ACTIVE;
  4255. ap->qc_active |= 1ULL << qc->tag;
  4256. /*
  4257. * We guarantee to LLDs that they will have at least one
  4258. * non-zero sg if the command is a data command.
  4259. */
  4260. if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
  4261. goto sys_err;
  4262. if (ata_is_dma(prot) || (ata_is_pio(prot) &&
  4263. (ap->flags & ATA_FLAG_PIO_DMA)))
  4264. if (ata_sg_setup(qc))
  4265. goto sys_err;
  4266. /* if device is sleeping, schedule reset and abort the link */
  4267. if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
  4268. link->eh_info.action |= ATA_EH_RESET;
  4269. ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
  4270. ata_link_abort(link);
  4271. return;
  4272. }
  4273. trace_ata_qc_prep(qc);
  4274. qc->err_mask |= ap->ops->qc_prep(qc);
  4275. if (unlikely(qc->err_mask))
  4276. goto err;
  4277. trace_ata_qc_issue(qc);
  4278. qc->err_mask |= ap->ops->qc_issue(qc);
  4279. if (unlikely(qc->err_mask))
  4280. goto err;
  4281. return;
  4282. sys_err:
  4283. qc->err_mask |= AC_ERR_SYSTEM;
  4284. err:
  4285. ata_qc_complete(qc);
  4286. }
  4287. /**
  4288. * ata_phys_link_online - test whether the given link is online
  4289. * @link: ATA link to test
  4290. *
  4291. * Test whether @link is online. Note that this function returns
  4292. * 0 if online status of @link cannot be obtained, so
  4293. * ata_link_online(link) != !ata_link_offline(link).
  4294. *
  4295. * LOCKING:
  4296. * None.
  4297. *
  4298. * RETURNS:
  4299. * True if the port online status is available and online.
  4300. */
  4301. bool ata_phys_link_online(struct ata_link *link)
  4302. {
  4303. u32 sstatus;
  4304. if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
  4305. ata_sstatus_online(sstatus))
  4306. return true;
  4307. return false;
  4308. }
  4309. /**
  4310. * ata_phys_link_offline - test whether the given link is offline
  4311. * @link: ATA link to test
  4312. *
  4313. * Test whether @link is offline. Note that this function
  4314. * returns 0 if offline status of @link cannot be obtained, so
  4315. * ata_link_online(link) != !ata_link_offline(link).
  4316. *
  4317. * LOCKING:
  4318. * None.
  4319. *
  4320. * RETURNS:
  4321. * True if the port offline status is available and offline.
  4322. */
  4323. bool ata_phys_link_offline(struct ata_link *link)
  4324. {
  4325. u32 sstatus;
  4326. if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
  4327. !ata_sstatus_online(sstatus))
  4328. return true;
  4329. return false;
  4330. }
  4331. /**
  4332. * ata_link_online - test whether the given link is online
  4333. * @link: ATA link to test
  4334. *
  4335. * Test whether @link is online. This is identical to
  4336. * ata_phys_link_online() when there's no slave link. When
  4337. * there's a slave link, this function should only be called on
  4338. * the master link and will return true if any of M/S links is
  4339. * online.
  4340. *
  4341. * LOCKING:
  4342. * None.
  4343. *
  4344. * RETURNS:
  4345. * True if the port online status is available and online.
  4346. */
  4347. bool ata_link_online(struct ata_link *link)
  4348. {
  4349. struct ata_link *slave = link->ap->slave_link;
  4350. WARN_ON(link == slave); /* shouldn't be called on slave link */
  4351. return ata_phys_link_online(link) ||
  4352. (slave && ata_phys_link_online(slave));
  4353. }
  4354. EXPORT_SYMBOL_GPL(ata_link_online);
  4355. /**
  4356. * ata_link_offline - test whether the given link is offline
  4357. * @link: ATA link to test
  4358. *
  4359. * Test whether @link is offline. This is identical to
  4360. * ata_phys_link_offline() when there's no slave link. When
  4361. * there's a slave link, this function should only be called on
  4362. * the master link and will return true if both M/S links are
  4363. * offline.
  4364. *
  4365. * LOCKING:
  4366. * None.
  4367. *
  4368. * RETURNS:
  4369. * True if the port offline status is available and offline.
  4370. */
  4371. bool ata_link_offline(struct ata_link *link)
  4372. {
  4373. struct ata_link *slave = link->ap->slave_link;
  4374. WARN_ON(link == slave); /* shouldn't be called on slave link */
  4375. return ata_phys_link_offline(link) &&
  4376. (!slave || ata_phys_link_offline(slave));
  4377. }
  4378. EXPORT_SYMBOL_GPL(ata_link_offline);
  4379. #ifdef CONFIG_PM
  4380. static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
  4381. unsigned int action, unsigned int ehi_flags,
  4382. bool async)
  4383. {
  4384. struct ata_link *link;
  4385. unsigned long flags;
  4386. spin_lock_irqsave(ap->lock, flags);
  4387. /*
  4388. * A previous PM operation might still be in progress. Wait for
  4389. * ATA_PFLAG_PM_PENDING to clear.
  4390. */
  4391. if (ap->pflags & ATA_PFLAG_PM_PENDING) {
  4392. spin_unlock_irqrestore(ap->lock, flags);
  4393. ata_port_wait_eh(ap);
  4394. spin_lock_irqsave(ap->lock, flags);
  4395. }
  4396. /* Request PM operation to EH */
  4397. ap->pm_mesg = mesg;
  4398. ap->pflags |= ATA_PFLAG_PM_PENDING;
  4399. ata_for_each_link(link, ap, HOST_FIRST) {
  4400. link->eh_info.action |= action;
  4401. link->eh_info.flags |= ehi_flags;
  4402. }
  4403. ata_port_schedule_eh(ap);
  4404. spin_unlock_irqrestore(ap->lock, flags);
  4405. if (!async)
  4406. ata_port_wait_eh(ap);
  4407. }
  4408. /*
  4409. * On some hardware, device fails to respond after spun down for suspend. As
  4410. * the device won't be used before being resumed, we don't need to touch the
  4411. * device. Ask EH to skip the usual stuff and proceed directly to suspend.
  4412. *
  4413. * http://thread.gmane.org/gmane.linux.ide/46764
  4414. */
  4415. static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
  4416. | ATA_EHI_NO_AUTOPSY
  4417. | ATA_EHI_NO_RECOVERY;
  4418. static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
  4419. {
  4420. /*
  4421. * We are about to suspend the port, so we do not care about
  4422. * scsi_rescan_device() calls scheduled by previous resume operations.
  4423. * The next resume will schedule the rescan again. So cancel any rescan
  4424. * that is not done yet.
  4425. */
  4426. cancel_delayed_work_sync(&ap->scsi_rescan_task);
  4427. ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
  4428. }
  4429. static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
  4430. {
  4431. /*
  4432. * We are about to suspend the port, so we do not care about
  4433. * scsi_rescan_device() calls scheduled by previous resume operations.
  4434. * The next resume will schedule the rescan again. So cancel any rescan
  4435. * that is not done yet.
  4436. */
  4437. cancel_delayed_work_sync(&ap->scsi_rescan_task);
  4438. ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
  4439. }
  4440. static int ata_port_pm_suspend(struct device *dev)
  4441. {
  4442. struct ata_port *ap = to_ata_port(dev);
  4443. if (pm_runtime_suspended(dev))
  4444. return 0;
  4445. ata_port_suspend(ap, PMSG_SUSPEND);
  4446. return 0;
  4447. }
  4448. static int ata_port_pm_freeze(struct device *dev)
  4449. {
  4450. struct ata_port *ap = to_ata_port(dev);
  4451. if (pm_runtime_suspended(dev))
  4452. return 0;
  4453. ata_port_suspend(ap, PMSG_FREEZE);
  4454. return 0;
  4455. }
  4456. static int ata_port_pm_poweroff(struct device *dev)
  4457. {
  4458. ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
  4459. return 0;
  4460. }
  4461. static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
  4462. | ATA_EHI_QUIET;
  4463. static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
  4464. {
  4465. ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
  4466. }
  4467. static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
  4468. {
  4469. ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
  4470. }
  4471. static int ata_port_pm_resume(struct device *dev)
  4472. {
  4473. ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
  4474. pm_runtime_disable(dev);
  4475. pm_runtime_set_active(dev);
  4476. pm_runtime_enable(dev);
  4477. return 0;
  4478. }
  4479. /*
  4480. * For ODDs, the upper layer will poll for media change every few seconds,
  4481. * which will make it enter and leave suspend state every few seconds. And
  4482. * as each suspend will cause a hard/soft reset, the gain of runtime suspend
  4483. * is very little and the ODD may malfunction after constantly being reset.
  4484. * So the idle callback here will not proceed to suspend if a non-ZPODD capable
  4485. * ODD is attached to the port.
  4486. */
  4487. static int ata_port_runtime_idle(struct device *dev)
  4488. {
  4489. struct ata_port *ap = to_ata_port(dev);
  4490. struct ata_link *link;
  4491. struct ata_device *adev;
  4492. ata_for_each_link(link, ap, HOST_FIRST) {
  4493. ata_for_each_dev(adev, link, ENABLED)
  4494. if (adev->class == ATA_DEV_ATAPI &&
  4495. !zpodd_dev_enabled(adev))
  4496. return -EBUSY;
  4497. }
  4498. return 0;
  4499. }
  4500. static int ata_port_runtime_suspend(struct device *dev)
  4501. {
  4502. ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
  4503. return 0;
  4504. }
  4505. static int ata_port_runtime_resume(struct device *dev)
  4506. {
  4507. ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
  4508. return 0;
  4509. }
  4510. static const struct dev_pm_ops ata_port_pm_ops = {
  4511. .suspend = ata_port_pm_suspend,
  4512. .resume = ata_port_pm_resume,
  4513. .freeze = ata_port_pm_freeze,
  4514. .thaw = ata_port_pm_resume,
  4515. .poweroff = ata_port_pm_poweroff,
  4516. .restore = ata_port_pm_resume,
  4517. .runtime_suspend = ata_port_runtime_suspend,
  4518. .runtime_resume = ata_port_runtime_resume,
  4519. .runtime_idle = ata_port_runtime_idle,
  4520. };
  4521. /* sas ports don't participate in pm runtime management of ata_ports,
  4522. * and need to resume ata devices at the domain level, not the per-port
  4523. * level. sas suspend/resume is async to allow parallel port recovery
  4524. * since sas has multiple ata_port instances per Scsi_Host.
  4525. */
  4526. void ata_sas_port_suspend(struct ata_port *ap)
  4527. {
  4528. ata_port_suspend_async(ap, PMSG_SUSPEND);
  4529. }
  4530. EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
  4531. void ata_sas_port_resume(struct ata_port *ap)
  4532. {
  4533. ata_port_resume_async(ap, PMSG_RESUME);
  4534. }
  4535. EXPORT_SYMBOL_GPL(ata_sas_port_resume);
  4536. /**
  4537. * ata_host_suspend - suspend host
  4538. * @host: host to suspend
  4539. * @mesg: PM message
  4540. *
  4541. * Suspend @host. Actual operation is performed by port suspend.
  4542. */
  4543. void ata_host_suspend(struct ata_host *host, pm_message_t mesg)
  4544. {
  4545. host->dev->power.power_state = mesg;
  4546. }
  4547. EXPORT_SYMBOL_GPL(ata_host_suspend);
  4548. /**
  4549. * ata_host_resume - resume host
  4550. * @host: host to resume
  4551. *
  4552. * Resume @host. Actual operation is performed by port resume.
  4553. */
  4554. void ata_host_resume(struct ata_host *host)
  4555. {
  4556. host->dev->power.power_state = PMSG_ON;
  4557. }
  4558. EXPORT_SYMBOL_GPL(ata_host_resume);
  4559. #endif
  4560. const struct device_type ata_port_type = {
  4561. .name = ATA_PORT_TYPE_NAME,
  4562. #ifdef CONFIG_PM
  4563. .pm = &ata_port_pm_ops,
  4564. #endif
  4565. };
  4566. /**
  4567. * ata_dev_init - Initialize an ata_device structure
  4568. * @dev: Device structure to initialize
  4569. *
  4570. * Initialize @dev in preparation for probing.
  4571. *
  4572. * LOCKING:
  4573. * Inherited from caller.
  4574. */
  4575. void ata_dev_init(struct ata_device *dev)
  4576. {
  4577. struct ata_link *link = ata_dev_phys_link(dev);
  4578. struct ata_port *ap = link->ap;
  4579. unsigned long flags;
  4580. /* SATA spd limit is bound to the attached device, reset together */
  4581. link->sata_spd_limit = link->hw_sata_spd_limit;
  4582. link->sata_spd = 0;
  4583. /* High bits of dev->flags are used to record warm plug
  4584. * requests which occur asynchronously. Synchronize using
  4585. * host lock.
  4586. */
  4587. spin_lock_irqsave(ap->lock, flags);
  4588. dev->flags &= ~ATA_DFLAG_INIT_MASK;
  4589. dev->horkage = 0;
  4590. spin_unlock_irqrestore(ap->lock, flags);
  4591. memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
  4592. ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
  4593. dev->pio_mask = UINT_MAX;
  4594. dev->mwdma_mask = UINT_MAX;
  4595. dev->udma_mask = UINT_MAX;
  4596. }
  4597. /**
  4598. * ata_link_init - Initialize an ata_link structure
  4599. * @ap: ATA port link is attached to
  4600. * @link: Link structure to initialize
  4601. * @pmp: Port multiplier port number
  4602. *
  4603. * Initialize @link.
  4604. *
  4605. * LOCKING:
  4606. * Kernel thread context (may sleep)
  4607. */
  4608. void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
  4609. {
  4610. int i;
  4611. /* clear everything except for devices */
  4612. memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
  4613. ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
  4614. link->ap = ap;
  4615. link->pmp = pmp;
  4616. link->active_tag = ATA_TAG_POISON;
  4617. link->hw_sata_spd_limit = UINT_MAX;
  4618. /* can't use iterator, ap isn't initialized yet */
  4619. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  4620. struct ata_device *dev = &link->device[i];
  4621. dev->link = link;
  4622. dev->devno = dev - link->device;
  4623. #ifdef CONFIG_ATA_ACPI
  4624. dev->gtf_filter = ata_acpi_gtf_filter;
  4625. #endif
  4626. ata_dev_init(dev);
  4627. }
  4628. }
  4629. /**
  4630. * sata_link_init_spd - Initialize link->sata_spd_limit
  4631. * @link: Link to configure sata_spd_limit for
  4632. *
  4633. * Initialize ``link->[hw_]sata_spd_limit`` to the currently
  4634. * configured value.
  4635. *
  4636. * LOCKING:
  4637. * Kernel thread context (may sleep).
  4638. *
  4639. * RETURNS:
  4640. * 0 on success, -errno on failure.
  4641. */
  4642. int sata_link_init_spd(struct ata_link *link)
  4643. {
  4644. u8 spd;
  4645. int rc;
  4646. rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
  4647. if (rc)
  4648. return rc;
  4649. spd = (link->saved_scontrol >> 4) & 0xf;
  4650. if (spd)
  4651. link->hw_sata_spd_limit &= (1 << spd) - 1;
  4652. ata_force_link_limits(link);
  4653. link->sata_spd_limit = link->hw_sata_spd_limit;
  4654. return 0;
  4655. }
  4656. /**
  4657. * ata_port_alloc - allocate and initialize basic ATA port resources
  4658. * @host: ATA host this allocated port belongs to
  4659. *
  4660. * Allocate and initialize basic ATA port resources.
  4661. *
  4662. * RETURNS:
  4663. * Allocate ATA port on success, NULL on failure.
  4664. *
  4665. * LOCKING:
  4666. * Inherited from calling layer (may sleep).
  4667. */
  4668. struct ata_port *ata_port_alloc(struct ata_host *host)
  4669. {
  4670. struct ata_port *ap;
  4671. ap = kzalloc(sizeof(*ap), GFP_KERNEL);
  4672. if (!ap)
  4673. return NULL;
  4674. ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
  4675. ap->lock = &host->lock;
  4676. ap->print_id = -1;
  4677. ap->local_port_no = -1;
  4678. ap->host = host;
  4679. ap->dev = host->dev;
  4680. mutex_init(&ap->scsi_scan_mutex);
  4681. INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
  4682. INIT_DELAYED_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
  4683. INIT_LIST_HEAD(&ap->eh_done_q);
  4684. init_waitqueue_head(&ap->eh_wait_q);
  4685. init_completion(&ap->park_req_pending);
  4686. timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
  4687. TIMER_DEFERRABLE);
  4688. ap->cbl = ATA_CBL_NONE;
  4689. ata_link_init(ap, &ap->link, 0);
  4690. #ifdef ATA_IRQ_TRAP
  4691. ap->stats.unhandled_irq = 1;
  4692. ap->stats.idle_irq = 1;
  4693. #endif
  4694. ata_sff_port_init(ap);
  4695. return ap;
  4696. }
  4697. static void ata_devres_release(struct device *gendev, void *res)
  4698. {
  4699. struct ata_host *host = dev_get_drvdata(gendev);
  4700. int i;
  4701. for (i = 0; i < host->n_ports; i++) {
  4702. struct ata_port *ap = host->ports[i];
  4703. if (!ap)
  4704. continue;
  4705. if (ap->scsi_host)
  4706. scsi_host_put(ap->scsi_host);
  4707. }
  4708. dev_set_drvdata(gendev, NULL);
  4709. ata_host_put(host);
  4710. }
  4711. static void ata_host_release(struct kref *kref)
  4712. {
  4713. struct ata_host *host = container_of(kref, struct ata_host, kref);
  4714. int i;
  4715. for (i = 0; i < host->n_ports; i++) {
  4716. struct ata_port *ap = host->ports[i];
  4717. kfree(ap->pmp_link);
  4718. kfree(ap->slave_link);
  4719. kfree(ap);
  4720. host->ports[i] = NULL;
  4721. }
  4722. kfree(host);
  4723. }
  4724. void ata_host_get(struct ata_host *host)
  4725. {
  4726. kref_get(&host->kref);
  4727. }
  4728. void ata_host_put(struct ata_host *host)
  4729. {
  4730. kref_put(&host->kref, ata_host_release);
  4731. }
  4732. EXPORT_SYMBOL_GPL(ata_host_put);
  4733. /**
  4734. * ata_host_alloc - allocate and init basic ATA host resources
  4735. * @dev: generic device this host is associated with
  4736. * @max_ports: maximum number of ATA ports associated with this host
  4737. *
  4738. * Allocate and initialize basic ATA host resources. LLD calls
  4739. * this function to allocate a host, initializes it fully and
  4740. * attaches it using ata_host_register().
  4741. *
  4742. * @max_ports ports are allocated and host->n_ports is
  4743. * initialized to @max_ports. The caller is allowed to decrease
  4744. * host->n_ports before calling ata_host_register(). The unused
  4745. * ports will be automatically freed on registration.
  4746. *
  4747. * RETURNS:
  4748. * Allocate ATA host on success, NULL on failure.
  4749. *
  4750. * LOCKING:
  4751. * Inherited from calling layer (may sleep).
  4752. */
  4753. struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
  4754. {
  4755. struct ata_host *host;
  4756. size_t sz;
  4757. int i;
  4758. void *dr;
  4759. /* alloc a container for our list of ATA ports (buses) */
  4760. sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
  4761. host = kzalloc(sz, GFP_KERNEL);
  4762. if (!host)
  4763. return NULL;
  4764. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  4765. goto err_free;
  4766. dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
  4767. if (!dr)
  4768. goto err_out;
  4769. devres_add(dev, dr);
  4770. dev_set_drvdata(dev, host);
  4771. spin_lock_init(&host->lock);
  4772. mutex_init(&host->eh_mutex);
  4773. host->dev = dev;
  4774. host->n_ports = max_ports;
  4775. kref_init(&host->kref);
  4776. /* allocate ports bound to this host */
  4777. for (i = 0; i < max_ports; i++) {
  4778. struct ata_port *ap;
  4779. ap = ata_port_alloc(host);
  4780. if (!ap)
  4781. goto err_out;
  4782. ap->port_no = i;
  4783. host->ports[i] = ap;
  4784. }
  4785. devres_remove_group(dev, NULL);
  4786. return host;
  4787. err_out:
  4788. devres_release_group(dev, NULL);
  4789. err_free:
  4790. kfree(host);
  4791. return NULL;
  4792. }
  4793. EXPORT_SYMBOL_GPL(ata_host_alloc);
  4794. /**
  4795. * ata_host_alloc_pinfo - alloc host and init with port_info array
  4796. * @dev: generic device this host is associated with
  4797. * @ppi: array of ATA port_info to initialize host with
  4798. * @n_ports: number of ATA ports attached to this host
  4799. *
  4800. * Allocate ATA host and initialize with info from @ppi. If NULL
  4801. * terminated, @ppi may contain fewer entries than @n_ports. The
  4802. * last entry will be used for the remaining ports.
  4803. *
  4804. * RETURNS:
  4805. * Allocate ATA host on success, NULL on failure.
  4806. *
  4807. * LOCKING:
  4808. * Inherited from calling layer (may sleep).
  4809. */
  4810. struct ata_host *ata_host_alloc_pinfo(struct device *dev,
  4811. const struct ata_port_info * const * ppi,
  4812. int n_ports)
  4813. {
  4814. const struct ata_port_info *pi = &ata_dummy_port_info;
  4815. struct ata_host *host;
  4816. int i, j;
  4817. host = ata_host_alloc(dev, n_ports);
  4818. if (!host)
  4819. return NULL;
  4820. for (i = 0, j = 0; i < host->n_ports; i++) {
  4821. struct ata_port *ap = host->ports[i];
  4822. if (ppi[j])
  4823. pi = ppi[j++];
  4824. ap->pio_mask = pi->pio_mask;
  4825. ap->mwdma_mask = pi->mwdma_mask;
  4826. ap->udma_mask = pi->udma_mask;
  4827. ap->flags |= pi->flags;
  4828. ap->link.flags |= pi->link_flags;
  4829. ap->ops = pi->port_ops;
  4830. if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
  4831. host->ops = pi->port_ops;
  4832. }
  4833. return host;
  4834. }
  4835. EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
  4836. static void ata_host_stop(struct device *gendev, void *res)
  4837. {
  4838. struct ata_host *host = dev_get_drvdata(gendev);
  4839. int i;
  4840. WARN_ON(!(host->flags & ATA_HOST_STARTED));
  4841. for (i = 0; i < host->n_ports; i++) {
  4842. struct ata_port *ap = host->ports[i];
  4843. if (ap->ops->port_stop)
  4844. ap->ops->port_stop(ap);
  4845. }
  4846. if (host->ops->host_stop)
  4847. host->ops->host_stop(host);
  4848. }
  4849. /**
  4850. * ata_finalize_port_ops - finalize ata_port_operations
  4851. * @ops: ata_port_operations to finalize
  4852. *
  4853. * An ata_port_operations can inherit from another ops and that
  4854. * ops can again inherit from another. This can go on as many
  4855. * times as necessary as long as there is no loop in the
  4856. * inheritance chain.
  4857. *
  4858. * Ops tables are finalized when the host is started. NULL or
  4859. * unspecified entries are inherited from the closet ancestor
  4860. * which has the method and the entry is populated with it.
  4861. * After finalization, the ops table directly points to all the
  4862. * methods and ->inherits is no longer necessary and cleared.
  4863. *
  4864. * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
  4865. *
  4866. * LOCKING:
  4867. * None.
  4868. */
  4869. static void ata_finalize_port_ops(struct ata_port_operations *ops)
  4870. {
  4871. static DEFINE_SPINLOCK(lock);
  4872. const struct ata_port_operations *cur;
  4873. void **begin = (void **)ops;
  4874. void **end = (void **)&ops->inherits;
  4875. void **pp;
  4876. if (!ops || !ops->inherits)
  4877. return;
  4878. spin_lock(&lock);
  4879. for (cur = ops->inherits; cur; cur = cur->inherits) {
  4880. void **inherit = (void **)cur;
  4881. for (pp = begin; pp < end; pp++, inherit++)
  4882. if (!*pp)
  4883. *pp = *inherit;
  4884. }
  4885. for (pp = begin; pp < end; pp++)
  4886. if (IS_ERR(*pp))
  4887. *pp = NULL;
  4888. ops->inherits = NULL;
  4889. spin_unlock(&lock);
  4890. }
  4891. /**
  4892. * ata_host_start - start and freeze ports of an ATA host
  4893. * @host: ATA host to start ports for
  4894. *
  4895. * Start and then freeze ports of @host. Started status is
  4896. * recorded in host->flags, so this function can be called
  4897. * multiple times. Ports are guaranteed to get started only
  4898. * once. If host->ops is not initialized yet, it is set to the
  4899. * first non-dummy port ops.
  4900. *
  4901. * LOCKING:
  4902. * Inherited from calling layer (may sleep).
  4903. *
  4904. * RETURNS:
  4905. * 0 if all ports are started successfully, -errno otherwise.
  4906. */
  4907. int ata_host_start(struct ata_host *host)
  4908. {
  4909. int have_stop = 0;
  4910. void *start_dr = NULL;
  4911. int i, rc;
  4912. if (host->flags & ATA_HOST_STARTED)
  4913. return 0;
  4914. ata_finalize_port_ops(host->ops);
  4915. for (i = 0; i < host->n_ports; i++) {
  4916. struct ata_port *ap = host->ports[i];
  4917. ata_finalize_port_ops(ap->ops);
  4918. if (!host->ops && !ata_port_is_dummy(ap))
  4919. host->ops = ap->ops;
  4920. if (ap->ops->port_stop)
  4921. have_stop = 1;
  4922. }
  4923. if (host->ops && host->ops->host_stop)
  4924. have_stop = 1;
  4925. if (have_stop) {
  4926. start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
  4927. if (!start_dr)
  4928. return -ENOMEM;
  4929. }
  4930. for (i = 0; i < host->n_ports; i++) {
  4931. struct ata_port *ap = host->ports[i];
  4932. if (ap->ops->port_start) {
  4933. rc = ap->ops->port_start(ap);
  4934. if (rc) {
  4935. if (rc != -ENODEV)
  4936. dev_err(host->dev,
  4937. "failed to start port %d (errno=%d)\n",
  4938. i, rc);
  4939. goto err_out;
  4940. }
  4941. }
  4942. ata_eh_freeze_port(ap);
  4943. }
  4944. if (start_dr)
  4945. devres_add(host->dev, start_dr);
  4946. host->flags |= ATA_HOST_STARTED;
  4947. return 0;
  4948. err_out:
  4949. while (--i >= 0) {
  4950. struct ata_port *ap = host->ports[i];
  4951. if (ap->ops->port_stop)
  4952. ap->ops->port_stop(ap);
  4953. }
  4954. devres_free(start_dr);
  4955. return rc;
  4956. }
  4957. EXPORT_SYMBOL_GPL(ata_host_start);
  4958. /**
  4959. * ata_host_init - Initialize a host struct for sas (ipr, libsas)
  4960. * @host: host to initialize
  4961. * @dev: device host is attached to
  4962. * @ops: port_ops
  4963. *
  4964. */
  4965. void ata_host_init(struct ata_host *host, struct device *dev,
  4966. struct ata_port_operations *ops)
  4967. {
  4968. spin_lock_init(&host->lock);
  4969. mutex_init(&host->eh_mutex);
  4970. host->n_tags = ATA_MAX_QUEUE;
  4971. host->dev = dev;
  4972. host->ops = ops;
  4973. kref_init(&host->kref);
  4974. }
  4975. EXPORT_SYMBOL_GPL(ata_host_init);
  4976. void __ata_port_probe(struct ata_port *ap)
  4977. {
  4978. struct ata_eh_info *ehi = &ap->link.eh_info;
  4979. unsigned long flags;
  4980. /* kick EH for boot probing */
  4981. spin_lock_irqsave(ap->lock, flags);
  4982. ehi->probe_mask |= ATA_ALL_DEVICES;
  4983. ehi->action |= ATA_EH_RESET;
  4984. ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
  4985. ap->pflags &= ~ATA_PFLAG_INITIALIZING;
  4986. ap->pflags |= ATA_PFLAG_LOADING;
  4987. ata_port_schedule_eh(ap);
  4988. spin_unlock_irqrestore(ap->lock, flags);
  4989. }
  4990. int ata_port_probe(struct ata_port *ap)
  4991. {
  4992. int rc = 0;
  4993. if (ap->ops->error_handler) {
  4994. __ata_port_probe(ap);
  4995. ata_port_wait_eh(ap);
  4996. } else {
  4997. rc = ata_bus_probe(ap);
  4998. }
  4999. return rc;
  5000. }
  5001. static void async_port_probe(void *data, async_cookie_t cookie)
  5002. {
  5003. struct ata_port *ap = data;
  5004. /*
  5005. * If we're not allowed to scan this host in parallel,
  5006. * we need to wait until all previous scans have completed
  5007. * before going further.
  5008. * Jeff Garzik says this is only within a controller, so we
  5009. * don't need to wait for port 0, only for later ports.
  5010. */
  5011. if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
  5012. async_synchronize_cookie(cookie);
  5013. (void)ata_port_probe(ap);
  5014. /* in order to keep device order, we need to synchronize at this point */
  5015. async_synchronize_cookie(cookie);
  5016. ata_scsi_scan_host(ap, 1);
  5017. }
  5018. /**
  5019. * ata_host_register - register initialized ATA host
  5020. * @host: ATA host to register
  5021. * @sht: template for SCSI host
  5022. *
  5023. * Register initialized ATA host. @host is allocated using
  5024. * ata_host_alloc() and fully initialized by LLD. This function
  5025. * starts ports, registers @host with ATA and SCSI layers and
  5026. * probe registered devices.
  5027. *
  5028. * LOCKING:
  5029. * Inherited from calling layer (may sleep).
  5030. *
  5031. * RETURNS:
  5032. * 0 on success, -errno otherwise.
  5033. */
  5034. int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
  5035. {
  5036. int i, rc;
  5037. host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE);
  5038. /* host must have been started */
  5039. if (!(host->flags & ATA_HOST_STARTED)) {
  5040. dev_err(host->dev, "BUG: trying to register unstarted host\n");
  5041. WARN_ON(1);
  5042. return -EINVAL;
  5043. }
  5044. /* Blow away unused ports. This happens when LLD can't
  5045. * determine the exact number of ports to allocate at
  5046. * allocation time.
  5047. */
  5048. for (i = host->n_ports; host->ports[i]; i++)
  5049. kfree(host->ports[i]);
  5050. /* give ports names and add SCSI hosts */
  5051. for (i = 0; i < host->n_ports; i++) {
  5052. host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
  5053. host->ports[i]->local_port_no = i + 1;
  5054. }
  5055. /* Create associated sysfs transport objects */
  5056. for (i = 0; i < host->n_ports; i++) {
  5057. rc = ata_tport_add(host->dev,host->ports[i]);
  5058. if (rc) {
  5059. goto err_tadd;
  5060. }
  5061. }
  5062. rc = ata_scsi_add_hosts(host, sht);
  5063. if (rc)
  5064. goto err_tadd;
  5065. /* set cable, sata_spd_limit and report */
  5066. for (i = 0; i < host->n_ports; i++) {
  5067. struct ata_port *ap = host->ports[i];
  5068. unsigned int xfer_mask;
  5069. /* set SATA cable type if still unset */
  5070. if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
  5071. ap->cbl = ATA_CBL_SATA;
  5072. /* init sata_spd_limit to the current value */
  5073. sata_link_init_spd(&ap->link);
  5074. if (ap->slave_link)
  5075. sata_link_init_spd(ap->slave_link);
  5076. /* print per-port info to dmesg */
  5077. xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
  5078. ap->udma_mask);
  5079. if (!ata_port_is_dummy(ap)) {
  5080. ata_port_info(ap, "%cATA max %s %s\n",
  5081. (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
  5082. ata_mode_string(xfer_mask),
  5083. ap->link.eh_info.desc);
  5084. ata_ehi_clear_desc(&ap->link.eh_info);
  5085. } else
  5086. ata_port_info(ap, "DUMMY\n");
  5087. }
  5088. /* perform each probe asynchronously */
  5089. for (i = 0; i < host->n_ports; i++) {
  5090. struct ata_port *ap = host->ports[i];
  5091. ap->cookie = async_schedule(async_port_probe, ap);
  5092. }
  5093. return 0;
  5094. err_tadd:
  5095. while (--i >= 0) {
  5096. ata_tport_delete(host->ports[i]);
  5097. }
  5098. return rc;
  5099. }
  5100. EXPORT_SYMBOL_GPL(ata_host_register);
  5101. /**
  5102. * ata_host_activate - start host, request IRQ and register it
  5103. * @host: target ATA host
  5104. * @irq: IRQ to request
  5105. * @irq_handler: irq_handler used when requesting IRQ
  5106. * @irq_flags: irq_flags used when requesting IRQ
  5107. * @sht: scsi_host_template to use when registering the host
  5108. *
  5109. * After allocating an ATA host and initializing it, most libata
  5110. * LLDs perform three steps to activate the host - start host,
  5111. * request IRQ and register it. This helper takes necessary
  5112. * arguments and performs the three steps in one go.
  5113. *
  5114. * An invalid IRQ skips the IRQ registration and expects the host to
  5115. * have set polling mode on the port. In this case, @irq_handler
  5116. * should be NULL.
  5117. *
  5118. * LOCKING:
  5119. * Inherited from calling layer (may sleep).
  5120. *
  5121. * RETURNS:
  5122. * 0 on success, -errno otherwise.
  5123. */
  5124. int ata_host_activate(struct ata_host *host, int irq,
  5125. irq_handler_t irq_handler, unsigned long irq_flags,
  5126. struct scsi_host_template *sht)
  5127. {
  5128. int i, rc;
  5129. char *irq_desc;
  5130. rc = ata_host_start(host);
  5131. if (rc)
  5132. return rc;
  5133. /* Special case for polling mode */
  5134. if (!irq) {
  5135. WARN_ON(irq_handler);
  5136. return ata_host_register(host, sht);
  5137. }
  5138. irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
  5139. dev_driver_string(host->dev),
  5140. dev_name(host->dev));
  5141. if (!irq_desc)
  5142. return -ENOMEM;
  5143. rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
  5144. irq_desc, host);
  5145. if (rc)
  5146. return rc;
  5147. for (i = 0; i < host->n_ports; i++)
  5148. ata_port_desc(host->ports[i], "irq %d", irq);
  5149. rc = ata_host_register(host, sht);
  5150. /* if failed, just free the IRQ and leave ports alone */
  5151. if (rc)
  5152. devm_free_irq(host->dev, irq, host);
  5153. return rc;
  5154. }
  5155. EXPORT_SYMBOL_GPL(ata_host_activate);
  5156. /**
  5157. * ata_port_detach - Detach ATA port in preparation of device removal
  5158. * @ap: ATA port to be detached
  5159. *
  5160. * Detach all ATA devices and the associated SCSI devices of @ap;
  5161. * then, remove the associated SCSI host. @ap is guaranteed to
  5162. * be quiescent on return from this function.
  5163. *
  5164. * LOCKING:
  5165. * Kernel thread context (may sleep).
  5166. */
  5167. static void ata_port_detach(struct ata_port *ap)
  5168. {
  5169. unsigned long flags;
  5170. struct ata_link *link;
  5171. struct ata_device *dev;
  5172. if (!ap->ops->error_handler)
  5173. goto skip_eh;
  5174. /* Wait for any ongoing EH */
  5175. ata_port_wait_eh(ap);
  5176. mutex_lock(&ap->scsi_scan_mutex);
  5177. spin_lock_irqsave(ap->lock, flags);
  5178. /* Remove scsi devices */
  5179. ata_for_each_link(link, ap, HOST_FIRST) {
  5180. ata_for_each_dev(dev, link, ALL) {
  5181. if (dev->sdev) {
  5182. spin_unlock_irqrestore(ap->lock, flags);
  5183. scsi_remove_device(dev->sdev);
  5184. spin_lock_irqsave(ap->lock, flags);
  5185. dev->sdev = NULL;
  5186. }
  5187. }
  5188. }
  5189. /* Tell EH to disable all devices */
  5190. ap->pflags |= ATA_PFLAG_UNLOADING;
  5191. ata_port_schedule_eh(ap);
  5192. spin_unlock_irqrestore(ap->lock, flags);
  5193. mutex_unlock(&ap->scsi_scan_mutex);
  5194. /* wait till EH commits suicide */
  5195. ata_port_wait_eh(ap);
  5196. /* it better be dead now */
  5197. WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
  5198. cancel_delayed_work_sync(&ap->hotplug_task);
  5199. cancel_delayed_work_sync(&ap->scsi_rescan_task);
  5200. skip_eh:
  5201. /* clean up zpodd on port removal */
  5202. ata_for_each_link(link, ap, HOST_FIRST) {
  5203. ata_for_each_dev(dev, link, ALL) {
  5204. if (zpodd_dev_enabled(dev))
  5205. zpodd_exit(dev);
  5206. }
  5207. }
  5208. if (ap->pmp_link) {
  5209. int i;
  5210. for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
  5211. ata_tlink_delete(&ap->pmp_link[i]);
  5212. }
  5213. /* remove the associated SCSI host */
  5214. scsi_remove_host(ap->scsi_host);
  5215. ata_tport_delete(ap);
  5216. }
  5217. /**
  5218. * ata_host_detach - Detach all ports of an ATA host
  5219. * @host: Host to detach
  5220. *
  5221. * Detach all ports of @host.
  5222. *
  5223. * LOCKING:
  5224. * Kernel thread context (may sleep).
  5225. */
  5226. void ata_host_detach(struct ata_host *host)
  5227. {
  5228. int i;
  5229. for (i = 0; i < host->n_ports; i++) {
  5230. /* Ensure ata_port probe has completed */
  5231. async_synchronize_cookie(host->ports[i]->cookie + 1);
  5232. ata_port_detach(host->ports[i]);
  5233. }
  5234. /* the host is dead now, dissociate ACPI */
  5235. ata_acpi_dissociate(host);
  5236. }
  5237. EXPORT_SYMBOL_GPL(ata_host_detach);
  5238. #ifdef CONFIG_PCI
  5239. /**
  5240. * ata_pci_remove_one - PCI layer callback for device removal
  5241. * @pdev: PCI device that was removed
  5242. *
  5243. * PCI layer indicates to libata via this hook that hot-unplug or
  5244. * module unload event has occurred. Detach all ports. Resource
  5245. * release is handled via devres.
  5246. *
  5247. * LOCKING:
  5248. * Inherited from PCI layer (may sleep).
  5249. */
  5250. void ata_pci_remove_one(struct pci_dev *pdev)
  5251. {
  5252. struct ata_host *host = pci_get_drvdata(pdev);
  5253. ata_host_detach(host);
  5254. }
  5255. EXPORT_SYMBOL_GPL(ata_pci_remove_one);
  5256. void ata_pci_shutdown_one(struct pci_dev *pdev)
  5257. {
  5258. struct ata_host *host = pci_get_drvdata(pdev);
  5259. int i;
  5260. for (i = 0; i < host->n_ports; i++) {
  5261. struct ata_port *ap = host->ports[i];
  5262. ap->pflags |= ATA_PFLAG_FROZEN;
  5263. /* Disable port interrupts */
  5264. if (ap->ops->freeze)
  5265. ap->ops->freeze(ap);
  5266. /* Stop the port DMA engines */
  5267. if (ap->ops->port_stop)
  5268. ap->ops->port_stop(ap);
  5269. }
  5270. }
  5271. EXPORT_SYMBOL_GPL(ata_pci_shutdown_one);
  5272. /* move to PCI subsystem */
  5273. int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
  5274. {
  5275. unsigned long tmp = 0;
  5276. switch (bits->width) {
  5277. case 1: {
  5278. u8 tmp8 = 0;
  5279. pci_read_config_byte(pdev, bits->reg, &tmp8);
  5280. tmp = tmp8;
  5281. break;
  5282. }
  5283. case 2: {
  5284. u16 tmp16 = 0;
  5285. pci_read_config_word(pdev, bits->reg, &tmp16);
  5286. tmp = tmp16;
  5287. break;
  5288. }
  5289. case 4: {
  5290. u32 tmp32 = 0;
  5291. pci_read_config_dword(pdev, bits->reg, &tmp32);
  5292. tmp = tmp32;
  5293. break;
  5294. }
  5295. default:
  5296. return -EINVAL;
  5297. }
  5298. tmp &= bits->mask;
  5299. return (tmp == bits->val) ? 1 : 0;
  5300. }
  5301. EXPORT_SYMBOL_GPL(pci_test_config_bits);
  5302. #ifdef CONFIG_PM
  5303. void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
  5304. {
  5305. pci_save_state(pdev);
  5306. pci_disable_device(pdev);
  5307. if (mesg.event & PM_EVENT_SLEEP)
  5308. pci_set_power_state(pdev, PCI_D3hot);
  5309. }
  5310. EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
  5311. int ata_pci_device_do_resume(struct pci_dev *pdev)
  5312. {
  5313. int rc;
  5314. pci_set_power_state(pdev, PCI_D0);
  5315. pci_restore_state(pdev);
  5316. rc = pcim_enable_device(pdev);
  5317. if (rc) {
  5318. dev_err(&pdev->dev,
  5319. "failed to enable device after resume (%d)\n", rc);
  5320. return rc;
  5321. }
  5322. pci_set_master(pdev);
  5323. return 0;
  5324. }
  5325. EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
  5326. int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
  5327. {
  5328. struct ata_host *host = pci_get_drvdata(pdev);
  5329. ata_host_suspend(host, mesg);
  5330. ata_pci_device_do_suspend(pdev, mesg);
  5331. return 0;
  5332. }
  5333. EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
  5334. int ata_pci_device_resume(struct pci_dev *pdev)
  5335. {
  5336. struct ata_host *host = pci_get_drvdata(pdev);
  5337. int rc;
  5338. rc = ata_pci_device_do_resume(pdev);
  5339. if (rc == 0)
  5340. ata_host_resume(host);
  5341. return rc;
  5342. }
  5343. EXPORT_SYMBOL_GPL(ata_pci_device_resume);
  5344. #endif /* CONFIG_PM */
  5345. #endif /* CONFIG_PCI */
  5346. /**
  5347. * ata_platform_remove_one - Platform layer callback for device removal
  5348. * @pdev: Platform device that was removed
  5349. *
  5350. * Platform layer indicates to libata via this hook that hot-unplug or
  5351. * module unload event has occurred. Detach all ports. Resource
  5352. * release is handled via devres.
  5353. *
  5354. * LOCKING:
  5355. * Inherited from platform layer (may sleep).
  5356. */
  5357. int ata_platform_remove_one(struct platform_device *pdev)
  5358. {
  5359. struct ata_host *host = platform_get_drvdata(pdev);
  5360. ata_host_detach(host);
  5361. return 0;
  5362. }
  5363. EXPORT_SYMBOL_GPL(ata_platform_remove_one);
  5364. #ifdef CONFIG_ATA_FORCE
  5365. #define force_cbl(name, flag) \
  5366. { #name, .cbl = (flag) }
  5367. #define force_spd_limit(spd, val) \
  5368. { #spd, .spd_limit = (val) }
  5369. #define force_xfer(mode, shift) \
  5370. { #mode, .xfer_mask = (1UL << (shift)) }
  5371. #define force_lflag_on(name, flags) \
  5372. { #name, .lflags_on = (flags) }
  5373. #define force_lflag_onoff(name, flags) \
  5374. { "no" #name, .lflags_on = (flags) }, \
  5375. { #name, .lflags_off = (flags) }
  5376. #define force_horkage_on(name, flag) \
  5377. { #name, .horkage_on = (flag) }
  5378. #define force_horkage_onoff(name, flag) \
  5379. { "no" #name, .horkage_on = (flag) }, \
  5380. { #name, .horkage_off = (flag) }
  5381. static const struct ata_force_param force_tbl[] __initconst = {
  5382. force_cbl(40c, ATA_CBL_PATA40),
  5383. force_cbl(80c, ATA_CBL_PATA80),
  5384. force_cbl(short40c, ATA_CBL_PATA40_SHORT),
  5385. force_cbl(unk, ATA_CBL_PATA_UNK),
  5386. force_cbl(ign, ATA_CBL_PATA_IGN),
  5387. force_cbl(sata, ATA_CBL_SATA),
  5388. force_spd_limit(1.5Gbps, 1),
  5389. force_spd_limit(3.0Gbps, 2),
  5390. force_xfer(pio0, ATA_SHIFT_PIO + 0),
  5391. force_xfer(pio1, ATA_SHIFT_PIO + 1),
  5392. force_xfer(pio2, ATA_SHIFT_PIO + 2),
  5393. force_xfer(pio3, ATA_SHIFT_PIO + 3),
  5394. force_xfer(pio4, ATA_SHIFT_PIO + 4),
  5395. force_xfer(pio5, ATA_SHIFT_PIO + 5),
  5396. force_xfer(pio6, ATA_SHIFT_PIO + 6),
  5397. force_xfer(mwdma0, ATA_SHIFT_MWDMA + 0),
  5398. force_xfer(mwdma1, ATA_SHIFT_MWDMA + 1),
  5399. force_xfer(mwdma2, ATA_SHIFT_MWDMA + 2),
  5400. force_xfer(mwdma3, ATA_SHIFT_MWDMA + 3),
  5401. force_xfer(mwdma4, ATA_SHIFT_MWDMA + 4),
  5402. force_xfer(udma0, ATA_SHIFT_UDMA + 0),
  5403. force_xfer(udma16, ATA_SHIFT_UDMA + 0),
  5404. force_xfer(udma/16, ATA_SHIFT_UDMA + 0),
  5405. force_xfer(udma1, ATA_SHIFT_UDMA + 1),
  5406. force_xfer(udma25, ATA_SHIFT_UDMA + 1),
  5407. force_xfer(udma/25, ATA_SHIFT_UDMA + 1),
  5408. force_xfer(udma2, ATA_SHIFT_UDMA + 2),
  5409. force_xfer(udma33, ATA_SHIFT_UDMA + 2),
  5410. force_xfer(udma/33, ATA_SHIFT_UDMA + 2),
  5411. force_xfer(udma3, ATA_SHIFT_UDMA + 3),
  5412. force_xfer(udma44, ATA_SHIFT_UDMA + 3),
  5413. force_xfer(udma/44, ATA_SHIFT_UDMA + 3),
  5414. force_xfer(udma4, ATA_SHIFT_UDMA + 4),
  5415. force_xfer(udma66, ATA_SHIFT_UDMA + 4),
  5416. force_xfer(udma/66, ATA_SHIFT_UDMA + 4),
  5417. force_xfer(udma5, ATA_SHIFT_UDMA + 5),
  5418. force_xfer(udma100, ATA_SHIFT_UDMA + 5),
  5419. force_xfer(udma/100, ATA_SHIFT_UDMA + 5),
  5420. force_xfer(udma6, ATA_SHIFT_UDMA + 6),
  5421. force_xfer(udma133, ATA_SHIFT_UDMA + 6),
  5422. force_xfer(udma/133, ATA_SHIFT_UDMA + 6),
  5423. force_xfer(udma7, ATA_SHIFT_UDMA + 7),
  5424. force_lflag_on(nohrst, ATA_LFLAG_NO_HRST),
  5425. force_lflag_on(nosrst, ATA_LFLAG_NO_SRST),
  5426. force_lflag_on(norst, ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST),
  5427. force_lflag_on(rstonce, ATA_LFLAG_RST_ONCE),
  5428. force_lflag_onoff(dbdelay, ATA_LFLAG_NO_DEBOUNCE_DELAY),
  5429. force_horkage_onoff(ncq, ATA_HORKAGE_NONCQ),
  5430. force_horkage_onoff(ncqtrim, ATA_HORKAGE_NO_NCQ_TRIM),
  5431. force_horkage_onoff(ncqati, ATA_HORKAGE_NO_NCQ_ON_ATI),
  5432. force_horkage_onoff(trim, ATA_HORKAGE_NOTRIM),
  5433. force_horkage_on(trim_zero, ATA_HORKAGE_ZERO_AFTER_TRIM),
  5434. force_horkage_on(max_trim_128m, ATA_HORKAGE_MAX_TRIM_128M),
  5435. force_horkage_onoff(dma, ATA_HORKAGE_NODMA),
  5436. force_horkage_on(atapi_dmadir, ATA_HORKAGE_ATAPI_DMADIR),
  5437. force_horkage_on(atapi_mod16_dma, ATA_HORKAGE_ATAPI_MOD16_DMA),
  5438. force_horkage_onoff(dmalog, ATA_HORKAGE_NO_DMA_LOG),
  5439. force_horkage_onoff(iddevlog, ATA_HORKAGE_NO_ID_DEV_LOG),
  5440. force_horkage_onoff(logdir, ATA_HORKAGE_NO_LOG_DIR),
  5441. force_horkage_on(max_sec_128, ATA_HORKAGE_MAX_SEC_128),
  5442. force_horkage_on(max_sec_1024, ATA_HORKAGE_MAX_SEC_1024),
  5443. force_horkage_on(max_sec_lba48, ATA_HORKAGE_MAX_SEC_LBA48),
  5444. force_horkage_onoff(lpm, ATA_HORKAGE_NOLPM),
  5445. force_horkage_onoff(setxfer, ATA_HORKAGE_NOSETXFER),
  5446. force_horkage_on(dump_id, ATA_HORKAGE_DUMP_ID),
  5447. force_horkage_on(disable, ATA_HORKAGE_DISABLE),
  5448. };
  5449. static int __init ata_parse_force_one(char **cur,
  5450. struct ata_force_ent *force_ent,
  5451. const char **reason)
  5452. {
  5453. char *start = *cur, *p = *cur;
  5454. char *id, *val, *endp;
  5455. const struct ata_force_param *match_fp = NULL;
  5456. int nr_matches = 0, i;
  5457. /* find where this param ends and update *cur */
  5458. while (*p != '\0' && *p != ',')
  5459. p++;
  5460. if (*p == '\0')
  5461. *cur = p;
  5462. else
  5463. *cur = p + 1;
  5464. *p = '\0';
  5465. /* parse */
  5466. p = strchr(start, ':');
  5467. if (!p) {
  5468. val = strstrip(start);
  5469. goto parse_val;
  5470. }
  5471. *p = '\0';
  5472. id = strstrip(start);
  5473. val = strstrip(p + 1);
  5474. /* parse id */
  5475. p = strchr(id, '.');
  5476. if (p) {
  5477. *p++ = '\0';
  5478. force_ent->device = simple_strtoul(p, &endp, 10);
  5479. if (p == endp || *endp != '\0') {
  5480. *reason = "invalid device";
  5481. return -EINVAL;
  5482. }
  5483. }
  5484. force_ent->port = simple_strtoul(id, &endp, 10);
  5485. if (id == endp || *endp != '\0') {
  5486. *reason = "invalid port/link";
  5487. return -EINVAL;
  5488. }
  5489. parse_val:
  5490. /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
  5491. for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
  5492. const struct ata_force_param *fp = &force_tbl[i];
  5493. if (strncasecmp(val, fp->name, strlen(val)))
  5494. continue;
  5495. nr_matches++;
  5496. match_fp = fp;
  5497. if (strcasecmp(val, fp->name) == 0) {
  5498. nr_matches = 1;
  5499. break;
  5500. }
  5501. }
  5502. if (!nr_matches) {
  5503. *reason = "unknown value";
  5504. return -EINVAL;
  5505. }
  5506. if (nr_matches > 1) {
  5507. *reason = "ambiguous value";
  5508. return -EINVAL;
  5509. }
  5510. force_ent->param = *match_fp;
  5511. return 0;
  5512. }
  5513. static void __init ata_parse_force_param(void)
  5514. {
  5515. int idx = 0, size = 1;
  5516. int last_port = -1, last_device = -1;
  5517. char *p, *cur, *next;
  5518. /* Calculate maximum number of params and allocate ata_force_tbl */
  5519. for (p = ata_force_param_buf; *p; p++)
  5520. if (*p == ',')
  5521. size++;
  5522. ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL);
  5523. if (!ata_force_tbl) {
  5524. printk(KERN_WARNING "ata: failed to extend force table, "
  5525. "libata.force ignored\n");
  5526. return;
  5527. }
  5528. /* parse and populate the table */
  5529. for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
  5530. const char *reason = "";
  5531. struct ata_force_ent te = { .port = -1, .device = -1 };
  5532. next = cur;
  5533. if (ata_parse_force_one(&next, &te, &reason)) {
  5534. printk(KERN_WARNING "ata: failed to parse force "
  5535. "parameter \"%s\" (%s)\n",
  5536. cur, reason);
  5537. continue;
  5538. }
  5539. if (te.port == -1) {
  5540. te.port = last_port;
  5541. te.device = last_device;
  5542. }
  5543. ata_force_tbl[idx++] = te;
  5544. last_port = te.port;
  5545. last_device = te.device;
  5546. }
  5547. ata_force_tbl_size = idx;
  5548. }
  5549. static void ata_free_force_param(void)
  5550. {
  5551. kfree(ata_force_tbl);
  5552. }
  5553. #else
  5554. static inline void ata_parse_force_param(void) { }
  5555. static inline void ata_free_force_param(void) { }
  5556. #endif
  5557. static int __init ata_init(void)
  5558. {
  5559. int rc;
  5560. ata_parse_force_param();
  5561. rc = ata_sff_init();
  5562. if (rc) {
  5563. ata_free_force_param();
  5564. return rc;
  5565. }
  5566. libata_transport_init();
  5567. ata_scsi_transport_template = ata_attach_transport();
  5568. if (!ata_scsi_transport_template) {
  5569. ata_sff_exit();
  5570. rc = -ENOMEM;
  5571. goto err_out;
  5572. }
  5573. printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
  5574. return 0;
  5575. err_out:
  5576. return rc;
  5577. }
  5578. static void __exit ata_exit(void)
  5579. {
  5580. ata_release_transport(ata_scsi_transport_template);
  5581. libata_transport_exit();
  5582. ata_sff_exit();
  5583. ata_free_force_param();
  5584. }
  5585. subsys_initcall(ata_init);
  5586. module_exit(ata_exit);
  5587. static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
  5588. int ata_ratelimit(void)
  5589. {
  5590. return __ratelimit(&ratelimit);
  5591. }
  5592. EXPORT_SYMBOL_GPL(ata_ratelimit);
  5593. /**
  5594. * ata_msleep - ATA EH owner aware msleep
  5595. * @ap: ATA port to attribute the sleep to
  5596. * @msecs: duration to sleep in milliseconds
  5597. *
  5598. * Sleeps @msecs. If the current task is owner of @ap's EH, the
  5599. * ownership is released before going to sleep and reacquired
  5600. * after the sleep is complete. IOW, other ports sharing the
  5601. * @ap->host will be allowed to own the EH while this task is
  5602. * sleeping.
  5603. *
  5604. * LOCKING:
  5605. * Might sleep.
  5606. */
  5607. void ata_msleep(struct ata_port *ap, unsigned int msecs)
  5608. {
  5609. bool owns_eh = ap && ap->host->eh_owner == current;
  5610. if (owns_eh)
  5611. ata_eh_release(ap);
  5612. if (msecs < 20) {
  5613. unsigned long usecs = msecs * USEC_PER_MSEC;
  5614. usleep_range(usecs, usecs + 50);
  5615. } else {
  5616. msleep(msecs);
  5617. }
  5618. if (owns_eh)
  5619. ata_eh_acquire(ap);
  5620. }
  5621. EXPORT_SYMBOL_GPL(ata_msleep);
  5622. /**
  5623. * ata_wait_register - wait until register value changes
  5624. * @ap: ATA port to wait register for, can be NULL
  5625. * @reg: IO-mapped register
  5626. * @mask: Mask to apply to read register value
  5627. * @val: Wait condition
  5628. * @interval: polling interval in milliseconds
  5629. * @timeout: timeout in milliseconds
  5630. *
  5631. * Waiting for some bits of register to change is a common
  5632. * operation for ATA controllers. This function reads 32bit LE
  5633. * IO-mapped register @reg and tests for the following condition.
  5634. *
  5635. * (*@reg & mask) != val
  5636. *
  5637. * If the condition is met, it returns; otherwise, the process is
  5638. * repeated after @interval_msec until timeout.
  5639. *
  5640. * LOCKING:
  5641. * Kernel thread context (may sleep)
  5642. *
  5643. * RETURNS:
  5644. * The final register value.
  5645. */
  5646. u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
  5647. unsigned long interval, unsigned long timeout)
  5648. {
  5649. unsigned long deadline;
  5650. u32 tmp;
  5651. tmp = ioread32(reg);
  5652. /* Calculate timeout _after_ the first read to make sure
  5653. * preceding writes reach the controller before starting to
  5654. * eat away the timeout.
  5655. */
  5656. deadline = ata_deadline(jiffies, timeout);
  5657. while ((tmp & mask) == val && time_before(jiffies, deadline)) {
  5658. ata_msleep(ap, interval);
  5659. tmp = ioread32(reg);
  5660. }
  5661. return tmp;
  5662. }
  5663. EXPORT_SYMBOL_GPL(ata_wait_register);
  5664. /*
  5665. * Dummy port_ops
  5666. */
  5667. static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
  5668. {
  5669. return AC_ERR_SYSTEM;
  5670. }
  5671. static void ata_dummy_error_handler(struct ata_port *ap)
  5672. {
  5673. /* truly dummy */
  5674. }
  5675. struct ata_port_operations ata_dummy_port_ops = {
  5676. .qc_prep = ata_noop_qc_prep,
  5677. .qc_issue = ata_dummy_qc_issue,
  5678. .error_handler = ata_dummy_error_handler,
  5679. .sched_eh = ata_std_sched_eh,
  5680. .end_eh = ata_std_end_eh,
  5681. };
  5682. EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
  5683. const struct ata_port_info ata_dummy_port_info = {
  5684. .port_ops = &ata_dummy_port_ops,
  5685. };
  5686. EXPORT_SYMBOL_GPL(ata_dummy_port_info);
  5687. void ata_print_version(const struct device *dev, const char *version)
  5688. {
  5689. dev_printk(KERN_DEBUG, dev, "version %s\n", version);
  5690. }
  5691. EXPORT_SYMBOL(ata_print_version);
  5692. EXPORT_TRACEPOINT_SYMBOL_GPL(ata_tf_load);
  5693. EXPORT_TRACEPOINT_SYMBOL_GPL(ata_exec_command);
  5694. EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_setup);
  5695. EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_start);
  5696. EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_status);