libata-core.c 156 KB

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