libata-core.c 171 KB

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