libata-core.c 197 KB

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