libata-core.c 180 KB

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