libata-core.c 93 KB

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