libata-core.c 193 KB

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