libata-core.c 118 KB

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