libata-core.c 173 KB

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