libata-core.c 179 KB

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