libata-core.c 120 KB

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