libata-core.c 169 KB

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