libata-core.c 191 KB

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