libata-core.c 120 KB

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