libata-core.c 173 KB

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