libata-core.c 153 KB

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