libata-core.c 177 KB

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