libata-core.c 170 KB

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