libata-core.c 148 KB

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