libata-core.c 139 KB

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