libata-core.c 175 KB

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