libata-core.c 184 KB

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