libata-core.c 169 KB

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