libata-core.c 178 KB

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