libata-core.c 157 KB

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