libata-core.c 154 KB

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