libata-core.c 192 KB

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