libata-core.c 158 KB

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