libata-core.c 109 KB

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