libata-core.c 166 KB

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