libata-core.c 152 KB

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