libata-core.c 112 KB

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