libata-core.c 191 KB

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