libata-core.c 118 KB

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