libata-core.c 186 KB

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