libata-core.c 174 KB

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