libata-core.c 155 KB

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