libata-core.c 158 KB

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