libata-core.c 166 KB

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