libata-core.c 192 KB

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