sata_mv.c 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749
  1. /*
  2. * sata_mv.c - Marvell SATA support
  3. *
  4. * Copyright 2008: Marvell Corporation, all rights reserved.
  5. * Copyright 2005: EMC Corporation, all rights reserved.
  6. * Copyright 2005 Red Hat, Inc. All rights reserved.
  7. *
  8. * Please ALWAYS copy linux-ide@vger.kernel.org on emails.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; version 2 of the License.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. /*
  25. * sata_mv TODO list:
  26. *
  27. * --> Errata workaround for NCQ device errors.
  28. *
  29. * --> More errata workarounds for PCI-X.
  30. *
  31. * --> Complete a full errata audit for all chipsets to identify others.
  32. *
  33. * --> Develop a low-power-consumption strategy, and implement it.
  34. *
  35. * --> [Experiment, low priority] Investigate interrupt coalescing.
  36. * Quite often, especially with PCI Message Signalled Interrupts (MSI),
  37. * the overhead reduced by interrupt mitigation is quite often not
  38. * worth the latency cost.
  39. *
  40. * --> [Experiment, Marvell value added] Is it possible to use target
  41. * mode to cross-connect two Linux boxes with Marvell cards? If so,
  42. * creating LibATA target mode support would be very interesting.
  43. *
  44. * Target mode, for those without docs, is the ability to directly
  45. * connect two SATA ports.
  46. */
  47. #include <linux/kernel.h>
  48. #include <linux/module.h>
  49. #include <linux/pci.h>
  50. #include <linux/init.h>
  51. #include <linux/blkdev.h>
  52. #include <linux/delay.h>
  53. #include <linux/interrupt.h>
  54. #include <linux/dmapool.h>
  55. #include <linux/dma-mapping.h>
  56. #include <linux/device.h>
  57. #include <linux/platform_device.h>
  58. #include <linux/ata_platform.h>
  59. #include <linux/mbus.h>
  60. #include <linux/bitops.h>
  61. #include <scsi/scsi_host.h>
  62. #include <scsi/scsi_cmnd.h>
  63. #include <scsi/scsi_device.h>
  64. #include <linux/libata.h>
  65. #define DRV_NAME "sata_mv"
  66. #define DRV_VERSION "1.26"
  67. enum {
  68. /* BAR's are enumerated in terms of pci_resource_start() terms */
  69. MV_PRIMARY_BAR = 0, /* offset 0x10: memory space */
  70. MV_IO_BAR = 2, /* offset 0x18: IO space */
  71. MV_MISC_BAR = 3, /* offset 0x1c: FLASH, NVRAM, SRAM */
  72. MV_MAJOR_REG_AREA_SZ = 0x10000, /* 64KB */
  73. MV_MINOR_REG_AREA_SZ = 0x2000, /* 8KB */
  74. MV_PCI_REG_BASE = 0,
  75. MV_IRQ_COAL_REG_BASE = 0x18000, /* 6xxx part only */
  76. MV_IRQ_COAL_CAUSE = (MV_IRQ_COAL_REG_BASE + 0x08),
  77. MV_IRQ_COAL_CAUSE_LO = (MV_IRQ_COAL_REG_BASE + 0x88),
  78. MV_IRQ_COAL_CAUSE_HI = (MV_IRQ_COAL_REG_BASE + 0x8c),
  79. MV_IRQ_COAL_THRESHOLD = (MV_IRQ_COAL_REG_BASE + 0xcc),
  80. MV_IRQ_COAL_TIME_THRESHOLD = (MV_IRQ_COAL_REG_BASE + 0xd0),
  81. MV_SATAHC0_REG_BASE = 0x20000,
  82. MV_FLASH_CTL_OFS = 0x1046c,
  83. MV_GPIO_PORT_CTL_OFS = 0x104f0,
  84. MV_RESET_CFG_OFS = 0x180d8,
  85. MV_PCI_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  86. MV_SATAHC_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  87. MV_SATAHC_ARBTR_REG_SZ = MV_MINOR_REG_AREA_SZ, /* arbiter */
  88. MV_PORT_REG_SZ = MV_MINOR_REG_AREA_SZ,
  89. MV_MAX_Q_DEPTH = 32,
  90. MV_MAX_Q_DEPTH_MASK = MV_MAX_Q_DEPTH - 1,
  91. /* CRQB needs alignment on a 1KB boundary. Size == 1KB
  92. * CRPB needs alignment on a 256B boundary. Size == 256B
  93. * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
  94. */
  95. MV_CRQB_Q_SZ = (32 * MV_MAX_Q_DEPTH),
  96. MV_CRPB_Q_SZ = (8 * MV_MAX_Q_DEPTH),
  97. MV_MAX_SG_CT = 256,
  98. MV_SG_TBL_SZ = (16 * MV_MAX_SG_CT),
  99. /* Determine hc from 0-7 port: hc = port >> MV_PORT_HC_SHIFT */
  100. MV_PORT_HC_SHIFT = 2,
  101. MV_PORTS_PER_HC = (1 << MV_PORT_HC_SHIFT), /* 4 */
  102. /* Determine hc port from 0-7 port: hardport = port & MV_PORT_MASK */
  103. MV_PORT_MASK = (MV_PORTS_PER_HC - 1), /* 3 */
  104. /* Host Flags */
  105. MV_FLAG_DUAL_HC = (1 << 30), /* two SATA Host Controllers */
  106. MV_FLAG_IRQ_COALESCE = (1 << 29), /* IRQ coalescing capability */
  107. MV_COMMON_FLAGS = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  108. ATA_FLAG_MMIO | ATA_FLAG_PIO_POLLING,
  109. MV_GEN_I_FLAGS = MV_COMMON_FLAGS | ATA_FLAG_NO_ATAPI,
  110. MV_GEN_II_FLAGS = MV_COMMON_FLAGS | MV_FLAG_IRQ_COALESCE |
  111. ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
  112. ATA_FLAG_NCQ,
  113. MV_GEN_IIE_FLAGS = MV_GEN_II_FLAGS | ATA_FLAG_AN,
  114. CRQB_FLAG_READ = (1 << 0),
  115. CRQB_TAG_SHIFT = 1,
  116. CRQB_IOID_SHIFT = 6, /* CRQB Gen-II/IIE IO Id shift */
  117. CRQB_PMP_SHIFT = 12, /* CRQB Gen-II/IIE PMP shift */
  118. CRQB_HOSTQ_SHIFT = 17, /* CRQB Gen-II/IIE HostQueTag shift */
  119. CRQB_CMD_ADDR_SHIFT = 8,
  120. CRQB_CMD_CS = (0x2 << 11),
  121. CRQB_CMD_LAST = (1 << 15),
  122. CRPB_FLAG_STATUS_SHIFT = 8,
  123. CRPB_IOID_SHIFT_6 = 5, /* CRPB Gen-II IO Id shift */
  124. CRPB_IOID_SHIFT_7 = 7, /* CRPB Gen-IIE IO Id shift */
  125. EPRD_FLAG_END_OF_TBL = (1 << 31),
  126. /* PCI interface registers */
  127. PCI_COMMAND_OFS = 0xc00,
  128. PCI_COMMAND_MRDTRIG = (1 << 7), /* PCI Master Read Trigger */
  129. PCI_MAIN_CMD_STS_OFS = 0xd30,
  130. STOP_PCI_MASTER = (1 << 2),
  131. PCI_MASTER_EMPTY = (1 << 3),
  132. GLOB_SFT_RST = (1 << 4),
  133. MV_PCI_MODE_OFS = 0xd00,
  134. MV_PCI_MODE_MASK = 0x30,
  135. MV_PCI_EXP_ROM_BAR_CTL = 0xd2c,
  136. MV_PCI_DISC_TIMER = 0xd04,
  137. MV_PCI_MSI_TRIGGER = 0xc38,
  138. MV_PCI_SERR_MASK = 0xc28,
  139. MV_PCI_XBAR_TMOUT_OFS = 0x1d04,
  140. MV_PCI_ERR_LOW_ADDRESS = 0x1d40,
  141. MV_PCI_ERR_HIGH_ADDRESS = 0x1d44,
  142. MV_PCI_ERR_ATTRIBUTE = 0x1d48,
  143. MV_PCI_ERR_COMMAND = 0x1d50,
  144. PCI_IRQ_CAUSE_OFS = 0x1d58,
  145. PCI_IRQ_MASK_OFS = 0x1d5c,
  146. PCI_UNMASK_ALL_IRQS = 0x7fffff, /* bits 22-0 */
  147. PCIE_IRQ_CAUSE_OFS = 0x1900,
  148. PCIE_IRQ_MASK_OFS = 0x1910,
  149. PCIE_UNMASK_ALL_IRQS = 0x40a, /* assorted bits */
  150. /* Host Controller Main Interrupt Cause/Mask registers (1 per-chip) */
  151. PCI_HC_MAIN_IRQ_CAUSE_OFS = 0x1d60,
  152. PCI_HC_MAIN_IRQ_MASK_OFS = 0x1d64,
  153. SOC_HC_MAIN_IRQ_CAUSE_OFS = 0x20020,
  154. SOC_HC_MAIN_IRQ_MASK_OFS = 0x20024,
  155. ERR_IRQ = (1 << 0), /* shift by port # */
  156. DONE_IRQ = (1 << 1), /* shift by port # */
  157. HC0_IRQ_PEND = 0x1ff, /* bits 0-8 = HC0's ports */
  158. HC_SHIFT = 9, /* bits 9-17 = HC1's ports */
  159. PCI_ERR = (1 << 18),
  160. TRAN_LO_DONE = (1 << 19), /* 6xxx: IRQ coalescing */
  161. TRAN_HI_DONE = (1 << 20), /* 6xxx: IRQ coalescing */
  162. PORTS_0_3_COAL_DONE = (1 << 8),
  163. PORTS_4_7_COAL_DONE = (1 << 17),
  164. PORTS_0_7_COAL_DONE = (1 << 21), /* 6xxx: IRQ coalescing */
  165. GPIO_INT = (1 << 22),
  166. SELF_INT = (1 << 23),
  167. TWSI_INT = (1 << 24),
  168. HC_MAIN_RSVD = (0x7f << 25), /* bits 31-25 */
  169. HC_MAIN_RSVD_5 = (0x1fff << 19), /* bits 31-19 */
  170. HC_MAIN_RSVD_SOC = (0x3fffffb << 6), /* bits 31-9, 7-6 */
  171. /* SATAHC registers */
  172. HC_CFG_OFS = 0,
  173. HC_IRQ_CAUSE_OFS = 0x14,
  174. DMA_IRQ = (1 << 0), /* shift by port # */
  175. HC_COAL_IRQ = (1 << 4), /* IRQ coalescing */
  176. DEV_IRQ = (1 << 8), /* shift by port # */
  177. /* Shadow block registers */
  178. SHD_BLK_OFS = 0x100,
  179. SHD_CTL_AST_OFS = 0x20, /* ofs from SHD_BLK_OFS */
  180. /* SATA registers */
  181. SATA_STATUS_OFS = 0x300, /* ctrl, err regs follow status */
  182. SATA_ACTIVE_OFS = 0x350,
  183. SATA_FIS_IRQ_CAUSE_OFS = 0x364,
  184. SATA_FIS_IRQ_AN = (1 << 9), /* async notification */
  185. LTMODE_OFS = 0x30c,
  186. LTMODE_BIT8 = (1 << 8), /* unknown, but necessary */
  187. PHY_MODE3 = 0x310,
  188. PHY_MODE4 = 0x314,
  189. PHY_MODE4_CFG_MASK = 0x00000003, /* phy internal config field */
  190. PHY_MODE4_CFG_VALUE = 0x00000001, /* phy internal config field */
  191. PHY_MODE4_RSVD_ZEROS = 0x5de3fffa, /* Gen2e always write zeros */
  192. PHY_MODE4_RSVD_ONES = 0x00000005, /* Gen2e always write ones */
  193. PHY_MODE2 = 0x330,
  194. SATA_IFCTL_OFS = 0x344,
  195. SATA_TESTCTL_OFS = 0x348,
  196. SATA_IFSTAT_OFS = 0x34c,
  197. VENDOR_UNIQUE_FIS_OFS = 0x35c,
  198. FISCFG_OFS = 0x360,
  199. FISCFG_WAIT_DEV_ERR = (1 << 8), /* wait for host on DevErr */
  200. FISCFG_SINGLE_SYNC = (1 << 16), /* SYNC on DMA activation */
  201. MV5_PHY_MODE = 0x74,
  202. MV5_LTMODE_OFS = 0x30,
  203. MV5_PHY_CTL_OFS = 0x0C,
  204. SATA_INTERFACE_CFG_OFS = 0x050,
  205. MV_M2_PREAMP_MASK = 0x7e0,
  206. /* Port registers */
  207. EDMA_CFG_OFS = 0,
  208. EDMA_CFG_Q_DEPTH = 0x1f, /* max device queue depth */
  209. EDMA_CFG_NCQ = (1 << 5), /* for R/W FPDMA queued */
  210. EDMA_CFG_NCQ_GO_ON_ERR = (1 << 14), /* continue on error */
  211. EDMA_CFG_RD_BRST_EXT = (1 << 11), /* read burst 512B */
  212. EDMA_CFG_WR_BUFF_LEN = (1 << 13), /* write buffer 512B */
  213. EDMA_CFG_EDMA_FBS = (1 << 16), /* EDMA FIS-Based Switching */
  214. EDMA_CFG_FBS = (1 << 26), /* FIS-Based Switching */
  215. EDMA_ERR_IRQ_CAUSE_OFS = 0x8,
  216. EDMA_ERR_IRQ_MASK_OFS = 0xc,
  217. EDMA_ERR_D_PAR = (1 << 0), /* UDMA data parity err */
  218. EDMA_ERR_PRD_PAR = (1 << 1), /* UDMA PRD parity err */
  219. EDMA_ERR_DEV = (1 << 2), /* device error */
  220. EDMA_ERR_DEV_DCON = (1 << 3), /* device disconnect */
  221. EDMA_ERR_DEV_CON = (1 << 4), /* device connected */
  222. EDMA_ERR_SERR = (1 << 5), /* SError bits [WBDST] raised */
  223. EDMA_ERR_SELF_DIS = (1 << 7), /* Gen II/IIE self-disable */
  224. EDMA_ERR_SELF_DIS_5 = (1 << 8), /* Gen I self-disable */
  225. EDMA_ERR_BIST_ASYNC = (1 << 8), /* BIST FIS or Async Notify */
  226. EDMA_ERR_TRANS_IRQ_7 = (1 << 8), /* Gen IIE transprt layer irq */
  227. EDMA_ERR_CRQB_PAR = (1 << 9), /* CRQB parity error */
  228. EDMA_ERR_CRPB_PAR = (1 << 10), /* CRPB parity error */
  229. EDMA_ERR_INTRL_PAR = (1 << 11), /* internal parity error */
  230. EDMA_ERR_IORDY = (1 << 12), /* IORdy timeout */
  231. EDMA_ERR_LNK_CTRL_RX = (0xf << 13), /* link ctrl rx error */
  232. EDMA_ERR_LNK_CTRL_RX_0 = (1 << 13), /* transient: CRC err */
  233. EDMA_ERR_LNK_CTRL_RX_1 = (1 << 14), /* transient: FIFO err */
  234. EDMA_ERR_LNK_CTRL_RX_2 = (1 << 15), /* fatal: caught SYNC */
  235. EDMA_ERR_LNK_CTRL_RX_3 = (1 << 16), /* transient: FIS rx err */
  236. EDMA_ERR_LNK_DATA_RX = (0xf << 17), /* link data rx error */
  237. EDMA_ERR_LNK_CTRL_TX = (0x1f << 21), /* link ctrl tx error */
  238. EDMA_ERR_LNK_CTRL_TX_0 = (1 << 21), /* transient: CRC err */
  239. EDMA_ERR_LNK_CTRL_TX_1 = (1 << 22), /* transient: FIFO err */
  240. EDMA_ERR_LNK_CTRL_TX_2 = (1 << 23), /* transient: caught SYNC */
  241. EDMA_ERR_LNK_CTRL_TX_3 = (1 << 24), /* transient: caught DMAT */
  242. EDMA_ERR_LNK_CTRL_TX_4 = (1 << 25), /* transient: FIS collision */
  243. EDMA_ERR_LNK_DATA_TX = (0x1f << 26), /* link data tx error */
  244. EDMA_ERR_TRANS_PROTO = (1 << 31), /* transport protocol error */
  245. EDMA_ERR_OVERRUN_5 = (1 << 5),
  246. EDMA_ERR_UNDERRUN_5 = (1 << 6),
  247. EDMA_ERR_IRQ_TRANSIENT = EDMA_ERR_LNK_CTRL_RX_0 |
  248. EDMA_ERR_LNK_CTRL_RX_1 |
  249. EDMA_ERR_LNK_CTRL_RX_3 |
  250. EDMA_ERR_LNK_CTRL_TX,
  251. EDMA_EH_FREEZE = EDMA_ERR_D_PAR |
  252. EDMA_ERR_PRD_PAR |
  253. EDMA_ERR_DEV_DCON |
  254. EDMA_ERR_DEV_CON |
  255. EDMA_ERR_SERR |
  256. EDMA_ERR_SELF_DIS |
  257. EDMA_ERR_CRQB_PAR |
  258. EDMA_ERR_CRPB_PAR |
  259. EDMA_ERR_INTRL_PAR |
  260. EDMA_ERR_IORDY |
  261. EDMA_ERR_LNK_CTRL_RX_2 |
  262. EDMA_ERR_LNK_DATA_RX |
  263. EDMA_ERR_LNK_DATA_TX |
  264. EDMA_ERR_TRANS_PROTO,
  265. EDMA_EH_FREEZE_5 = EDMA_ERR_D_PAR |
  266. EDMA_ERR_PRD_PAR |
  267. EDMA_ERR_DEV_DCON |
  268. EDMA_ERR_DEV_CON |
  269. EDMA_ERR_OVERRUN_5 |
  270. EDMA_ERR_UNDERRUN_5 |
  271. EDMA_ERR_SELF_DIS_5 |
  272. EDMA_ERR_CRQB_PAR |
  273. EDMA_ERR_CRPB_PAR |
  274. EDMA_ERR_INTRL_PAR |
  275. EDMA_ERR_IORDY,
  276. EDMA_REQ_Q_BASE_HI_OFS = 0x10,
  277. EDMA_REQ_Q_IN_PTR_OFS = 0x14, /* also contains BASE_LO */
  278. EDMA_REQ_Q_OUT_PTR_OFS = 0x18,
  279. EDMA_REQ_Q_PTR_SHIFT = 5,
  280. EDMA_RSP_Q_BASE_HI_OFS = 0x1c,
  281. EDMA_RSP_Q_IN_PTR_OFS = 0x20,
  282. EDMA_RSP_Q_OUT_PTR_OFS = 0x24, /* also contains BASE_LO */
  283. EDMA_RSP_Q_PTR_SHIFT = 3,
  284. EDMA_CMD_OFS = 0x28, /* EDMA command register */
  285. EDMA_EN = (1 << 0), /* enable EDMA */
  286. EDMA_DS = (1 << 1), /* disable EDMA; self-negated */
  287. EDMA_RESET = (1 << 2), /* reset eng/trans/link/phy */
  288. EDMA_STATUS_OFS = 0x30, /* EDMA engine status */
  289. EDMA_STATUS_CACHE_EMPTY = (1 << 6), /* GenIIe command cache empty */
  290. EDMA_STATUS_IDLE = (1 << 7), /* GenIIe EDMA enabled/idle */
  291. EDMA_IORDY_TMOUT_OFS = 0x34,
  292. EDMA_ARB_CFG_OFS = 0x38,
  293. EDMA_HALTCOND_OFS = 0x60, /* GenIIe halt conditions */
  294. EDMA_UNKNOWN_RSVD_OFS = 0x6C, /* GenIIe unknown/reserved */
  295. BMDMA_CMD_OFS = 0x224, /* bmdma command register */
  296. BMDMA_STATUS_OFS = 0x228, /* bmdma status register */
  297. BMDMA_PRD_LOW_OFS = 0x22c, /* bmdma PRD addr 31:0 */
  298. BMDMA_PRD_HIGH_OFS = 0x230, /* bmdma PRD addr 63:32 */
  299. /* Host private flags (hp_flags) */
  300. MV_HP_FLAG_MSI = (1 << 0),
  301. MV_HP_ERRATA_50XXB0 = (1 << 1),
  302. MV_HP_ERRATA_50XXB2 = (1 << 2),
  303. MV_HP_ERRATA_60X1B2 = (1 << 3),
  304. MV_HP_ERRATA_60X1C0 = (1 << 4),
  305. MV_HP_GEN_I = (1 << 6), /* Generation I: 50xx */
  306. MV_HP_GEN_II = (1 << 7), /* Generation II: 60xx */
  307. MV_HP_GEN_IIE = (1 << 8), /* Generation IIE: 6042/7042 */
  308. MV_HP_PCIE = (1 << 9), /* PCIe bus/regs: 7042 */
  309. MV_HP_CUT_THROUGH = (1 << 10), /* can use EDMA cut-through */
  310. MV_HP_FLAG_SOC = (1 << 11), /* SystemOnChip, no PCI */
  311. /* Port private flags (pp_flags) */
  312. MV_PP_FLAG_EDMA_EN = (1 << 0), /* is EDMA engine enabled? */
  313. MV_PP_FLAG_NCQ_EN = (1 << 1), /* is EDMA set up for NCQ? */
  314. MV_PP_FLAG_FBS_EN = (1 << 2), /* is EDMA set up for FBS? */
  315. MV_PP_FLAG_DELAYED_EH = (1 << 3), /* delayed dev err handling */
  316. };
  317. #define IS_GEN_I(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_I)
  318. #define IS_GEN_II(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_II)
  319. #define IS_GEN_IIE(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_IIE)
  320. #define IS_PCIE(hpriv) ((hpriv)->hp_flags & MV_HP_PCIE)
  321. #define IS_SOC(hpriv) ((hpriv)->hp_flags & MV_HP_FLAG_SOC)
  322. #define WINDOW_CTRL(i) (0x20030 + ((i) << 4))
  323. #define WINDOW_BASE(i) (0x20034 + ((i) << 4))
  324. enum {
  325. /* DMA boundary 0xffff is required by the s/g splitting
  326. * we need on /length/ in mv_fill-sg().
  327. */
  328. MV_DMA_BOUNDARY = 0xffffU,
  329. /* mask of register bits containing lower 32 bits
  330. * of EDMA request queue DMA address
  331. */
  332. EDMA_REQ_Q_BASE_LO_MASK = 0xfffffc00U,
  333. /* ditto, for response queue */
  334. EDMA_RSP_Q_BASE_LO_MASK = 0xffffff00U,
  335. };
  336. enum chip_type {
  337. chip_504x,
  338. chip_508x,
  339. chip_5080,
  340. chip_604x,
  341. chip_608x,
  342. chip_6042,
  343. chip_7042,
  344. chip_soc,
  345. };
  346. /* Command ReQuest Block: 32B */
  347. struct mv_crqb {
  348. __le32 sg_addr;
  349. __le32 sg_addr_hi;
  350. __le16 ctrl_flags;
  351. __le16 ata_cmd[11];
  352. };
  353. struct mv_crqb_iie {
  354. __le32 addr;
  355. __le32 addr_hi;
  356. __le32 flags;
  357. __le32 len;
  358. __le32 ata_cmd[4];
  359. };
  360. /* Command ResPonse Block: 8B */
  361. struct mv_crpb {
  362. __le16 id;
  363. __le16 flags;
  364. __le32 tmstmp;
  365. };
  366. /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
  367. struct mv_sg {
  368. __le32 addr;
  369. __le32 flags_size;
  370. __le32 addr_hi;
  371. __le32 reserved;
  372. };
  373. /*
  374. * We keep a local cache of a few frequently accessed port
  375. * registers here, to avoid having to read them (very slow)
  376. * when switching between EDMA and non-EDMA modes.
  377. */
  378. struct mv_cached_regs {
  379. u32 fiscfg;
  380. u32 ltmode;
  381. u32 haltcond;
  382. u32 unknown_rsvd;
  383. };
  384. struct mv_port_priv {
  385. struct mv_crqb *crqb;
  386. dma_addr_t crqb_dma;
  387. struct mv_crpb *crpb;
  388. dma_addr_t crpb_dma;
  389. struct mv_sg *sg_tbl[MV_MAX_Q_DEPTH];
  390. dma_addr_t sg_tbl_dma[MV_MAX_Q_DEPTH];
  391. unsigned int req_idx;
  392. unsigned int resp_idx;
  393. u32 pp_flags;
  394. struct mv_cached_regs cached;
  395. unsigned int delayed_eh_pmp_map;
  396. };
  397. struct mv_port_signal {
  398. u32 amps;
  399. u32 pre;
  400. };
  401. struct mv_host_priv {
  402. u32 hp_flags;
  403. u32 main_irq_mask;
  404. struct mv_port_signal signal[8];
  405. const struct mv_hw_ops *ops;
  406. int n_ports;
  407. void __iomem *base;
  408. void __iomem *main_irq_cause_addr;
  409. void __iomem *main_irq_mask_addr;
  410. u32 irq_cause_ofs;
  411. u32 irq_mask_ofs;
  412. u32 unmask_all_irqs;
  413. /*
  414. * These consistent DMA memory pools give us guaranteed
  415. * alignment for hardware-accessed data structures,
  416. * and less memory waste in accomplishing the alignment.
  417. */
  418. struct dma_pool *crqb_pool;
  419. struct dma_pool *crpb_pool;
  420. struct dma_pool *sg_tbl_pool;
  421. };
  422. struct mv_hw_ops {
  423. void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio,
  424. unsigned int port);
  425. void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio);
  426. void (*read_preamp)(struct mv_host_priv *hpriv, int idx,
  427. void __iomem *mmio);
  428. int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio,
  429. unsigned int n_hc);
  430. void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio);
  431. void (*reset_bus)(struct ata_host *host, void __iomem *mmio);
  432. };
  433. static int mv_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val);
  434. static int mv_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val);
  435. static int mv5_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val);
  436. static int mv5_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val);
  437. static int mv_port_start(struct ata_port *ap);
  438. static void mv_port_stop(struct ata_port *ap);
  439. static int mv_qc_defer(struct ata_queued_cmd *qc);
  440. static void mv_qc_prep(struct ata_queued_cmd *qc);
  441. static void mv_qc_prep_iie(struct ata_queued_cmd *qc);
  442. static unsigned int mv_qc_issue(struct ata_queued_cmd *qc);
  443. static int mv_hardreset(struct ata_link *link, unsigned int *class,
  444. unsigned long deadline);
  445. static void mv_eh_freeze(struct ata_port *ap);
  446. static void mv_eh_thaw(struct ata_port *ap);
  447. static void mv6_dev_config(struct ata_device *dev);
  448. static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  449. unsigned int port);
  450. static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
  451. static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
  452. void __iomem *mmio);
  453. static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  454. unsigned int n_hc);
  455. static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
  456. static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio);
  457. static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  458. unsigned int port);
  459. static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
  460. static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
  461. void __iomem *mmio);
  462. static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  463. unsigned int n_hc);
  464. static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
  465. static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
  466. void __iomem *mmio);
  467. static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
  468. void __iomem *mmio);
  469. static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
  470. void __iomem *mmio, unsigned int n_hc);
  471. static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
  472. void __iomem *mmio);
  473. static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio);
  474. static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio);
  475. static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
  476. unsigned int port_no);
  477. static int mv_stop_edma(struct ata_port *ap);
  478. static int mv_stop_edma_engine(void __iomem *port_mmio);
  479. static void mv_edma_cfg(struct ata_port *ap, int want_ncq, int want_edma);
  480. static void mv_pmp_select(struct ata_port *ap, int pmp);
  481. static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
  482. unsigned long deadline);
  483. static int mv_softreset(struct ata_link *link, unsigned int *class,
  484. unsigned long deadline);
  485. static void mv_pmp_error_handler(struct ata_port *ap);
  486. static void mv_process_crpb_entries(struct ata_port *ap,
  487. struct mv_port_priv *pp);
  488. static void mv_sff_irq_clear(struct ata_port *ap);
  489. static int mv_check_atapi_dma(struct ata_queued_cmd *qc);
  490. static void mv_bmdma_setup(struct ata_queued_cmd *qc);
  491. static void mv_bmdma_start(struct ata_queued_cmd *qc);
  492. static void mv_bmdma_stop(struct ata_queued_cmd *qc);
  493. static u8 mv_bmdma_status(struct ata_port *ap);
  494. /* .sg_tablesize is (MV_MAX_SG_CT / 2) in the structures below
  495. * because we have to allow room for worst case splitting of
  496. * PRDs for 64K boundaries in mv_fill_sg().
  497. */
  498. static struct scsi_host_template mv5_sht = {
  499. ATA_BASE_SHT(DRV_NAME),
  500. .sg_tablesize = MV_MAX_SG_CT / 2,
  501. .dma_boundary = MV_DMA_BOUNDARY,
  502. };
  503. static struct scsi_host_template mv6_sht = {
  504. ATA_NCQ_SHT(DRV_NAME),
  505. .can_queue = MV_MAX_Q_DEPTH - 1,
  506. .sg_tablesize = MV_MAX_SG_CT / 2,
  507. .dma_boundary = MV_DMA_BOUNDARY,
  508. };
  509. static struct ata_port_operations mv5_ops = {
  510. .inherits = &ata_sff_port_ops,
  511. .qc_defer = mv_qc_defer,
  512. .qc_prep = mv_qc_prep,
  513. .qc_issue = mv_qc_issue,
  514. .freeze = mv_eh_freeze,
  515. .thaw = mv_eh_thaw,
  516. .hardreset = mv_hardreset,
  517. .error_handler = ata_std_error_handler, /* avoid SFF EH */
  518. .post_internal_cmd = ATA_OP_NULL,
  519. .scr_read = mv5_scr_read,
  520. .scr_write = mv5_scr_write,
  521. .port_start = mv_port_start,
  522. .port_stop = mv_port_stop,
  523. };
  524. static struct ata_port_operations mv6_ops = {
  525. .inherits = &mv5_ops,
  526. .dev_config = mv6_dev_config,
  527. .scr_read = mv_scr_read,
  528. .scr_write = mv_scr_write,
  529. .pmp_hardreset = mv_pmp_hardreset,
  530. .pmp_softreset = mv_softreset,
  531. .softreset = mv_softreset,
  532. .error_handler = mv_pmp_error_handler,
  533. .sff_irq_clear = mv_sff_irq_clear,
  534. .check_atapi_dma = mv_check_atapi_dma,
  535. .bmdma_setup = mv_bmdma_setup,
  536. .bmdma_start = mv_bmdma_start,
  537. .bmdma_stop = mv_bmdma_stop,
  538. .bmdma_status = mv_bmdma_status,
  539. };
  540. static struct ata_port_operations mv_iie_ops = {
  541. .inherits = &mv6_ops,
  542. .dev_config = ATA_OP_NULL,
  543. .qc_prep = mv_qc_prep_iie,
  544. };
  545. static const struct ata_port_info mv_port_info[] = {
  546. { /* chip_504x */
  547. .flags = MV_GEN_I_FLAGS,
  548. .pio_mask = 0x1f, /* pio0-4 */
  549. .udma_mask = ATA_UDMA6,
  550. .port_ops = &mv5_ops,
  551. },
  552. { /* chip_508x */
  553. .flags = MV_GEN_I_FLAGS | MV_FLAG_DUAL_HC,
  554. .pio_mask = 0x1f, /* pio0-4 */
  555. .udma_mask = ATA_UDMA6,
  556. .port_ops = &mv5_ops,
  557. },
  558. { /* chip_5080 */
  559. .flags = MV_GEN_I_FLAGS | MV_FLAG_DUAL_HC,
  560. .pio_mask = 0x1f, /* pio0-4 */
  561. .udma_mask = ATA_UDMA6,
  562. .port_ops = &mv5_ops,
  563. },
  564. { /* chip_604x */
  565. .flags = MV_GEN_II_FLAGS,
  566. .pio_mask = 0x1f, /* pio0-4 */
  567. .udma_mask = ATA_UDMA6,
  568. .port_ops = &mv6_ops,
  569. },
  570. { /* chip_608x */
  571. .flags = MV_GEN_II_FLAGS | MV_FLAG_DUAL_HC,
  572. .pio_mask = 0x1f, /* pio0-4 */
  573. .udma_mask = ATA_UDMA6,
  574. .port_ops = &mv6_ops,
  575. },
  576. { /* chip_6042 */
  577. .flags = MV_GEN_IIE_FLAGS,
  578. .pio_mask = 0x1f, /* pio0-4 */
  579. .udma_mask = ATA_UDMA6,
  580. .port_ops = &mv_iie_ops,
  581. },
  582. { /* chip_7042 */
  583. .flags = MV_GEN_IIE_FLAGS,
  584. .pio_mask = 0x1f, /* pio0-4 */
  585. .udma_mask = ATA_UDMA6,
  586. .port_ops = &mv_iie_ops,
  587. },
  588. { /* chip_soc */
  589. .flags = MV_GEN_IIE_FLAGS,
  590. .pio_mask = 0x1f, /* pio0-4 */
  591. .udma_mask = ATA_UDMA6,
  592. .port_ops = &mv_iie_ops,
  593. },
  594. };
  595. static const struct pci_device_id mv_pci_tbl[] = {
  596. { PCI_VDEVICE(MARVELL, 0x5040), chip_504x },
  597. { PCI_VDEVICE(MARVELL, 0x5041), chip_504x },
  598. { PCI_VDEVICE(MARVELL, 0x5080), chip_5080 },
  599. { PCI_VDEVICE(MARVELL, 0x5081), chip_508x },
  600. /* RocketRAID 1720/174x have different identifiers */
  601. { PCI_VDEVICE(TTI, 0x1720), chip_6042 },
  602. { PCI_VDEVICE(TTI, 0x1740), chip_6042 },
  603. { PCI_VDEVICE(TTI, 0x1742), chip_6042 },
  604. { PCI_VDEVICE(MARVELL, 0x6040), chip_604x },
  605. { PCI_VDEVICE(MARVELL, 0x6041), chip_604x },
  606. { PCI_VDEVICE(MARVELL, 0x6042), chip_6042 },
  607. { PCI_VDEVICE(MARVELL, 0x6080), chip_608x },
  608. { PCI_VDEVICE(MARVELL, 0x6081), chip_608x },
  609. { PCI_VDEVICE(ADAPTEC2, 0x0241), chip_604x },
  610. /* Adaptec 1430SA */
  611. { PCI_VDEVICE(ADAPTEC2, 0x0243), chip_7042 },
  612. /* Marvell 7042 support */
  613. { PCI_VDEVICE(MARVELL, 0x7042), chip_7042 },
  614. /* Highpoint RocketRAID PCIe series */
  615. { PCI_VDEVICE(TTI, 0x2300), chip_7042 },
  616. { PCI_VDEVICE(TTI, 0x2310), chip_7042 },
  617. { } /* terminate list */
  618. };
  619. static const struct mv_hw_ops mv5xxx_ops = {
  620. .phy_errata = mv5_phy_errata,
  621. .enable_leds = mv5_enable_leds,
  622. .read_preamp = mv5_read_preamp,
  623. .reset_hc = mv5_reset_hc,
  624. .reset_flash = mv5_reset_flash,
  625. .reset_bus = mv5_reset_bus,
  626. };
  627. static const struct mv_hw_ops mv6xxx_ops = {
  628. .phy_errata = mv6_phy_errata,
  629. .enable_leds = mv6_enable_leds,
  630. .read_preamp = mv6_read_preamp,
  631. .reset_hc = mv6_reset_hc,
  632. .reset_flash = mv6_reset_flash,
  633. .reset_bus = mv_reset_pci_bus,
  634. };
  635. static const struct mv_hw_ops mv_soc_ops = {
  636. .phy_errata = mv6_phy_errata,
  637. .enable_leds = mv_soc_enable_leds,
  638. .read_preamp = mv_soc_read_preamp,
  639. .reset_hc = mv_soc_reset_hc,
  640. .reset_flash = mv_soc_reset_flash,
  641. .reset_bus = mv_soc_reset_bus,
  642. };
  643. /*
  644. * Functions
  645. */
  646. static inline void writelfl(unsigned long data, void __iomem *addr)
  647. {
  648. writel(data, addr);
  649. (void) readl(addr); /* flush to avoid PCI posted write */
  650. }
  651. static inline unsigned int mv_hc_from_port(unsigned int port)
  652. {
  653. return port >> MV_PORT_HC_SHIFT;
  654. }
  655. static inline unsigned int mv_hardport_from_port(unsigned int port)
  656. {
  657. return port & MV_PORT_MASK;
  658. }
  659. /*
  660. * Consolidate some rather tricky bit shift calculations.
  661. * This is hot-path stuff, so not a function.
  662. * Simple code, with two return values, so macro rather than inline.
  663. *
  664. * port is the sole input, in range 0..7.
  665. * shift is one output, for use with main_irq_cause / main_irq_mask registers.
  666. * hardport is the other output, in range 0..3.
  667. *
  668. * Note that port and hardport may be the same variable in some cases.
  669. */
  670. #define MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport) \
  671. { \
  672. shift = mv_hc_from_port(port) * HC_SHIFT; \
  673. hardport = mv_hardport_from_port(port); \
  674. shift += hardport * 2; \
  675. }
  676. static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
  677. {
  678. return (base + MV_SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
  679. }
  680. static inline void __iomem *mv_hc_base_from_port(void __iomem *base,
  681. unsigned int port)
  682. {
  683. return mv_hc_base(base, mv_hc_from_port(port));
  684. }
  685. static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
  686. {
  687. return mv_hc_base_from_port(base, port) +
  688. MV_SATAHC_ARBTR_REG_SZ +
  689. (mv_hardport_from_port(port) * MV_PORT_REG_SZ);
  690. }
  691. static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
  692. {
  693. void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
  694. unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
  695. return hc_mmio + ofs;
  696. }
  697. static inline void __iomem *mv_host_base(struct ata_host *host)
  698. {
  699. struct mv_host_priv *hpriv = host->private_data;
  700. return hpriv->base;
  701. }
  702. static inline void __iomem *mv_ap_base(struct ata_port *ap)
  703. {
  704. return mv_port_base(mv_host_base(ap->host), ap->port_no);
  705. }
  706. static inline int mv_get_hc_count(unsigned long port_flags)
  707. {
  708. return ((port_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
  709. }
  710. /**
  711. * mv_save_cached_regs - (re-)initialize cached port registers
  712. * @ap: the port whose registers we are caching
  713. *
  714. * Initialize the local cache of port registers,
  715. * so that reading them over and over again can
  716. * be avoided on the hotter paths of this driver.
  717. * This saves a few microseconds each time we switch
  718. * to/from EDMA mode to perform (eg.) a drive cache flush.
  719. */
  720. static void mv_save_cached_regs(struct ata_port *ap)
  721. {
  722. void __iomem *port_mmio = mv_ap_base(ap);
  723. struct mv_port_priv *pp = ap->private_data;
  724. pp->cached.fiscfg = readl(port_mmio + FISCFG_OFS);
  725. pp->cached.ltmode = readl(port_mmio + LTMODE_OFS);
  726. pp->cached.haltcond = readl(port_mmio + EDMA_HALTCOND_OFS);
  727. pp->cached.unknown_rsvd = readl(port_mmio + EDMA_UNKNOWN_RSVD_OFS);
  728. }
  729. /**
  730. * mv_write_cached_reg - write to a cached port register
  731. * @addr: hardware address of the register
  732. * @old: pointer to cached value of the register
  733. * @new: new value for the register
  734. *
  735. * Write a new value to a cached register,
  736. * but only if the value is different from before.
  737. */
  738. static inline void mv_write_cached_reg(void __iomem *addr, u32 *old, u32 new)
  739. {
  740. if (new != *old) {
  741. *old = new;
  742. writel(new, addr);
  743. }
  744. }
  745. static void mv_set_edma_ptrs(void __iomem *port_mmio,
  746. struct mv_host_priv *hpriv,
  747. struct mv_port_priv *pp)
  748. {
  749. u32 index;
  750. /*
  751. * initialize request queue
  752. */
  753. pp->req_idx &= MV_MAX_Q_DEPTH_MASK; /* paranoia */
  754. index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
  755. WARN_ON(pp->crqb_dma & 0x3ff);
  756. writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI_OFS);
  757. writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | index,
  758. port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  759. writelfl(index, port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
  760. /*
  761. * initialize response queue
  762. */
  763. pp->resp_idx &= MV_MAX_Q_DEPTH_MASK; /* paranoia */
  764. index = pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT;
  765. WARN_ON(pp->crpb_dma & 0xff);
  766. writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI_OFS);
  767. writelfl(index, port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
  768. writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) | index,
  769. port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  770. }
  771. static void mv_set_main_irq_mask(struct ata_host *host,
  772. u32 disable_bits, u32 enable_bits)
  773. {
  774. struct mv_host_priv *hpriv = host->private_data;
  775. u32 old_mask, new_mask;
  776. old_mask = hpriv->main_irq_mask;
  777. new_mask = (old_mask & ~disable_bits) | enable_bits;
  778. if (new_mask != old_mask) {
  779. hpriv->main_irq_mask = new_mask;
  780. writelfl(new_mask, hpriv->main_irq_mask_addr);
  781. }
  782. }
  783. static void mv_enable_port_irqs(struct ata_port *ap,
  784. unsigned int port_bits)
  785. {
  786. unsigned int shift, hardport, port = ap->port_no;
  787. u32 disable_bits, enable_bits;
  788. MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
  789. disable_bits = (DONE_IRQ | ERR_IRQ) << shift;
  790. enable_bits = port_bits << shift;
  791. mv_set_main_irq_mask(ap->host, disable_bits, enable_bits);
  792. }
  793. static void mv_clear_and_enable_port_irqs(struct ata_port *ap,
  794. void __iomem *port_mmio,
  795. unsigned int port_irqs)
  796. {
  797. struct mv_host_priv *hpriv = ap->host->private_data;
  798. int hardport = mv_hardport_from_port(ap->port_no);
  799. void __iomem *hc_mmio = mv_hc_base_from_port(
  800. mv_host_base(ap->host), ap->port_no);
  801. u32 hc_irq_cause;
  802. /* clear EDMA event indicators, if any */
  803. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  804. /* clear pending irq events */
  805. hc_irq_cause = ~((DEV_IRQ | DMA_IRQ) << hardport);
  806. writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
  807. /* clear FIS IRQ Cause */
  808. if (IS_GEN_IIE(hpriv))
  809. writelfl(0, port_mmio + SATA_FIS_IRQ_CAUSE_OFS);
  810. mv_enable_port_irqs(ap, port_irqs);
  811. }
  812. /**
  813. * mv_start_edma - Enable eDMA engine
  814. * @base: port base address
  815. * @pp: port private data
  816. *
  817. * Verify the local cache of the eDMA state is accurate with a
  818. * WARN_ON.
  819. *
  820. * LOCKING:
  821. * Inherited from caller.
  822. */
  823. static void mv_start_edma(struct ata_port *ap, void __iomem *port_mmio,
  824. struct mv_port_priv *pp, u8 protocol)
  825. {
  826. int want_ncq = (protocol == ATA_PROT_NCQ);
  827. if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
  828. int using_ncq = ((pp->pp_flags & MV_PP_FLAG_NCQ_EN) != 0);
  829. if (want_ncq != using_ncq)
  830. mv_stop_edma(ap);
  831. }
  832. if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN)) {
  833. struct mv_host_priv *hpriv = ap->host->private_data;
  834. mv_edma_cfg(ap, want_ncq, 1);
  835. mv_set_edma_ptrs(port_mmio, hpriv, pp);
  836. mv_clear_and_enable_port_irqs(ap, port_mmio, DONE_IRQ|ERR_IRQ);
  837. writelfl(EDMA_EN, port_mmio + EDMA_CMD_OFS);
  838. pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
  839. }
  840. }
  841. static void mv_wait_for_edma_empty_idle(struct ata_port *ap)
  842. {
  843. void __iomem *port_mmio = mv_ap_base(ap);
  844. const u32 empty_idle = (EDMA_STATUS_CACHE_EMPTY | EDMA_STATUS_IDLE);
  845. const int per_loop = 5, timeout = (15 * 1000 / per_loop);
  846. int i;
  847. /*
  848. * Wait for the EDMA engine to finish transactions in progress.
  849. * No idea what a good "timeout" value might be, but measurements
  850. * indicate that it often requires hundreds of microseconds
  851. * with two drives in-use. So we use the 15msec value above
  852. * as a rough guess at what even more drives might require.
  853. */
  854. for (i = 0; i < timeout; ++i) {
  855. u32 edma_stat = readl(port_mmio + EDMA_STATUS_OFS);
  856. if ((edma_stat & empty_idle) == empty_idle)
  857. break;
  858. udelay(per_loop);
  859. }
  860. /* ata_port_printk(ap, KERN_INFO, "%s: %u+ usecs\n", __func__, i); */
  861. }
  862. /**
  863. * mv_stop_edma_engine - Disable eDMA engine
  864. * @port_mmio: io base address
  865. *
  866. * LOCKING:
  867. * Inherited from caller.
  868. */
  869. static int mv_stop_edma_engine(void __iomem *port_mmio)
  870. {
  871. int i;
  872. /* Disable eDMA. The disable bit auto clears. */
  873. writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
  874. /* Wait for the chip to confirm eDMA is off. */
  875. for (i = 10000; i > 0; i--) {
  876. u32 reg = readl(port_mmio + EDMA_CMD_OFS);
  877. if (!(reg & EDMA_EN))
  878. return 0;
  879. udelay(10);
  880. }
  881. return -EIO;
  882. }
  883. static int mv_stop_edma(struct ata_port *ap)
  884. {
  885. void __iomem *port_mmio = mv_ap_base(ap);
  886. struct mv_port_priv *pp = ap->private_data;
  887. int err = 0;
  888. if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
  889. return 0;
  890. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  891. mv_wait_for_edma_empty_idle(ap);
  892. if (mv_stop_edma_engine(port_mmio)) {
  893. ata_port_printk(ap, KERN_ERR, "Unable to stop eDMA\n");
  894. err = -EIO;
  895. }
  896. mv_edma_cfg(ap, 0, 0);
  897. return err;
  898. }
  899. #ifdef ATA_DEBUG
  900. static void mv_dump_mem(void __iomem *start, unsigned bytes)
  901. {
  902. int b, w;
  903. for (b = 0; b < bytes; ) {
  904. DPRINTK("%p: ", start + b);
  905. for (w = 0; b < bytes && w < 4; w++) {
  906. printk("%08x ", readl(start + b));
  907. b += sizeof(u32);
  908. }
  909. printk("\n");
  910. }
  911. }
  912. #endif
  913. static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
  914. {
  915. #ifdef ATA_DEBUG
  916. int b, w;
  917. u32 dw;
  918. for (b = 0; b < bytes; ) {
  919. DPRINTK("%02x: ", b);
  920. for (w = 0; b < bytes && w < 4; w++) {
  921. (void) pci_read_config_dword(pdev, b, &dw);
  922. printk("%08x ", dw);
  923. b += sizeof(u32);
  924. }
  925. printk("\n");
  926. }
  927. #endif
  928. }
  929. static void mv_dump_all_regs(void __iomem *mmio_base, int port,
  930. struct pci_dev *pdev)
  931. {
  932. #ifdef ATA_DEBUG
  933. void __iomem *hc_base = mv_hc_base(mmio_base,
  934. port >> MV_PORT_HC_SHIFT);
  935. void __iomem *port_base;
  936. int start_port, num_ports, p, start_hc, num_hcs, hc;
  937. if (0 > port) {
  938. start_hc = start_port = 0;
  939. num_ports = 8; /* shld be benign for 4 port devs */
  940. num_hcs = 2;
  941. } else {
  942. start_hc = port >> MV_PORT_HC_SHIFT;
  943. start_port = port;
  944. num_ports = num_hcs = 1;
  945. }
  946. DPRINTK("All registers for port(s) %u-%u:\n", start_port,
  947. num_ports > 1 ? num_ports - 1 : start_port);
  948. if (NULL != pdev) {
  949. DPRINTK("PCI config space regs:\n");
  950. mv_dump_pci_cfg(pdev, 0x68);
  951. }
  952. DPRINTK("PCI regs:\n");
  953. mv_dump_mem(mmio_base+0xc00, 0x3c);
  954. mv_dump_mem(mmio_base+0xd00, 0x34);
  955. mv_dump_mem(mmio_base+0xf00, 0x4);
  956. mv_dump_mem(mmio_base+0x1d00, 0x6c);
  957. for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
  958. hc_base = mv_hc_base(mmio_base, hc);
  959. DPRINTK("HC regs (HC %i):\n", hc);
  960. mv_dump_mem(hc_base, 0x1c);
  961. }
  962. for (p = start_port; p < start_port + num_ports; p++) {
  963. port_base = mv_port_base(mmio_base, p);
  964. DPRINTK("EDMA regs (port %i):\n", p);
  965. mv_dump_mem(port_base, 0x54);
  966. DPRINTK("SATA regs (port %i):\n", p);
  967. mv_dump_mem(port_base+0x300, 0x60);
  968. }
  969. #endif
  970. }
  971. static unsigned int mv_scr_offset(unsigned int sc_reg_in)
  972. {
  973. unsigned int ofs;
  974. switch (sc_reg_in) {
  975. case SCR_STATUS:
  976. case SCR_CONTROL:
  977. case SCR_ERROR:
  978. ofs = SATA_STATUS_OFS + (sc_reg_in * sizeof(u32));
  979. break;
  980. case SCR_ACTIVE:
  981. ofs = SATA_ACTIVE_OFS; /* active is not with the others */
  982. break;
  983. default:
  984. ofs = 0xffffffffU;
  985. break;
  986. }
  987. return ofs;
  988. }
  989. static int mv_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val)
  990. {
  991. unsigned int ofs = mv_scr_offset(sc_reg_in);
  992. if (ofs != 0xffffffffU) {
  993. *val = readl(mv_ap_base(link->ap) + ofs);
  994. return 0;
  995. } else
  996. return -EINVAL;
  997. }
  998. static int mv_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val)
  999. {
  1000. unsigned int ofs = mv_scr_offset(sc_reg_in);
  1001. if (ofs != 0xffffffffU) {
  1002. writelfl(val, mv_ap_base(link->ap) + ofs);
  1003. return 0;
  1004. } else
  1005. return -EINVAL;
  1006. }
  1007. static void mv6_dev_config(struct ata_device *adev)
  1008. {
  1009. /*
  1010. * Deal with Gen-II ("mv6") hardware quirks/restrictions:
  1011. *
  1012. * Gen-II does not support NCQ over a port multiplier
  1013. * (no FIS-based switching).
  1014. */
  1015. if (adev->flags & ATA_DFLAG_NCQ) {
  1016. if (sata_pmp_attached(adev->link->ap)) {
  1017. adev->flags &= ~ATA_DFLAG_NCQ;
  1018. ata_dev_printk(adev, KERN_INFO,
  1019. "NCQ disabled for command-based switching\n");
  1020. }
  1021. }
  1022. }
  1023. static int mv_qc_defer(struct ata_queued_cmd *qc)
  1024. {
  1025. struct ata_link *link = qc->dev->link;
  1026. struct ata_port *ap = link->ap;
  1027. struct mv_port_priv *pp = ap->private_data;
  1028. /*
  1029. * Don't allow new commands if we're in a delayed EH state
  1030. * for NCQ and/or FIS-based switching.
  1031. */
  1032. if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH)
  1033. return ATA_DEFER_PORT;
  1034. /*
  1035. * If the port is completely idle, then allow the new qc.
  1036. */
  1037. if (ap->nr_active_links == 0)
  1038. return 0;
  1039. /*
  1040. * The port is operating in host queuing mode (EDMA) with NCQ
  1041. * enabled, allow multiple NCQ commands. EDMA also allows
  1042. * queueing multiple DMA commands but libata core currently
  1043. * doesn't allow it.
  1044. */
  1045. if ((pp->pp_flags & MV_PP_FLAG_EDMA_EN) &&
  1046. (pp->pp_flags & MV_PP_FLAG_NCQ_EN) && ata_is_ncq(qc->tf.protocol))
  1047. return 0;
  1048. return ATA_DEFER_PORT;
  1049. }
  1050. static void mv_config_fbs(struct ata_port *ap, int want_ncq, int want_fbs)
  1051. {
  1052. struct mv_port_priv *pp = ap->private_data;
  1053. void __iomem *port_mmio;
  1054. u32 fiscfg, *old_fiscfg = &pp->cached.fiscfg;
  1055. u32 ltmode, *old_ltmode = &pp->cached.ltmode;
  1056. u32 haltcond, *old_haltcond = &pp->cached.haltcond;
  1057. ltmode = *old_ltmode & ~LTMODE_BIT8;
  1058. haltcond = *old_haltcond | EDMA_ERR_DEV;
  1059. if (want_fbs) {
  1060. fiscfg = *old_fiscfg | FISCFG_SINGLE_SYNC;
  1061. ltmode = *old_ltmode | LTMODE_BIT8;
  1062. if (want_ncq)
  1063. haltcond &= ~EDMA_ERR_DEV;
  1064. else
  1065. fiscfg |= FISCFG_WAIT_DEV_ERR;
  1066. } else {
  1067. fiscfg = *old_fiscfg & ~(FISCFG_SINGLE_SYNC | FISCFG_WAIT_DEV_ERR);
  1068. }
  1069. port_mmio = mv_ap_base(ap);
  1070. mv_write_cached_reg(port_mmio + FISCFG_OFS, old_fiscfg, fiscfg);
  1071. mv_write_cached_reg(port_mmio + LTMODE_OFS, old_ltmode, ltmode);
  1072. mv_write_cached_reg(port_mmio + EDMA_HALTCOND_OFS, old_haltcond, haltcond);
  1073. }
  1074. static void mv_60x1_errata_sata25(struct ata_port *ap, int want_ncq)
  1075. {
  1076. struct mv_host_priv *hpriv = ap->host->private_data;
  1077. u32 old, new;
  1078. /* workaround for 88SX60x1 FEr SATA#25 (part 1) */
  1079. old = readl(hpriv->base + MV_GPIO_PORT_CTL_OFS);
  1080. if (want_ncq)
  1081. new = old | (1 << 22);
  1082. else
  1083. new = old & ~(1 << 22);
  1084. if (new != old)
  1085. writel(new, hpriv->base + MV_GPIO_PORT_CTL_OFS);
  1086. }
  1087. /**
  1088. * mv_bmdma_enable - set a magic bit on GEN_IIE to allow bmdma
  1089. * @ap: Port being initialized
  1090. *
  1091. * There are two DMA modes on these chips: basic DMA, and EDMA.
  1092. *
  1093. * Bit-0 of the "EDMA RESERVED" register enables/disables use
  1094. * of basic DMA on the GEN_IIE versions of the chips.
  1095. *
  1096. * This bit survives EDMA resets, and must be set for basic DMA
  1097. * to function, and should be cleared when EDMA is active.
  1098. */
  1099. static void mv_bmdma_enable_iie(struct ata_port *ap, int enable_bmdma)
  1100. {
  1101. struct mv_port_priv *pp = ap->private_data;
  1102. u32 new, *old = &pp->cached.unknown_rsvd;
  1103. if (enable_bmdma)
  1104. new = *old | 1;
  1105. else
  1106. new = *old & ~1;
  1107. mv_write_cached_reg(mv_ap_base(ap) + EDMA_UNKNOWN_RSVD_OFS, old, new);
  1108. }
  1109. static void mv_edma_cfg(struct ata_port *ap, int want_ncq, int want_edma)
  1110. {
  1111. u32 cfg;
  1112. struct mv_port_priv *pp = ap->private_data;
  1113. struct mv_host_priv *hpriv = ap->host->private_data;
  1114. void __iomem *port_mmio = mv_ap_base(ap);
  1115. /* set up non-NCQ EDMA configuration */
  1116. cfg = EDMA_CFG_Q_DEPTH; /* always 0x1f for *all* chips */
  1117. pp->pp_flags &= ~(MV_PP_FLAG_FBS_EN | MV_PP_FLAG_NCQ_EN);
  1118. if (IS_GEN_I(hpriv))
  1119. cfg |= (1 << 8); /* enab config burst size mask */
  1120. else if (IS_GEN_II(hpriv)) {
  1121. cfg |= EDMA_CFG_RD_BRST_EXT | EDMA_CFG_WR_BUFF_LEN;
  1122. mv_60x1_errata_sata25(ap, want_ncq);
  1123. } else if (IS_GEN_IIE(hpriv)) {
  1124. int want_fbs = sata_pmp_attached(ap);
  1125. /*
  1126. * Possible future enhancement:
  1127. *
  1128. * The chip can use FBS with non-NCQ, if we allow it,
  1129. * But first we need to have the error handling in place
  1130. * for this mode (datasheet section 7.3.15.4.2.3).
  1131. * So disallow non-NCQ FBS for now.
  1132. */
  1133. want_fbs &= want_ncq;
  1134. mv_config_fbs(ap, want_ncq, want_fbs);
  1135. if (want_fbs) {
  1136. pp->pp_flags |= MV_PP_FLAG_FBS_EN;
  1137. cfg |= EDMA_CFG_EDMA_FBS; /* FIS-based switching */
  1138. }
  1139. cfg |= (1 << 23); /* do not mask PM field in rx'd FIS */
  1140. if (want_edma) {
  1141. cfg |= (1 << 22); /* enab 4-entry host queue cache */
  1142. if (!IS_SOC(hpriv))
  1143. cfg |= (1 << 18); /* enab early completion */
  1144. }
  1145. if (hpriv->hp_flags & MV_HP_CUT_THROUGH)
  1146. cfg |= (1 << 17); /* enab cut-thru (dis stor&forwrd) */
  1147. mv_bmdma_enable_iie(ap, !want_edma);
  1148. }
  1149. if (want_ncq) {
  1150. cfg |= EDMA_CFG_NCQ;
  1151. pp->pp_flags |= MV_PP_FLAG_NCQ_EN;
  1152. }
  1153. writelfl(cfg, port_mmio + EDMA_CFG_OFS);
  1154. }
  1155. static void mv_port_free_dma_mem(struct ata_port *ap)
  1156. {
  1157. struct mv_host_priv *hpriv = ap->host->private_data;
  1158. struct mv_port_priv *pp = ap->private_data;
  1159. int tag;
  1160. if (pp->crqb) {
  1161. dma_pool_free(hpriv->crqb_pool, pp->crqb, pp->crqb_dma);
  1162. pp->crqb = NULL;
  1163. }
  1164. if (pp->crpb) {
  1165. dma_pool_free(hpriv->crpb_pool, pp->crpb, pp->crpb_dma);
  1166. pp->crpb = NULL;
  1167. }
  1168. /*
  1169. * For GEN_I, there's no NCQ, so we have only a single sg_tbl.
  1170. * For later hardware, we have one unique sg_tbl per NCQ tag.
  1171. */
  1172. for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
  1173. if (pp->sg_tbl[tag]) {
  1174. if (tag == 0 || !IS_GEN_I(hpriv))
  1175. dma_pool_free(hpriv->sg_tbl_pool,
  1176. pp->sg_tbl[tag],
  1177. pp->sg_tbl_dma[tag]);
  1178. pp->sg_tbl[tag] = NULL;
  1179. }
  1180. }
  1181. }
  1182. /**
  1183. * mv_port_start - Port specific init/start routine.
  1184. * @ap: ATA channel to manipulate
  1185. *
  1186. * Allocate and point to DMA memory, init port private memory,
  1187. * zero indices.
  1188. *
  1189. * LOCKING:
  1190. * Inherited from caller.
  1191. */
  1192. static int mv_port_start(struct ata_port *ap)
  1193. {
  1194. struct device *dev = ap->host->dev;
  1195. struct mv_host_priv *hpriv = ap->host->private_data;
  1196. struct mv_port_priv *pp;
  1197. int tag;
  1198. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  1199. if (!pp)
  1200. return -ENOMEM;
  1201. ap->private_data = pp;
  1202. pp->crqb = dma_pool_alloc(hpriv->crqb_pool, GFP_KERNEL, &pp->crqb_dma);
  1203. if (!pp->crqb)
  1204. return -ENOMEM;
  1205. memset(pp->crqb, 0, MV_CRQB_Q_SZ);
  1206. pp->crpb = dma_pool_alloc(hpriv->crpb_pool, GFP_KERNEL, &pp->crpb_dma);
  1207. if (!pp->crpb)
  1208. goto out_port_free_dma_mem;
  1209. memset(pp->crpb, 0, MV_CRPB_Q_SZ);
  1210. /* 6041/6081 Rev. "C0" (and newer) are okay with async notify */
  1211. if (hpriv->hp_flags & MV_HP_ERRATA_60X1C0)
  1212. ap->flags |= ATA_FLAG_AN;
  1213. /*
  1214. * For GEN_I, there's no NCQ, so we only allocate a single sg_tbl.
  1215. * For later hardware, we need one unique sg_tbl per NCQ tag.
  1216. */
  1217. for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
  1218. if (tag == 0 || !IS_GEN_I(hpriv)) {
  1219. pp->sg_tbl[tag] = dma_pool_alloc(hpriv->sg_tbl_pool,
  1220. GFP_KERNEL, &pp->sg_tbl_dma[tag]);
  1221. if (!pp->sg_tbl[tag])
  1222. goto out_port_free_dma_mem;
  1223. } else {
  1224. pp->sg_tbl[tag] = pp->sg_tbl[0];
  1225. pp->sg_tbl_dma[tag] = pp->sg_tbl_dma[0];
  1226. }
  1227. }
  1228. mv_save_cached_regs(ap);
  1229. mv_edma_cfg(ap, 0, 0);
  1230. return 0;
  1231. out_port_free_dma_mem:
  1232. mv_port_free_dma_mem(ap);
  1233. return -ENOMEM;
  1234. }
  1235. /**
  1236. * mv_port_stop - Port specific cleanup/stop routine.
  1237. * @ap: ATA channel to manipulate
  1238. *
  1239. * Stop DMA, cleanup port memory.
  1240. *
  1241. * LOCKING:
  1242. * This routine uses the host lock to protect the DMA stop.
  1243. */
  1244. static void mv_port_stop(struct ata_port *ap)
  1245. {
  1246. mv_stop_edma(ap);
  1247. mv_enable_port_irqs(ap, 0);
  1248. mv_port_free_dma_mem(ap);
  1249. }
  1250. /**
  1251. * mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
  1252. * @qc: queued command whose SG list to source from
  1253. *
  1254. * Populate the SG list and mark the last entry.
  1255. *
  1256. * LOCKING:
  1257. * Inherited from caller.
  1258. */
  1259. static void mv_fill_sg(struct ata_queued_cmd *qc)
  1260. {
  1261. struct mv_port_priv *pp = qc->ap->private_data;
  1262. struct scatterlist *sg;
  1263. struct mv_sg *mv_sg, *last_sg = NULL;
  1264. unsigned int si;
  1265. mv_sg = pp->sg_tbl[qc->tag];
  1266. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  1267. dma_addr_t addr = sg_dma_address(sg);
  1268. u32 sg_len = sg_dma_len(sg);
  1269. while (sg_len) {
  1270. u32 offset = addr & 0xffff;
  1271. u32 len = sg_len;
  1272. if (offset + len > 0x10000)
  1273. len = 0x10000 - offset;
  1274. mv_sg->addr = cpu_to_le32(addr & 0xffffffff);
  1275. mv_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
  1276. mv_sg->flags_size = cpu_to_le32(len & 0xffff);
  1277. mv_sg->reserved = 0;
  1278. sg_len -= len;
  1279. addr += len;
  1280. last_sg = mv_sg;
  1281. mv_sg++;
  1282. }
  1283. }
  1284. if (likely(last_sg))
  1285. last_sg->flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
  1286. mb(); /* ensure data structure is visible to the chipset */
  1287. }
  1288. static void mv_crqb_pack_cmd(__le16 *cmdw, u8 data, u8 addr, unsigned last)
  1289. {
  1290. u16 tmp = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
  1291. (last ? CRQB_CMD_LAST : 0);
  1292. *cmdw = cpu_to_le16(tmp);
  1293. }
  1294. /**
  1295. * mv_sff_irq_clear - Clear hardware interrupt after DMA.
  1296. * @ap: Port associated with this ATA transaction.
  1297. *
  1298. * We need this only for ATAPI bmdma transactions,
  1299. * as otherwise we experience spurious interrupts
  1300. * after libata-sff handles the bmdma interrupts.
  1301. */
  1302. static void mv_sff_irq_clear(struct ata_port *ap)
  1303. {
  1304. mv_clear_and_enable_port_irqs(ap, mv_ap_base(ap), ERR_IRQ);
  1305. }
  1306. /**
  1307. * mv_check_atapi_dma - Filter ATAPI cmds which are unsuitable for DMA.
  1308. * @qc: queued command to check for chipset/DMA compatibility.
  1309. *
  1310. * The bmdma engines cannot handle speculative data sizes
  1311. * (bytecount under/over flow). So only allow DMA for
  1312. * data transfer commands with known data sizes.
  1313. *
  1314. * LOCKING:
  1315. * Inherited from caller.
  1316. */
  1317. static int mv_check_atapi_dma(struct ata_queued_cmd *qc)
  1318. {
  1319. struct scsi_cmnd *scmd = qc->scsicmd;
  1320. if (scmd) {
  1321. switch (scmd->cmnd[0]) {
  1322. case READ_6:
  1323. case READ_10:
  1324. case READ_12:
  1325. case WRITE_6:
  1326. case WRITE_10:
  1327. case WRITE_12:
  1328. case GPCMD_READ_CD:
  1329. case GPCMD_SEND_DVD_STRUCTURE:
  1330. case GPCMD_SEND_CUE_SHEET:
  1331. return 0; /* DMA is safe */
  1332. }
  1333. }
  1334. return -EOPNOTSUPP; /* use PIO instead */
  1335. }
  1336. /**
  1337. * mv_bmdma_setup - Set up BMDMA transaction
  1338. * @qc: queued command to prepare DMA for.
  1339. *
  1340. * LOCKING:
  1341. * Inherited from caller.
  1342. */
  1343. static void mv_bmdma_setup(struct ata_queued_cmd *qc)
  1344. {
  1345. struct ata_port *ap = qc->ap;
  1346. void __iomem *port_mmio = mv_ap_base(ap);
  1347. struct mv_port_priv *pp = ap->private_data;
  1348. mv_fill_sg(qc);
  1349. /* clear all DMA cmd bits */
  1350. writel(0, port_mmio + BMDMA_CMD_OFS);
  1351. /* load PRD table addr. */
  1352. writel((pp->sg_tbl_dma[qc->tag] >> 16) >> 16,
  1353. port_mmio + BMDMA_PRD_HIGH_OFS);
  1354. writelfl(pp->sg_tbl_dma[qc->tag],
  1355. port_mmio + BMDMA_PRD_LOW_OFS);
  1356. /* issue r/w command */
  1357. ap->ops->sff_exec_command(ap, &qc->tf);
  1358. }
  1359. /**
  1360. * mv_bmdma_start - Start a BMDMA transaction
  1361. * @qc: queued command to start DMA on.
  1362. *
  1363. * LOCKING:
  1364. * Inherited from caller.
  1365. */
  1366. static void mv_bmdma_start(struct ata_queued_cmd *qc)
  1367. {
  1368. struct ata_port *ap = qc->ap;
  1369. void __iomem *port_mmio = mv_ap_base(ap);
  1370. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  1371. u32 cmd = (rw ? 0 : ATA_DMA_WR) | ATA_DMA_START;
  1372. /* start host DMA transaction */
  1373. writelfl(cmd, port_mmio + BMDMA_CMD_OFS);
  1374. }
  1375. /**
  1376. * mv_bmdma_stop - Stop BMDMA transfer
  1377. * @qc: queued command to stop DMA on.
  1378. *
  1379. * Clears the ATA_DMA_START flag in the bmdma control register
  1380. *
  1381. * LOCKING:
  1382. * Inherited from caller.
  1383. */
  1384. static void mv_bmdma_stop(struct ata_queued_cmd *qc)
  1385. {
  1386. struct ata_port *ap = qc->ap;
  1387. void __iomem *port_mmio = mv_ap_base(ap);
  1388. u32 cmd;
  1389. /* clear start/stop bit */
  1390. cmd = readl(port_mmio + BMDMA_CMD_OFS);
  1391. cmd &= ~ATA_DMA_START;
  1392. writelfl(cmd, port_mmio + BMDMA_CMD_OFS);
  1393. /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
  1394. ata_sff_dma_pause(ap);
  1395. }
  1396. /**
  1397. * mv_bmdma_status - Read BMDMA status
  1398. * @ap: port for which to retrieve DMA status.
  1399. *
  1400. * Read and return equivalent of the sff BMDMA status register.
  1401. *
  1402. * LOCKING:
  1403. * Inherited from caller.
  1404. */
  1405. static u8 mv_bmdma_status(struct ata_port *ap)
  1406. {
  1407. void __iomem *port_mmio = mv_ap_base(ap);
  1408. u32 reg, status;
  1409. /*
  1410. * Other bits are valid only if ATA_DMA_ACTIVE==0,
  1411. * and the ATA_DMA_INTR bit doesn't exist.
  1412. */
  1413. reg = readl(port_mmio + BMDMA_STATUS_OFS);
  1414. if (reg & ATA_DMA_ACTIVE)
  1415. status = ATA_DMA_ACTIVE;
  1416. else
  1417. status = (reg & ATA_DMA_ERR) | ATA_DMA_INTR;
  1418. return status;
  1419. }
  1420. /**
  1421. * mv_qc_prep - Host specific command preparation.
  1422. * @qc: queued command to prepare
  1423. *
  1424. * This routine simply redirects to the general purpose routine
  1425. * if command is not DMA. Else, it handles prep of the CRQB
  1426. * (command request block), does some sanity checking, and calls
  1427. * the SG load routine.
  1428. *
  1429. * LOCKING:
  1430. * Inherited from caller.
  1431. */
  1432. static void mv_qc_prep(struct ata_queued_cmd *qc)
  1433. {
  1434. struct ata_port *ap = qc->ap;
  1435. struct mv_port_priv *pp = ap->private_data;
  1436. __le16 *cw;
  1437. struct ata_taskfile *tf;
  1438. u16 flags = 0;
  1439. unsigned in_index;
  1440. if ((qc->tf.protocol != ATA_PROT_DMA) &&
  1441. (qc->tf.protocol != ATA_PROT_NCQ))
  1442. return;
  1443. /* Fill in command request block
  1444. */
  1445. if (!(qc->tf.flags & ATA_TFLAG_WRITE))
  1446. flags |= CRQB_FLAG_READ;
  1447. WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
  1448. flags |= qc->tag << CRQB_TAG_SHIFT;
  1449. flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
  1450. /* get current queue index from software */
  1451. in_index = pp->req_idx;
  1452. pp->crqb[in_index].sg_addr =
  1453. cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
  1454. pp->crqb[in_index].sg_addr_hi =
  1455. cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
  1456. pp->crqb[in_index].ctrl_flags = cpu_to_le16(flags);
  1457. cw = &pp->crqb[in_index].ata_cmd[0];
  1458. tf = &qc->tf;
  1459. /* Sadly, the CRQB cannot accomodate all registers--there are
  1460. * only 11 bytes...so we must pick and choose required
  1461. * registers based on the command. So, we drop feature and
  1462. * hob_feature for [RW] DMA commands, but they are needed for
  1463. * NCQ. NCQ will drop hob_nsect, which is not needed there
  1464. * (nsect is used only for the tag; feat/hob_feat hold true nsect).
  1465. */
  1466. switch (tf->command) {
  1467. case ATA_CMD_READ:
  1468. case ATA_CMD_READ_EXT:
  1469. case ATA_CMD_WRITE:
  1470. case ATA_CMD_WRITE_EXT:
  1471. case ATA_CMD_WRITE_FUA_EXT:
  1472. mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
  1473. break;
  1474. case ATA_CMD_FPDMA_READ:
  1475. case ATA_CMD_FPDMA_WRITE:
  1476. mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
  1477. mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
  1478. break;
  1479. default:
  1480. /* The only other commands EDMA supports in non-queued and
  1481. * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
  1482. * of which are defined/used by Linux. If we get here, this
  1483. * driver needs work.
  1484. *
  1485. * FIXME: modify libata to give qc_prep a return value and
  1486. * return error here.
  1487. */
  1488. BUG_ON(tf->command);
  1489. break;
  1490. }
  1491. mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
  1492. mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
  1493. mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
  1494. mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
  1495. mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
  1496. mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
  1497. mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
  1498. mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
  1499. mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1); /* last */
  1500. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  1501. return;
  1502. mv_fill_sg(qc);
  1503. }
  1504. /**
  1505. * mv_qc_prep_iie - Host specific command preparation.
  1506. * @qc: queued command to prepare
  1507. *
  1508. * This routine simply redirects to the general purpose routine
  1509. * if command is not DMA. Else, it handles prep of the CRQB
  1510. * (command request block), does some sanity checking, and calls
  1511. * the SG load routine.
  1512. *
  1513. * LOCKING:
  1514. * Inherited from caller.
  1515. */
  1516. static void mv_qc_prep_iie(struct ata_queued_cmd *qc)
  1517. {
  1518. struct ata_port *ap = qc->ap;
  1519. struct mv_port_priv *pp = ap->private_data;
  1520. struct mv_crqb_iie *crqb;
  1521. struct ata_taskfile *tf;
  1522. unsigned in_index;
  1523. u32 flags = 0;
  1524. if ((qc->tf.protocol != ATA_PROT_DMA) &&
  1525. (qc->tf.protocol != ATA_PROT_NCQ))
  1526. return;
  1527. /* Fill in Gen IIE command request block */
  1528. if (!(qc->tf.flags & ATA_TFLAG_WRITE))
  1529. flags |= CRQB_FLAG_READ;
  1530. WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
  1531. flags |= qc->tag << CRQB_TAG_SHIFT;
  1532. flags |= qc->tag << CRQB_HOSTQ_SHIFT;
  1533. flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
  1534. /* get current queue index from software */
  1535. in_index = pp->req_idx;
  1536. crqb = (struct mv_crqb_iie *) &pp->crqb[in_index];
  1537. crqb->addr = cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
  1538. crqb->addr_hi = cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
  1539. crqb->flags = cpu_to_le32(flags);
  1540. tf = &qc->tf;
  1541. crqb->ata_cmd[0] = cpu_to_le32(
  1542. (tf->command << 16) |
  1543. (tf->feature << 24)
  1544. );
  1545. crqb->ata_cmd[1] = cpu_to_le32(
  1546. (tf->lbal << 0) |
  1547. (tf->lbam << 8) |
  1548. (tf->lbah << 16) |
  1549. (tf->device << 24)
  1550. );
  1551. crqb->ata_cmd[2] = cpu_to_le32(
  1552. (tf->hob_lbal << 0) |
  1553. (tf->hob_lbam << 8) |
  1554. (tf->hob_lbah << 16) |
  1555. (tf->hob_feature << 24)
  1556. );
  1557. crqb->ata_cmd[3] = cpu_to_le32(
  1558. (tf->nsect << 0) |
  1559. (tf->hob_nsect << 8)
  1560. );
  1561. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  1562. return;
  1563. mv_fill_sg(qc);
  1564. }
  1565. /**
  1566. * mv_qc_issue - Initiate a command to the host
  1567. * @qc: queued command to start
  1568. *
  1569. * This routine simply redirects to the general purpose routine
  1570. * if command is not DMA. Else, it sanity checks our local
  1571. * caches of the request producer/consumer indices then enables
  1572. * DMA and bumps the request producer index.
  1573. *
  1574. * LOCKING:
  1575. * Inherited from caller.
  1576. */
  1577. static unsigned int mv_qc_issue(struct ata_queued_cmd *qc)
  1578. {
  1579. static int limit_warnings = 10;
  1580. struct ata_port *ap = qc->ap;
  1581. void __iomem *port_mmio = mv_ap_base(ap);
  1582. struct mv_port_priv *pp = ap->private_data;
  1583. u32 in_index;
  1584. unsigned int port_irqs;
  1585. switch (qc->tf.protocol) {
  1586. case ATA_PROT_DMA:
  1587. case ATA_PROT_NCQ:
  1588. mv_start_edma(ap, port_mmio, pp, qc->tf.protocol);
  1589. pp->req_idx = (pp->req_idx + 1) & MV_MAX_Q_DEPTH_MASK;
  1590. in_index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
  1591. /* Write the request in pointer to kick the EDMA to life */
  1592. writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | in_index,
  1593. port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  1594. return 0;
  1595. case ATA_PROT_PIO:
  1596. /*
  1597. * Errata SATA#16, SATA#24: warn if multiple DRQs expected.
  1598. *
  1599. * Someday, we might implement special polling workarounds
  1600. * for these, but it all seems rather unnecessary since we
  1601. * normally use only DMA for commands which transfer more
  1602. * than a single block of data.
  1603. *
  1604. * Much of the time, this could just work regardless.
  1605. * So for now, just log the incident, and allow the attempt.
  1606. */
  1607. if (limit_warnings > 0 && (qc->nbytes / qc->sect_size) > 1) {
  1608. --limit_warnings;
  1609. ata_link_printk(qc->dev->link, KERN_WARNING, DRV_NAME
  1610. ": attempting PIO w/multiple DRQ: "
  1611. "this may fail due to h/w errata\n");
  1612. }
  1613. /* drop through */
  1614. case ATA_PROT_NODATA:
  1615. case ATAPI_PROT_PIO:
  1616. case ATAPI_PROT_NODATA:
  1617. if (ap->flags & ATA_FLAG_PIO_POLLING)
  1618. qc->tf.flags |= ATA_TFLAG_POLLING;
  1619. break;
  1620. }
  1621. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1622. port_irqs = ERR_IRQ; /* mask device interrupt when polling */
  1623. else
  1624. port_irqs = ERR_IRQ | DONE_IRQ; /* unmask all interrupts */
  1625. /*
  1626. * We're about to send a non-EDMA capable command to the
  1627. * port. Turn off EDMA so there won't be problems accessing
  1628. * shadow block, etc registers.
  1629. */
  1630. mv_stop_edma(ap);
  1631. mv_clear_and_enable_port_irqs(ap, mv_ap_base(ap), port_irqs);
  1632. mv_pmp_select(ap, qc->dev->link->pmp);
  1633. return ata_sff_qc_issue(qc);
  1634. }
  1635. static struct ata_queued_cmd *mv_get_active_qc(struct ata_port *ap)
  1636. {
  1637. struct mv_port_priv *pp = ap->private_data;
  1638. struct ata_queued_cmd *qc;
  1639. if (pp->pp_flags & MV_PP_FLAG_NCQ_EN)
  1640. return NULL;
  1641. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  1642. if (qc) {
  1643. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1644. qc = NULL;
  1645. else if (!(qc->flags & ATA_QCFLAG_ACTIVE))
  1646. qc = NULL;
  1647. }
  1648. return qc;
  1649. }
  1650. static void mv_pmp_error_handler(struct ata_port *ap)
  1651. {
  1652. unsigned int pmp, pmp_map;
  1653. struct mv_port_priv *pp = ap->private_data;
  1654. if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH) {
  1655. /*
  1656. * Perform NCQ error analysis on failed PMPs
  1657. * before we freeze the port entirely.
  1658. *
  1659. * The failed PMPs are marked earlier by mv_pmp_eh_prep().
  1660. */
  1661. pmp_map = pp->delayed_eh_pmp_map;
  1662. pp->pp_flags &= ~MV_PP_FLAG_DELAYED_EH;
  1663. for (pmp = 0; pmp_map != 0; pmp++) {
  1664. unsigned int this_pmp = (1 << pmp);
  1665. if (pmp_map & this_pmp) {
  1666. struct ata_link *link = &ap->pmp_link[pmp];
  1667. pmp_map &= ~this_pmp;
  1668. ata_eh_analyze_ncq_error(link);
  1669. }
  1670. }
  1671. ata_port_freeze(ap);
  1672. }
  1673. sata_pmp_error_handler(ap);
  1674. }
  1675. static unsigned int mv_get_err_pmp_map(struct ata_port *ap)
  1676. {
  1677. void __iomem *port_mmio = mv_ap_base(ap);
  1678. return readl(port_mmio + SATA_TESTCTL_OFS) >> 16;
  1679. }
  1680. static void mv_pmp_eh_prep(struct ata_port *ap, unsigned int pmp_map)
  1681. {
  1682. struct ata_eh_info *ehi;
  1683. unsigned int pmp;
  1684. /*
  1685. * Initialize EH info for PMPs which saw device errors
  1686. */
  1687. ehi = &ap->link.eh_info;
  1688. for (pmp = 0; pmp_map != 0; pmp++) {
  1689. unsigned int this_pmp = (1 << pmp);
  1690. if (pmp_map & this_pmp) {
  1691. struct ata_link *link = &ap->pmp_link[pmp];
  1692. pmp_map &= ~this_pmp;
  1693. ehi = &link->eh_info;
  1694. ata_ehi_clear_desc(ehi);
  1695. ata_ehi_push_desc(ehi, "dev err");
  1696. ehi->err_mask |= AC_ERR_DEV;
  1697. ehi->action |= ATA_EH_RESET;
  1698. ata_link_abort(link);
  1699. }
  1700. }
  1701. }
  1702. static int mv_req_q_empty(struct ata_port *ap)
  1703. {
  1704. void __iomem *port_mmio = mv_ap_base(ap);
  1705. u32 in_ptr, out_ptr;
  1706. in_ptr = (readl(port_mmio + EDMA_REQ_Q_IN_PTR_OFS)
  1707. >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
  1708. out_ptr = (readl(port_mmio + EDMA_REQ_Q_OUT_PTR_OFS)
  1709. >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
  1710. return (in_ptr == out_ptr); /* 1 == queue_is_empty */
  1711. }
  1712. static int mv_handle_fbs_ncq_dev_err(struct ata_port *ap)
  1713. {
  1714. struct mv_port_priv *pp = ap->private_data;
  1715. int failed_links;
  1716. unsigned int old_map, new_map;
  1717. /*
  1718. * Device error during FBS+NCQ operation:
  1719. *
  1720. * Set a port flag to prevent further I/O being enqueued.
  1721. * Leave the EDMA running to drain outstanding commands from this port.
  1722. * Perform the post-mortem/EH only when all responses are complete.
  1723. * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.2).
  1724. */
  1725. if (!(pp->pp_flags & MV_PP_FLAG_DELAYED_EH)) {
  1726. pp->pp_flags |= MV_PP_FLAG_DELAYED_EH;
  1727. pp->delayed_eh_pmp_map = 0;
  1728. }
  1729. old_map = pp->delayed_eh_pmp_map;
  1730. new_map = old_map | mv_get_err_pmp_map(ap);
  1731. if (old_map != new_map) {
  1732. pp->delayed_eh_pmp_map = new_map;
  1733. mv_pmp_eh_prep(ap, new_map & ~old_map);
  1734. }
  1735. failed_links = hweight16(new_map);
  1736. ata_port_printk(ap, KERN_INFO, "%s: pmp_map=%04x qc_map=%04x "
  1737. "failed_links=%d nr_active_links=%d\n",
  1738. __func__, pp->delayed_eh_pmp_map,
  1739. ap->qc_active, failed_links,
  1740. ap->nr_active_links);
  1741. if (ap->nr_active_links <= failed_links && mv_req_q_empty(ap)) {
  1742. mv_process_crpb_entries(ap, pp);
  1743. mv_stop_edma(ap);
  1744. mv_eh_freeze(ap);
  1745. ata_port_printk(ap, KERN_INFO, "%s: done\n", __func__);
  1746. return 1; /* handled */
  1747. }
  1748. ata_port_printk(ap, KERN_INFO, "%s: waiting\n", __func__);
  1749. return 1; /* handled */
  1750. }
  1751. static int mv_handle_fbs_non_ncq_dev_err(struct ata_port *ap)
  1752. {
  1753. /*
  1754. * Possible future enhancement:
  1755. *
  1756. * FBS+non-NCQ operation is not yet implemented.
  1757. * See related notes in mv_edma_cfg().
  1758. *
  1759. * Device error during FBS+non-NCQ operation:
  1760. *
  1761. * We need to snapshot the shadow registers for each failed command.
  1762. * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.3).
  1763. */
  1764. return 0; /* not handled */
  1765. }
  1766. static int mv_handle_dev_err(struct ata_port *ap, u32 edma_err_cause)
  1767. {
  1768. struct mv_port_priv *pp = ap->private_data;
  1769. if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
  1770. return 0; /* EDMA was not active: not handled */
  1771. if (!(pp->pp_flags & MV_PP_FLAG_FBS_EN))
  1772. return 0; /* FBS was not active: not handled */
  1773. if (!(edma_err_cause & EDMA_ERR_DEV))
  1774. return 0; /* non DEV error: not handled */
  1775. edma_err_cause &= ~EDMA_ERR_IRQ_TRANSIENT;
  1776. if (edma_err_cause & ~(EDMA_ERR_DEV | EDMA_ERR_SELF_DIS))
  1777. return 0; /* other problems: not handled */
  1778. if (pp->pp_flags & MV_PP_FLAG_NCQ_EN) {
  1779. /*
  1780. * EDMA should NOT have self-disabled for this case.
  1781. * If it did, then something is wrong elsewhere,
  1782. * and we cannot handle it here.
  1783. */
  1784. if (edma_err_cause & EDMA_ERR_SELF_DIS) {
  1785. ata_port_printk(ap, KERN_WARNING,
  1786. "%s: err_cause=0x%x pp_flags=0x%x\n",
  1787. __func__, edma_err_cause, pp->pp_flags);
  1788. return 0; /* not handled */
  1789. }
  1790. return mv_handle_fbs_ncq_dev_err(ap);
  1791. } else {
  1792. /*
  1793. * EDMA should have self-disabled for this case.
  1794. * If it did not, then something is wrong elsewhere,
  1795. * and we cannot handle it here.
  1796. */
  1797. if (!(edma_err_cause & EDMA_ERR_SELF_DIS)) {
  1798. ata_port_printk(ap, KERN_WARNING,
  1799. "%s: err_cause=0x%x pp_flags=0x%x\n",
  1800. __func__, edma_err_cause, pp->pp_flags);
  1801. return 0; /* not handled */
  1802. }
  1803. return mv_handle_fbs_non_ncq_dev_err(ap);
  1804. }
  1805. return 0; /* not handled */
  1806. }
  1807. static void mv_unexpected_intr(struct ata_port *ap, int edma_was_enabled)
  1808. {
  1809. struct ata_eh_info *ehi = &ap->link.eh_info;
  1810. char *when = "idle";
  1811. ata_ehi_clear_desc(ehi);
  1812. if (!ap || (ap->flags & ATA_FLAG_DISABLED)) {
  1813. when = "disabled";
  1814. } else if (edma_was_enabled) {
  1815. when = "EDMA enabled";
  1816. } else {
  1817. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
  1818. if (qc && (qc->tf.flags & ATA_TFLAG_POLLING))
  1819. when = "polling";
  1820. }
  1821. ata_ehi_push_desc(ehi, "unexpected device interrupt while %s", when);
  1822. ehi->err_mask |= AC_ERR_OTHER;
  1823. ehi->action |= ATA_EH_RESET;
  1824. ata_port_freeze(ap);
  1825. }
  1826. /**
  1827. * mv_err_intr - Handle error interrupts on the port
  1828. * @ap: ATA channel to manipulate
  1829. *
  1830. * Most cases require a full reset of the chip's state machine,
  1831. * which also performs a COMRESET.
  1832. * Also, if the port disabled DMA, update our cached copy to match.
  1833. *
  1834. * LOCKING:
  1835. * Inherited from caller.
  1836. */
  1837. static void mv_err_intr(struct ata_port *ap)
  1838. {
  1839. void __iomem *port_mmio = mv_ap_base(ap);
  1840. u32 edma_err_cause, eh_freeze_mask, serr = 0;
  1841. u32 fis_cause = 0;
  1842. struct mv_port_priv *pp = ap->private_data;
  1843. struct mv_host_priv *hpriv = ap->host->private_data;
  1844. unsigned int action = 0, err_mask = 0;
  1845. struct ata_eh_info *ehi = &ap->link.eh_info;
  1846. struct ata_queued_cmd *qc;
  1847. int abort = 0;
  1848. /*
  1849. * Read and clear the SError and err_cause bits.
  1850. * For GenIIe, if EDMA_ERR_TRANS_IRQ_7 is set, we also must read/clear
  1851. * the FIS_IRQ_CAUSE register before clearing edma_err_cause.
  1852. */
  1853. sata_scr_read(&ap->link, SCR_ERROR, &serr);
  1854. sata_scr_write_flush(&ap->link, SCR_ERROR, serr);
  1855. edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1856. if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) {
  1857. fis_cause = readl(port_mmio + SATA_FIS_IRQ_CAUSE_OFS);
  1858. writelfl(~fis_cause, port_mmio + SATA_FIS_IRQ_CAUSE_OFS);
  1859. }
  1860. writelfl(~edma_err_cause, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1861. if (edma_err_cause & EDMA_ERR_DEV) {
  1862. /*
  1863. * Device errors during FIS-based switching operation
  1864. * require special handling.
  1865. */
  1866. if (mv_handle_dev_err(ap, edma_err_cause))
  1867. return;
  1868. }
  1869. qc = mv_get_active_qc(ap);
  1870. ata_ehi_clear_desc(ehi);
  1871. ata_ehi_push_desc(ehi, "edma_err_cause=%08x pp_flags=%08x",
  1872. edma_err_cause, pp->pp_flags);
  1873. if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) {
  1874. ata_ehi_push_desc(ehi, "fis_cause=%08x", fis_cause);
  1875. if (fis_cause & SATA_FIS_IRQ_AN) {
  1876. u32 ec = edma_err_cause &
  1877. ~(EDMA_ERR_TRANS_IRQ_7 | EDMA_ERR_IRQ_TRANSIENT);
  1878. sata_async_notification(ap);
  1879. if (!ec)
  1880. return; /* Just an AN; no need for the nukes */
  1881. ata_ehi_push_desc(ehi, "SDB notify");
  1882. }
  1883. }
  1884. /*
  1885. * All generations share these EDMA error cause bits:
  1886. */
  1887. if (edma_err_cause & EDMA_ERR_DEV) {
  1888. err_mask |= AC_ERR_DEV;
  1889. action |= ATA_EH_RESET;
  1890. ata_ehi_push_desc(ehi, "dev error");
  1891. }
  1892. if (edma_err_cause & (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
  1893. EDMA_ERR_CRQB_PAR | EDMA_ERR_CRPB_PAR |
  1894. EDMA_ERR_INTRL_PAR)) {
  1895. err_mask |= AC_ERR_ATA_BUS;
  1896. action |= ATA_EH_RESET;
  1897. ata_ehi_push_desc(ehi, "parity error");
  1898. }
  1899. if (edma_err_cause & (EDMA_ERR_DEV_DCON | EDMA_ERR_DEV_CON)) {
  1900. ata_ehi_hotplugged(ehi);
  1901. ata_ehi_push_desc(ehi, edma_err_cause & EDMA_ERR_DEV_DCON ?
  1902. "dev disconnect" : "dev connect");
  1903. action |= ATA_EH_RESET;
  1904. }
  1905. /*
  1906. * Gen-I has a different SELF_DIS bit,
  1907. * different FREEZE bits, and no SERR bit:
  1908. */
  1909. if (IS_GEN_I(hpriv)) {
  1910. eh_freeze_mask = EDMA_EH_FREEZE_5;
  1911. if (edma_err_cause & EDMA_ERR_SELF_DIS_5) {
  1912. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  1913. ata_ehi_push_desc(ehi, "EDMA self-disable");
  1914. }
  1915. } else {
  1916. eh_freeze_mask = EDMA_EH_FREEZE;
  1917. if (edma_err_cause & EDMA_ERR_SELF_DIS) {
  1918. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  1919. ata_ehi_push_desc(ehi, "EDMA self-disable");
  1920. }
  1921. if (edma_err_cause & EDMA_ERR_SERR) {
  1922. ata_ehi_push_desc(ehi, "SError=%08x", serr);
  1923. err_mask |= AC_ERR_ATA_BUS;
  1924. action |= ATA_EH_RESET;
  1925. }
  1926. }
  1927. if (!err_mask) {
  1928. err_mask = AC_ERR_OTHER;
  1929. action |= ATA_EH_RESET;
  1930. }
  1931. ehi->serror |= serr;
  1932. ehi->action |= action;
  1933. if (qc)
  1934. qc->err_mask |= err_mask;
  1935. else
  1936. ehi->err_mask |= err_mask;
  1937. if (err_mask == AC_ERR_DEV) {
  1938. /*
  1939. * Cannot do ata_port_freeze() here,
  1940. * because it would kill PIO access,
  1941. * which is needed for further diagnosis.
  1942. */
  1943. mv_eh_freeze(ap);
  1944. abort = 1;
  1945. } else if (edma_err_cause & eh_freeze_mask) {
  1946. /*
  1947. * Note to self: ata_port_freeze() calls ata_port_abort()
  1948. */
  1949. ata_port_freeze(ap);
  1950. } else {
  1951. abort = 1;
  1952. }
  1953. if (abort) {
  1954. if (qc)
  1955. ata_link_abort(qc->dev->link);
  1956. else
  1957. ata_port_abort(ap);
  1958. }
  1959. }
  1960. static void mv_process_crpb_response(struct ata_port *ap,
  1961. struct mv_crpb *response, unsigned int tag, int ncq_enabled)
  1962. {
  1963. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
  1964. if (qc) {
  1965. u8 ata_status;
  1966. u16 edma_status = le16_to_cpu(response->flags);
  1967. /*
  1968. * edma_status from a response queue entry:
  1969. * LSB is from EDMA_ERR_IRQ_CAUSE_OFS (non-NCQ only).
  1970. * MSB is saved ATA status from command completion.
  1971. */
  1972. if (!ncq_enabled) {
  1973. u8 err_cause = edma_status & 0xff & ~EDMA_ERR_DEV;
  1974. if (err_cause) {
  1975. /*
  1976. * Error will be seen/handled by mv_err_intr().
  1977. * So do nothing at all here.
  1978. */
  1979. return;
  1980. }
  1981. }
  1982. ata_status = edma_status >> CRPB_FLAG_STATUS_SHIFT;
  1983. if (!ac_err_mask(ata_status))
  1984. ata_qc_complete(qc);
  1985. /* else: leave it for mv_err_intr() */
  1986. } else {
  1987. ata_port_printk(ap, KERN_ERR, "%s: no qc for tag=%d\n",
  1988. __func__, tag);
  1989. }
  1990. }
  1991. static void mv_process_crpb_entries(struct ata_port *ap, struct mv_port_priv *pp)
  1992. {
  1993. void __iomem *port_mmio = mv_ap_base(ap);
  1994. struct mv_host_priv *hpriv = ap->host->private_data;
  1995. u32 in_index;
  1996. bool work_done = false;
  1997. int ncq_enabled = (pp->pp_flags & MV_PP_FLAG_NCQ_EN);
  1998. /* Get the hardware queue position index */
  1999. in_index = (readl(port_mmio + EDMA_RSP_Q_IN_PTR_OFS)
  2000. >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
  2001. /* Process new responses from since the last time we looked */
  2002. while (in_index != pp->resp_idx) {
  2003. unsigned int tag;
  2004. struct mv_crpb *response = &pp->crpb[pp->resp_idx];
  2005. pp->resp_idx = (pp->resp_idx + 1) & MV_MAX_Q_DEPTH_MASK;
  2006. if (IS_GEN_I(hpriv)) {
  2007. /* 50xx: no NCQ, only one command active at a time */
  2008. tag = ap->link.active_tag;
  2009. } else {
  2010. /* Gen II/IIE: get command tag from CRPB entry */
  2011. tag = le16_to_cpu(response->id) & 0x1f;
  2012. }
  2013. mv_process_crpb_response(ap, response, tag, ncq_enabled);
  2014. work_done = true;
  2015. }
  2016. /* Update the software queue position index in hardware */
  2017. if (work_done)
  2018. writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) |
  2019. (pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT),
  2020. port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  2021. }
  2022. static void mv_port_intr(struct ata_port *ap, u32 port_cause)
  2023. {
  2024. struct mv_port_priv *pp;
  2025. int edma_was_enabled;
  2026. if (!ap || (ap->flags & ATA_FLAG_DISABLED)) {
  2027. mv_unexpected_intr(ap, 0);
  2028. return;
  2029. }
  2030. /*
  2031. * Grab a snapshot of the EDMA_EN flag setting,
  2032. * so that we have a consistent view for this port,
  2033. * even if something we call of our routines changes it.
  2034. */
  2035. pp = ap->private_data;
  2036. edma_was_enabled = (pp->pp_flags & MV_PP_FLAG_EDMA_EN);
  2037. /*
  2038. * Process completed CRPB response(s) before other events.
  2039. */
  2040. if (edma_was_enabled && (port_cause & DONE_IRQ)) {
  2041. mv_process_crpb_entries(ap, pp);
  2042. if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH)
  2043. mv_handle_fbs_ncq_dev_err(ap);
  2044. }
  2045. /*
  2046. * Handle chip-reported errors, or continue on to handle PIO.
  2047. */
  2048. if (unlikely(port_cause & ERR_IRQ)) {
  2049. mv_err_intr(ap);
  2050. } else if (!edma_was_enabled) {
  2051. struct ata_queued_cmd *qc = mv_get_active_qc(ap);
  2052. if (qc)
  2053. ata_sff_host_intr(ap, qc);
  2054. else
  2055. mv_unexpected_intr(ap, edma_was_enabled);
  2056. }
  2057. }
  2058. /**
  2059. * mv_host_intr - Handle all interrupts on the given host controller
  2060. * @host: host specific structure
  2061. * @main_irq_cause: Main interrupt cause register for the chip.
  2062. *
  2063. * LOCKING:
  2064. * Inherited from caller.
  2065. */
  2066. static int mv_host_intr(struct ata_host *host, u32 main_irq_cause)
  2067. {
  2068. struct mv_host_priv *hpriv = host->private_data;
  2069. void __iomem *mmio = hpriv->base, *hc_mmio;
  2070. unsigned int handled = 0, port;
  2071. for (port = 0; port < hpriv->n_ports; port++) {
  2072. struct ata_port *ap = host->ports[port];
  2073. unsigned int p, shift, hardport, port_cause;
  2074. MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
  2075. /*
  2076. * Each hc within the host has its own hc_irq_cause register,
  2077. * where the interrupting ports bits get ack'd.
  2078. */
  2079. if (hardport == 0) { /* first port on this hc ? */
  2080. u32 hc_cause = (main_irq_cause >> shift) & HC0_IRQ_PEND;
  2081. u32 port_mask, ack_irqs;
  2082. /*
  2083. * Skip this entire hc if nothing pending for any ports
  2084. */
  2085. if (!hc_cause) {
  2086. port += MV_PORTS_PER_HC - 1;
  2087. continue;
  2088. }
  2089. /*
  2090. * We don't need/want to read the hc_irq_cause register,
  2091. * because doing so hurts performance, and
  2092. * main_irq_cause already gives us everything we need.
  2093. *
  2094. * But we do have to *write* to the hc_irq_cause to ack
  2095. * the ports that we are handling this time through.
  2096. *
  2097. * This requires that we create a bitmap for those
  2098. * ports which interrupted us, and use that bitmap
  2099. * to ack (only) those ports via hc_irq_cause.
  2100. */
  2101. ack_irqs = 0;
  2102. for (p = 0; p < MV_PORTS_PER_HC; ++p) {
  2103. if ((port + p) >= hpriv->n_ports)
  2104. break;
  2105. port_mask = (DONE_IRQ | ERR_IRQ) << (p * 2);
  2106. if (hc_cause & port_mask)
  2107. ack_irqs |= (DMA_IRQ | DEV_IRQ) << p;
  2108. }
  2109. hc_mmio = mv_hc_base_from_port(mmio, port);
  2110. writelfl(~ack_irqs, hc_mmio + HC_IRQ_CAUSE_OFS);
  2111. handled = 1;
  2112. }
  2113. /*
  2114. * Handle interrupts signalled for this port:
  2115. */
  2116. port_cause = (main_irq_cause >> shift) & (DONE_IRQ | ERR_IRQ);
  2117. if (port_cause)
  2118. mv_port_intr(ap, port_cause);
  2119. }
  2120. return handled;
  2121. }
  2122. static int mv_pci_error(struct ata_host *host, void __iomem *mmio)
  2123. {
  2124. struct mv_host_priv *hpriv = host->private_data;
  2125. struct ata_port *ap;
  2126. struct ata_queued_cmd *qc;
  2127. struct ata_eh_info *ehi;
  2128. unsigned int i, err_mask, printed = 0;
  2129. u32 err_cause;
  2130. err_cause = readl(mmio + hpriv->irq_cause_ofs);
  2131. dev_printk(KERN_ERR, host->dev, "PCI ERROR; PCI IRQ cause=0x%08x\n",
  2132. err_cause);
  2133. DPRINTK("All regs @ PCI error\n");
  2134. mv_dump_all_regs(mmio, -1, to_pci_dev(host->dev));
  2135. writelfl(0, mmio + hpriv->irq_cause_ofs);
  2136. for (i = 0; i < host->n_ports; i++) {
  2137. ap = host->ports[i];
  2138. if (!ata_link_offline(&ap->link)) {
  2139. ehi = &ap->link.eh_info;
  2140. ata_ehi_clear_desc(ehi);
  2141. if (!printed++)
  2142. ata_ehi_push_desc(ehi,
  2143. "PCI err cause 0x%08x", err_cause);
  2144. err_mask = AC_ERR_HOST_BUS;
  2145. ehi->action = ATA_EH_RESET;
  2146. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  2147. if (qc)
  2148. qc->err_mask |= err_mask;
  2149. else
  2150. ehi->err_mask |= err_mask;
  2151. ata_port_freeze(ap);
  2152. }
  2153. }
  2154. return 1; /* handled */
  2155. }
  2156. /**
  2157. * mv_interrupt - Main interrupt event handler
  2158. * @irq: unused
  2159. * @dev_instance: private data; in this case the host structure
  2160. *
  2161. * Read the read only register to determine if any host
  2162. * controllers have pending interrupts. If so, call lower level
  2163. * routine to handle. Also check for PCI errors which are only
  2164. * reported here.
  2165. *
  2166. * LOCKING:
  2167. * This routine holds the host lock while processing pending
  2168. * interrupts.
  2169. */
  2170. static irqreturn_t mv_interrupt(int irq, void *dev_instance)
  2171. {
  2172. struct ata_host *host = dev_instance;
  2173. struct mv_host_priv *hpriv = host->private_data;
  2174. unsigned int handled = 0;
  2175. int using_msi = hpriv->hp_flags & MV_HP_FLAG_MSI;
  2176. u32 main_irq_cause, pending_irqs;
  2177. spin_lock(&host->lock);
  2178. /* for MSI: block new interrupts while in here */
  2179. if (using_msi)
  2180. writel(0, hpriv->main_irq_mask_addr);
  2181. main_irq_cause = readl(hpriv->main_irq_cause_addr);
  2182. pending_irqs = main_irq_cause & hpriv->main_irq_mask;
  2183. /*
  2184. * Deal with cases where we either have nothing pending, or have read
  2185. * a bogus register value which can indicate HW removal or PCI fault.
  2186. */
  2187. if (pending_irqs && main_irq_cause != 0xffffffffU) {
  2188. if (unlikely((pending_irqs & PCI_ERR) && !IS_SOC(hpriv)))
  2189. handled = mv_pci_error(host, hpriv->base);
  2190. else
  2191. handled = mv_host_intr(host, pending_irqs);
  2192. }
  2193. /* for MSI: unmask; interrupt cause bits will retrigger now */
  2194. if (using_msi)
  2195. writel(hpriv->main_irq_mask, hpriv->main_irq_mask_addr);
  2196. spin_unlock(&host->lock);
  2197. return IRQ_RETVAL(handled);
  2198. }
  2199. static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
  2200. {
  2201. unsigned int ofs;
  2202. switch (sc_reg_in) {
  2203. case SCR_STATUS:
  2204. case SCR_ERROR:
  2205. case SCR_CONTROL:
  2206. ofs = sc_reg_in * sizeof(u32);
  2207. break;
  2208. default:
  2209. ofs = 0xffffffffU;
  2210. break;
  2211. }
  2212. return ofs;
  2213. }
  2214. static int mv5_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val)
  2215. {
  2216. struct mv_host_priv *hpriv = link->ap->host->private_data;
  2217. void __iomem *mmio = hpriv->base;
  2218. void __iomem *addr = mv5_phy_base(mmio, link->ap->port_no);
  2219. unsigned int ofs = mv5_scr_offset(sc_reg_in);
  2220. if (ofs != 0xffffffffU) {
  2221. *val = readl(addr + ofs);
  2222. return 0;
  2223. } else
  2224. return -EINVAL;
  2225. }
  2226. static int mv5_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val)
  2227. {
  2228. struct mv_host_priv *hpriv = link->ap->host->private_data;
  2229. void __iomem *mmio = hpriv->base;
  2230. void __iomem *addr = mv5_phy_base(mmio, link->ap->port_no);
  2231. unsigned int ofs = mv5_scr_offset(sc_reg_in);
  2232. if (ofs != 0xffffffffU) {
  2233. writelfl(val, addr + ofs);
  2234. return 0;
  2235. } else
  2236. return -EINVAL;
  2237. }
  2238. static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio)
  2239. {
  2240. struct pci_dev *pdev = to_pci_dev(host->dev);
  2241. int early_5080;
  2242. early_5080 = (pdev->device == 0x5080) && (pdev->revision == 0);
  2243. if (!early_5080) {
  2244. u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
  2245. tmp |= (1 << 0);
  2246. writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
  2247. }
  2248. mv_reset_pci_bus(host, mmio);
  2249. }
  2250. static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
  2251. {
  2252. writel(0x0fcfffff, mmio + MV_FLASH_CTL_OFS);
  2253. }
  2254. static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
  2255. void __iomem *mmio)
  2256. {
  2257. void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
  2258. u32 tmp;
  2259. tmp = readl(phy_mmio + MV5_PHY_MODE);
  2260. hpriv->signal[idx].pre = tmp & 0x1800; /* bits 12:11 */
  2261. hpriv->signal[idx].amps = tmp & 0xe0; /* bits 7:5 */
  2262. }
  2263. static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
  2264. {
  2265. u32 tmp;
  2266. writel(0, mmio + MV_GPIO_PORT_CTL_OFS);
  2267. /* FIXME: handle MV_HP_ERRATA_50XXB2 errata */
  2268. tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
  2269. tmp |= ~(1 << 0);
  2270. writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
  2271. }
  2272. static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  2273. unsigned int port)
  2274. {
  2275. void __iomem *phy_mmio = mv5_phy_base(mmio, port);
  2276. const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
  2277. u32 tmp;
  2278. int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
  2279. if (fix_apm_sq) {
  2280. tmp = readl(phy_mmio + MV5_LTMODE_OFS);
  2281. tmp |= (1 << 19);
  2282. writel(tmp, phy_mmio + MV5_LTMODE_OFS);
  2283. tmp = readl(phy_mmio + MV5_PHY_CTL_OFS);
  2284. tmp &= ~0x3;
  2285. tmp |= 0x1;
  2286. writel(tmp, phy_mmio + MV5_PHY_CTL_OFS);
  2287. }
  2288. tmp = readl(phy_mmio + MV5_PHY_MODE);
  2289. tmp &= ~mask;
  2290. tmp |= hpriv->signal[port].pre;
  2291. tmp |= hpriv->signal[port].amps;
  2292. writel(tmp, phy_mmio + MV5_PHY_MODE);
  2293. }
  2294. #undef ZERO
  2295. #define ZERO(reg) writel(0, port_mmio + (reg))
  2296. static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
  2297. unsigned int port)
  2298. {
  2299. void __iomem *port_mmio = mv_port_base(mmio, port);
  2300. mv_reset_channel(hpriv, mmio, port);
  2301. ZERO(0x028); /* command */
  2302. writel(0x11f, port_mmio + EDMA_CFG_OFS);
  2303. ZERO(0x004); /* timer */
  2304. ZERO(0x008); /* irq err cause */
  2305. ZERO(0x00c); /* irq err mask */
  2306. ZERO(0x010); /* rq bah */
  2307. ZERO(0x014); /* rq inp */
  2308. ZERO(0x018); /* rq outp */
  2309. ZERO(0x01c); /* respq bah */
  2310. ZERO(0x024); /* respq outp */
  2311. ZERO(0x020); /* respq inp */
  2312. ZERO(0x02c); /* test control */
  2313. writel(0xbc, port_mmio + EDMA_IORDY_TMOUT_OFS);
  2314. }
  2315. #undef ZERO
  2316. #define ZERO(reg) writel(0, hc_mmio + (reg))
  2317. static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  2318. unsigned int hc)
  2319. {
  2320. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  2321. u32 tmp;
  2322. ZERO(0x00c);
  2323. ZERO(0x010);
  2324. ZERO(0x014);
  2325. ZERO(0x018);
  2326. tmp = readl(hc_mmio + 0x20);
  2327. tmp &= 0x1c1c1c1c;
  2328. tmp |= 0x03030303;
  2329. writel(tmp, hc_mmio + 0x20);
  2330. }
  2331. #undef ZERO
  2332. static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  2333. unsigned int n_hc)
  2334. {
  2335. unsigned int hc, port;
  2336. for (hc = 0; hc < n_hc; hc++) {
  2337. for (port = 0; port < MV_PORTS_PER_HC; port++)
  2338. mv5_reset_hc_port(hpriv, mmio,
  2339. (hc * MV_PORTS_PER_HC) + port);
  2340. mv5_reset_one_hc(hpriv, mmio, hc);
  2341. }
  2342. return 0;
  2343. }
  2344. #undef ZERO
  2345. #define ZERO(reg) writel(0, mmio + (reg))
  2346. static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio)
  2347. {
  2348. struct mv_host_priv *hpriv = host->private_data;
  2349. u32 tmp;
  2350. tmp = readl(mmio + MV_PCI_MODE_OFS);
  2351. tmp &= 0xff00ffff;
  2352. writel(tmp, mmio + MV_PCI_MODE_OFS);
  2353. ZERO(MV_PCI_DISC_TIMER);
  2354. ZERO(MV_PCI_MSI_TRIGGER);
  2355. writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT_OFS);
  2356. ZERO(MV_PCI_SERR_MASK);
  2357. ZERO(hpriv->irq_cause_ofs);
  2358. ZERO(hpriv->irq_mask_ofs);
  2359. ZERO(MV_PCI_ERR_LOW_ADDRESS);
  2360. ZERO(MV_PCI_ERR_HIGH_ADDRESS);
  2361. ZERO(MV_PCI_ERR_ATTRIBUTE);
  2362. ZERO(MV_PCI_ERR_COMMAND);
  2363. }
  2364. #undef ZERO
  2365. static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
  2366. {
  2367. u32 tmp;
  2368. mv5_reset_flash(hpriv, mmio);
  2369. tmp = readl(mmio + MV_GPIO_PORT_CTL_OFS);
  2370. tmp &= 0x3;
  2371. tmp |= (1 << 5) | (1 << 6);
  2372. writel(tmp, mmio + MV_GPIO_PORT_CTL_OFS);
  2373. }
  2374. /**
  2375. * mv6_reset_hc - Perform the 6xxx global soft reset
  2376. * @mmio: base address of the HBA
  2377. *
  2378. * This routine only applies to 6xxx parts.
  2379. *
  2380. * LOCKING:
  2381. * Inherited from caller.
  2382. */
  2383. static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  2384. unsigned int n_hc)
  2385. {
  2386. void __iomem *reg = mmio + PCI_MAIN_CMD_STS_OFS;
  2387. int i, rc = 0;
  2388. u32 t;
  2389. /* Following procedure defined in PCI "main command and status
  2390. * register" table.
  2391. */
  2392. t = readl(reg);
  2393. writel(t | STOP_PCI_MASTER, reg);
  2394. for (i = 0; i < 1000; i++) {
  2395. udelay(1);
  2396. t = readl(reg);
  2397. if (PCI_MASTER_EMPTY & t)
  2398. break;
  2399. }
  2400. if (!(PCI_MASTER_EMPTY & t)) {
  2401. printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
  2402. rc = 1;
  2403. goto done;
  2404. }
  2405. /* set reset */
  2406. i = 5;
  2407. do {
  2408. writel(t | GLOB_SFT_RST, reg);
  2409. t = readl(reg);
  2410. udelay(1);
  2411. } while (!(GLOB_SFT_RST & t) && (i-- > 0));
  2412. if (!(GLOB_SFT_RST & t)) {
  2413. printk(KERN_ERR DRV_NAME ": can't set global reset\n");
  2414. rc = 1;
  2415. goto done;
  2416. }
  2417. /* clear reset and *reenable the PCI master* (not mentioned in spec) */
  2418. i = 5;
  2419. do {
  2420. writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
  2421. t = readl(reg);
  2422. udelay(1);
  2423. } while ((GLOB_SFT_RST & t) && (i-- > 0));
  2424. if (GLOB_SFT_RST & t) {
  2425. printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
  2426. rc = 1;
  2427. }
  2428. done:
  2429. return rc;
  2430. }
  2431. static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
  2432. void __iomem *mmio)
  2433. {
  2434. void __iomem *port_mmio;
  2435. u32 tmp;
  2436. tmp = readl(mmio + MV_RESET_CFG_OFS);
  2437. if ((tmp & (1 << 0)) == 0) {
  2438. hpriv->signal[idx].amps = 0x7 << 8;
  2439. hpriv->signal[idx].pre = 0x1 << 5;
  2440. return;
  2441. }
  2442. port_mmio = mv_port_base(mmio, idx);
  2443. tmp = readl(port_mmio + PHY_MODE2);
  2444. hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
  2445. hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
  2446. }
  2447. static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
  2448. {
  2449. writel(0x00000060, mmio + MV_GPIO_PORT_CTL_OFS);
  2450. }
  2451. static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  2452. unsigned int port)
  2453. {
  2454. void __iomem *port_mmio = mv_port_base(mmio, port);
  2455. u32 hp_flags = hpriv->hp_flags;
  2456. int fix_phy_mode2 =
  2457. hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
  2458. int fix_phy_mode4 =
  2459. hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
  2460. u32 m2, m3;
  2461. if (fix_phy_mode2) {
  2462. m2 = readl(port_mmio + PHY_MODE2);
  2463. m2 &= ~(1 << 16);
  2464. m2 |= (1 << 31);
  2465. writel(m2, port_mmio + PHY_MODE2);
  2466. udelay(200);
  2467. m2 = readl(port_mmio + PHY_MODE2);
  2468. m2 &= ~((1 << 16) | (1 << 31));
  2469. writel(m2, port_mmio + PHY_MODE2);
  2470. udelay(200);
  2471. }
  2472. /*
  2473. * Gen-II/IIe PHY_MODE3 errata RM#2:
  2474. * Achieves better receiver noise performance than the h/w default:
  2475. */
  2476. m3 = readl(port_mmio + PHY_MODE3);
  2477. m3 = (m3 & 0x1f) | (0x5555601 << 5);
  2478. /* Guideline 88F5182 (GL# SATA-S11) */
  2479. if (IS_SOC(hpriv))
  2480. m3 &= ~0x1c;
  2481. if (fix_phy_mode4) {
  2482. u32 m4 = readl(port_mmio + PHY_MODE4);
  2483. /*
  2484. * Enforce reserved-bit restrictions on GenIIe devices only.
  2485. * For earlier chipsets, force only the internal config field
  2486. * (workaround for errata FEr SATA#10 part 1).
  2487. */
  2488. if (IS_GEN_IIE(hpriv))
  2489. m4 = (m4 & ~PHY_MODE4_RSVD_ZEROS) | PHY_MODE4_RSVD_ONES;
  2490. else
  2491. m4 = (m4 & ~PHY_MODE4_CFG_MASK) | PHY_MODE4_CFG_VALUE;
  2492. writel(m4, port_mmio + PHY_MODE4);
  2493. }
  2494. /*
  2495. * Workaround for 60x1-B2 errata SATA#13:
  2496. * Any write to PHY_MODE4 (above) may corrupt PHY_MODE3,
  2497. * so we must always rewrite PHY_MODE3 after PHY_MODE4.
  2498. */
  2499. writel(m3, port_mmio + PHY_MODE3);
  2500. /* Revert values of pre-emphasis and signal amps to the saved ones */
  2501. m2 = readl(port_mmio + PHY_MODE2);
  2502. m2 &= ~MV_M2_PREAMP_MASK;
  2503. m2 |= hpriv->signal[port].amps;
  2504. m2 |= hpriv->signal[port].pre;
  2505. m2 &= ~(1 << 16);
  2506. /* according to mvSata 3.6.1, some IIE values are fixed */
  2507. if (IS_GEN_IIE(hpriv)) {
  2508. m2 &= ~0xC30FF01F;
  2509. m2 |= 0x0000900F;
  2510. }
  2511. writel(m2, port_mmio + PHY_MODE2);
  2512. }
  2513. /* TODO: use the generic LED interface to configure the SATA Presence */
  2514. /* & Acitivy LEDs on the board */
  2515. static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
  2516. void __iomem *mmio)
  2517. {
  2518. return;
  2519. }
  2520. static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
  2521. void __iomem *mmio)
  2522. {
  2523. void __iomem *port_mmio;
  2524. u32 tmp;
  2525. port_mmio = mv_port_base(mmio, idx);
  2526. tmp = readl(port_mmio + PHY_MODE2);
  2527. hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
  2528. hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
  2529. }
  2530. #undef ZERO
  2531. #define ZERO(reg) writel(0, port_mmio + (reg))
  2532. static void mv_soc_reset_hc_port(struct mv_host_priv *hpriv,
  2533. void __iomem *mmio, unsigned int port)
  2534. {
  2535. void __iomem *port_mmio = mv_port_base(mmio, port);
  2536. mv_reset_channel(hpriv, mmio, port);
  2537. ZERO(0x028); /* command */
  2538. writel(0x101f, port_mmio + EDMA_CFG_OFS);
  2539. ZERO(0x004); /* timer */
  2540. ZERO(0x008); /* irq err cause */
  2541. ZERO(0x00c); /* irq err mask */
  2542. ZERO(0x010); /* rq bah */
  2543. ZERO(0x014); /* rq inp */
  2544. ZERO(0x018); /* rq outp */
  2545. ZERO(0x01c); /* respq bah */
  2546. ZERO(0x024); /* respq outp */
  2547. ZERO(0x020); /* respq inp */
  2548. ZERO(0x02c); /* test control */
  2549. writel(0xbc, port_mmio + EDMA_IORDY_TMOUT_OFS);
  2550. }
  2551. #undef ZERO
  2552. #define ZERO(reg) writel(0, hc_mmio + (reg))
  2553. static void mv_soc_reset_one_hc(struct mv_host_priv *hpriv,
  2554. void __iomem *mmio)
  2555. {
  2556. void __iomem *hc_mmio = mv_hc_base(mmio, 0);
  2557. ZERO(0x00c);
  2558. ZERO(0x010);
  2559. ZERO(0x014);
  2560. }
  2561. #undef ZERO
  2562. static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
  2563. void __iomem *mmio, unsigned int n_hc)
  2564. {
  2565. unsigned int port;
  2566. for (port = 0; port < hpriv->n_ports; port++)
  2567. mv_soc_reset_hc_port(hpriv, mmio, port);
  2568. mv_soc_reset_one_hc(hpriv, mmio);
  2569. return 0;
  2570. }
  2571. static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
  2572. void __iomem *mmio)
  2573. {
  2574. return;
  2575. }
  2576. static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio)
  2577. {
  2578. return;
  2579. }
  2580. static void mv_setup_ifcfg(void __iomem *port_mmio, int want_gen2i)
  2581. {
  2582. u32 ifcfg = readl(port_mmio + SATA_INTERFACE_CFG_OFS);
  2583. ifcfg = (ifcfg & 0xf7f) | 0x9b1000; /* from chip spec */
  2584. if (want_gen2i)
  2585. ifcfg |= (1 << 7); /* enable gen2i speed */
  2586. writelfl(ifcfg, port_mmio + SATA_INTERFACE_CFG_OFS);
  2587. }
  2588. static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
  2589. unsigned int port_no)
  2590. {
  2591. void __iomem *port_mmio = mv_port_base(mmio, port_no);
  2592. /*
  2593. * The datasheet warns against setting EDMA_RESET when EDMA is active
  2594. * (but doesn't say what the problem might be). So we first try
  2595. * to disable the EDMA engine before doing the EDMA_RESET operation.
  2596. */
  2597. mv_stop_edma_engine(port_mmio);
  2598. writelfl(EDMA_RESET, port_mmio + EDMA_CMD_OFS);
  2599. if (!IS_GEN_I(hpriv)) {
  2600. /* Enable 3.0gb/s link speed: this survives EDMA_RESET */
  2601. mv_setup_ifcfg(port_mmio, 1);
  2602. }
  2603. /*
  2604. * Strobing EDMA_RESET here causes a hard reset of the SATA transport,
  2605. * link, and physical layers. It resets all SATA interface registers
  2606. * (except for SATA_INTERFACE_CFG), and issues a COMRESET to the dev.
  2607. */
  2608. writelfl(EDMA_RESET, port_mmio + EDMA_CMD_OFS);
  2609. udelay(25); /* allow reset propagation */
  2610. writelfl(0, port_mmio + EDMA_CMD_OFS);
  2611. hpriv->ops->phy_errata(hpriv, mmio, port_no);
  2612. if (IS_GEN_I(hpriv))
  2613. mdelay(1);
  2614. }
  2615. static void mv_pmp_select(struct ata_port *ap, int pmp)
  2616. {
  2617. if (sata_pmp_supported(ap)) {
  2618. void __iomem *port_mmio = mv_ap_base(ap);
  2619. u32 reg = readl(port_mmio + SATA_IFCTL_OFS);
  2620. int old = reg & 0xf;
  2621. if (old != pmp) {
  2622. reg = (reg & ~0xf) | pmp;
  2623. writelfl(reg, port_mmio + SATA_IFCTL_OFS);
  2624. }
  2625. }
  2626. }
  2627. static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
  2628. unsigned long deadline)
  2629. {
  2630. mv_pmp_select(link->ap, sata_srst_pmp(link));
  2631. return sata_std_hardreset(link, class, deadline);
  2632. }
  2633. static int mv_softreset(struct ata_link *link, unsigned int *class,
  2634. unsigned long deadline)
  2635. {
  2636. mv_pmp_select(link->ap, sata_srst_pmp(link));
  2637. return ata_sff_softreset(link, class, deadline);
  2638. }
  2639. static int mv_hardreset(struct ata_link *link, unsigned int *class,
  2640. unsigned long deadline)
  2641. {
  2642. struct ata_port *ap = link->ap;
  2643. struct mv_host_priv *hpriv = ap->host->private_data;
  2644. struct mv_port_priv *pp = ap->private_data;
  2645. void __iomem *mmio = hpriv->base;
  2646. int rc, attempts = 0, extra = 0;
  2647. u32 sstatus;
  2648. bool online;
  2649. mv_reset_channel(hpriv, mmio, ap->port_no);
  2650. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  2651. /* Workaround for errata FEr SATA#10 (part 2) */
  2652. do {
  2653. const unsigned long *timing =
  2654. sata_ehc_deb_timing(&link->eh_context);
  2655. rc = sata_link_hardreset(link, timing, deadline + extra,
  2656. &online, NULL);
  2657. rc = online ? -EAGAIN : rc;
  2658. if (rc)
  2659. return rc;
  2660. sata_scr_read(link, SCR_STATUS, &sstatus);
  2661. if (!IS_GEN_I(hpriv) && ++attempts >= 5 && sstatus == 0x121) {
  2662. /* Force 1.5gb/s link speed and try again */
  2663. mv_setup_ifcfg(mv_ap_base(ap), 0);
  2664. if (time_after(jiffies + HZ, deadline))
  2665. extra = HZ; /* only extend it once, max */
  2666. }
  2667. } while (sstatus != 0x0 && sstatus != 0x113 && sstatus != 0x123);
  2668. mv_save_cached_regs(ap);
  2669. mv_edma_cfg(ap, 0, 0);
  2670. return rc;
  2671. }
  2672. static void mv_eh_freeze(struct ata_port *ap)
  2673. {
  2674. mv_stop_edma(ap);
  2675. mv_enable_port_irqs(ap, 0);
  2676. }
  2677. static void mv_eh_thaw(struct ata_port *ap)
  2678. {
  2679. struct mv_host_priv *hpriv = ap->host->private_data;
  2680. unsigned int port = ap->port_no;
  2681. unsigned int hardport = mv_hardport_from_port(port);
  2682. void __iomem *hc_mmio = mv_hc_base_from_port(hpriv->base, port);
  2683. void __iomem *port_mmio = mv_ap_base(ap);
  2684. u32 hc_irq_cause;
  2685. /* clear EDMA errors on this port */
  2686. writel(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  2687. /* clear pending irq events */
  2688. hc_irq_cause = ~((DEV_IRQ | DMA_IRQ) << hardport);
  2689. writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
  2690. mv_enable_port_irqs(ap, ERR_IRQ);
  2691. }
  2692. /**
  2693. * mv_port_init - Perform some early initialization on a single port.
  2694. * @port: libata data structure storing shadow register addresses
  2695. * @port_mmio: base address of the port
  2696. *
  2697. * Initialize shadow register mmio addresses, clear outstanding
  2698. * interrupts on the port, and unmask interrupts for the future
  2699. * start of the port.
  2700. *
  2701. * LOCKING:
  2702. * Inherited from caller.
  2703. */
  2704. static void mv_port_init(struct ata_ioports *port, void __iomem *port_mmio)
  2705. {
  2706. void __iomem *shd_base = port_mmio + SHD_BLK_OFS;
  2707. unsigned serr_ofs;
  2708. /* PIO related setup
  2709. */
  2710. port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
  2711. port->error_addr =
  2712. port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
  2713. port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
  2714. port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
  2715. port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
  2716. port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
  2717. port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
  2718. port->status_addr =
  2719. port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
  2720. /* special case: control/altstatus doesn't have ATA_REG_ address */
  2721. port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST_OFS;
  2722. /* unused: */
  2723. port->cmd_addr = port->bmdma_addr = port->scr_addr = NULL;
  2724. /* Clear any currently outstanding port interrupt conditions */
  2725. serr_ofs = mv_scr_offset(SCR_ERROR);
  2726. writelfl(readl(port_mmio + serr_ofs), port_mmio + serr_ofs);
  2727. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  2728. /* unmask all non-transient EDMA error interrupts */
  2729. writelfl(~EDMA_ERR_IRQ_TRANSIENT, port_mmio + EDMA_ERR_IRQ_MASK_OFS);
  2730. VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
  2731. readl(port_mmio + EDMA_CFG_OFS),
  2732. readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS),
  2733. readl(port_mmio + EDMA_ERR_IRQ_MASK_OFS));
  2734. }
  2735. static unsigned int mv_in_pcix_mode(struct ata_host *host)
  2736. {
  2737. struct mv_host_priv *hpriv = host->private_data;
  2738. void __iomem *mmio = hpriv->base;
  2739. u32 reg;
  2740. if (IS_SOC(hpriv) || !IS_PCIE(hpriv))
  2741. return 0; /* not PCI-X capable */
  2742. reg = readl(mmio + MV_PCI_MODE_OFS);
  2743. if ((reg & MV_PCI_MODE_MASK) == 0)
  2744. return 0; /* conventional PCI mode */
  2745. return 1; /* chip is in PCI-X mode */
  2746. }
  2747. static int mv_pci_cut_through_okay(struct ata_host *host)
  2748. {
  2749. struct mv_host_priv *hpriv = host->private_data;
  2750. void __iomem *mmio = hpriv->base;
  2751. u32 reg;
  2752. if (!mv_in_pcix_mode(host)) {
  2753. reg = readl(mmio + PCI_COMMAND_OFS);
  2754. if (reg & PCI_COMMAND_MRDTRIG)
  2755. return 0; /* not okay */
  2756. }
  2757. return 1; /* okay */
  2758. }
  2759. static int mv_chip_id(struct ata_host *host, unsigned int board_idx)
  2760. {
  2761. struct pci_dev *pdev = to_pci_dev(host->dev);
  2762. struct mv_host_priv *hpriv = host->private_data;
  2763. u32 hp_flags = hpriv->hp_flags;
  2764. switch (board_idx) {
  2765. case chip_5080:
  2766. hpriv->ops = &mv5xxx_ops;
  2767. hp_flags |= MV_HP_GEN_I;
  2768. switch (pdev->revision) {
  2769. case 0x1:
  2770. hp_flags |= MV_HP_ERRATA_50XXB0;
  2771. break;
  2772. case 0x3:
  2773. hp_flags |= MV_HP_ERRATA_50XXB2;
  2774. break;
  2775. default:
  2776. dev_printk(KERN_WARNING, &pdev->dev,
  2777. "Applying 50XXB2 workarounds to unknown rev\n");
  2778. hp_flags |= MV_HP_ERRATA_50XXB2;
  2779. break;
  2780. }
  2781. break;
  2782. case chip_504x:
  2783. case chip_508x:
  2784. hpriv->ops = &mv5xxx_ops;
  2785. hp_flags |= MV_HP_GEN_I;
  2786. switch (pdev->revision) {
  2787. case 0x0:
  2788. hp_flags |= MV_HP_ERRATA_50XXB0;
  2789. break;
  2790. case 0x3:
  2791. hp_flags |= MV_HP_ERRATA_50XXB2;
  2792. break;
  2793. default:
  2794. dev_printk(KERN_WARNING, &pdev->dev,
  2795. "Applying B2 workarounds to unknown rev\n");
  2796. hp_flags |= MV_HP_ERRATA_50XXB2;
  2797. break;
  2798. }
  2799. break;
  2800. case chip_604x:
  2801. case chip_608x:
  2802. hpriv->ops = &mv6xxx_ops;
  2803. hp_flags |= MV_HP_GEN_II;
  2804. switch (pdev->revision) {
  2805. case 0x7:
  2806. hp_flags |= MV_HP_ERRATA_60X1B2;
  2807. break;
  2808. case 0x9:
  2809. hp_flags |= MV_HP_ERRATA_60X1C0;
  2810. break;
  2811. default:
  2812. dev_printk(KERN_WARNING, &pdev->dev,
  2813. "Applying B2 workarounds to unknown rev\n");
  2814. hp_flags |= MV_HP_ERRATA_60X1B2;
  2815. break;
  2816. }
  2817. break;
  2818. case chip_7042:
  2819. hp_flags |= MV_HP_PCIE | MV_HP_CUT_THROUGH;
  2820. if (pdev->vendor == PCI_VENDOR_ID_TTI &&
  2821. (pdev->device == 0x2300 || pdev->device == 0x2310))
  2822. {
  2823. /*
  2824. * Highpoint RocketRAID PCIe 23xx series cards:
  2825. *
  2826. * Unconfigured drives are treated as "Legacy"
  2827. * by the BIOS, and it overwrites sector 8 with
  2828. * a "Lgcy" metadata block prior to Linux boot.
  2829. *
  2830. * Configured drives (RAID or JBOD) leave sector 8
  2831. * alone, but instead overwrite a high numbered
  2832. * sector for the RAID metadata. This sector can
  2833. * be determined exactly, by truncating the physical
  2834. * drive capacity to a nice even GB value.
  2835. *
  2836. * RAID metadata is at: (dev->n_sectors & ~0xfffff)
  2837. *
  2838. * Warn the user, lest they think we're just buggy.
  2839. */
  2840. printk(KERN_WARNING DRV_NAME ": Highpoint RocketRAID"
  2841. " BIOS CORRUPTS DATA on all attached drives,"
  2842. " regardless of if/how they are configured."
  2843. " BEWARE!\n");
  2844. printk(KERN_WARNING DRV_NAME ": For data safety, do not"
  2845. " use sectors 8-9 on \"Legacy\" drives,"
  2846. " and avoid the final two gigabytes on"
  2847. " all RocketRAID BIOS initialized drives.\n");
  2848. }
  2849. /* drop through */
  2850. case chip_6042:
  2851. hpriv->ops = &mv6xxx_ops;
  2852. hp_flags |= MV_HP_GEN_IIE;
  2853. if (board_idx == chip_6042 && mv_pci_cut_through_okay(host))
  2854. hp_flags |= MV_HP_CUT_THROUGH;
  2855. switch (pdev->revision) {
  2856. case 0x2: /* Rev.B0: the first/only public release */
  2857. hp_flags |= MV_HP_ERRATA_60X1C0;
  2858. break;
  2859. default:
  2860. dev_printk(KERN_WARNING, &pdev->dev,
  2861. "Applying 60X1C0 workarounds to unknown rev\n");
  2862. hp_flags |= MV_HP_ERRATA_60X1C0;
  2863. break;
  2864. }
  2865. break;
  2866. case chip_soc:
  2867. hpriv->ops = &mv_soc_ops;
  2868. hp_flags |= MV_HP_FLAG_SOC | MV_HP_GEN_IIE |
  2869. MV_HP_ERRATA_60X1C0;
  2870. break;
  2871. default:
  2872. dev_printk(KERN_ERR, host->dev,
  2873. "BUG: invalid board index %u\n", board_idx);
  2874. return 1;
  2875. }
  2876. hpriv->hp_flags = hp_flags;
  2877. if (hp_flags & MV_HP_PCIE) {
  2878. hpriv->irq_cause_ofs = PCIE_IRQ_CAUSE_OFS;
  2879. hpriv->irq_mask_ofs = PCIE_IRQ_MASK_OFS;
  2880. hpriv->unmask_all_irqs = PCIE_UNMASK_ALL_IRQS;
  2881. } else {
  2882. hpriv->irq_cause_ofs = PCI_IRQ_CAUSE_OFS;
  2883. hpriv->irq_mask_ofs = PCI_IRQ_MASK_OFS;
  2884. hpriv->unmask_all_irqs = PCI_UNMASK_ALL_IRQS;
  2885. }
  2886. return 0;
  2887. }
  2888. /**
  2889. * mv_init_host - Perform some early initialization of the host.
  2890. * @host: ATA host to initialize
  2891. * @board_idx: controller index
  2892. *
  2893. * If possible, do an early global reset of the host. Then do
  2894. * our port init and clear/unmask all/relevant host interrupts.
  2895. *
  2896. * LOCKING:
  2897. * Inherited from caller.
  2898. */
  2899. static int mv_init_host(struct ata_host *host, unsigned int board_idx)
  2900. {
  2901. int rc = 0, n_hc, port, hc;
  2902. struct mv_host_priv *hpriv = host->private_data;
  2903. void __iomem *mmio = hpriv->base;
  2904. rc = mv_chip_id(host, board_idx);
  2905. if (rc)
  2906. goto done;
  2907. if (IS_SOC(hpriv)) {
  2908. hpriv->main_irq_cause_addr = mmio + SOC_HC_MAIN_IRQ_CAUSE_OFS;
  2909. hpriv->main_irq_mask_addr = mmio + SOC_HC_MAIN_IRQ_MASK_OFS;
  2910. } else {
  2911. hpriv->main_irq_cause_addr = mmio + PCI_HC_MAIN_IRQ_CAUSE_OFS;
  2912. hpriv->main_irq_mask_addr = mmio + PCI_HC_MAIN_IRQ_MASK_OFS;
  2913. }
  2914. /* initialize shadow irq mask with register's value */
  2915. hpriv->main_irq_mask = readl(hpriv->main_irq_mask_addr);
  2916. /* global interrupt mask: 0 == mask everything */
  2917. mv_set_main_irq_mask(host, ~0, 0);
  2918. n_hc = mv_get_hc_count(host->ports[0]->flags);
  2919. for (port = 0; port < host->n_ports; port++)
  2920. hpriv->ops->read_preamp(hpriv, port, mmio);
  2921. rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc);
  2922. if (rc)
  2923. goto done;
  2924. hpriv->ops->reset_flash(hpriv, mmio);
  2925. hpriv->ops->reset_bus(host, mmio);
  2926. hpriv->ops->enable_leds(hpriv, mmio);
  2927. for (port = 0; port < host->n_ports; port++) {
  2928. struct ata_port *ap = host->ports[port];
  2929. void __iomem *port_mmio = mv_port_base(mmio, port);
  2930. mv_port_init(&ap->ioaddr, port_mmio);
  2931. #ifdef CONFIG_PCI
  2932. if (!IS_SOC(hpriv)) {
  2933. unsigned int offset = port_mmio - mmio;
  2934. ata_port_pbar_desc(ap, MV_PRIMARY_BAR, -1, "mmio");
  2935. ata_port_pbar_desc(ap, MV_PRIMARY_BAR, offset, "port");
  2936. }
  2937. #endif
  2938. }
  2939. for (hc = 0; hc < n_hc; hc++) {
  2940. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  2941. VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
  2942. "(before clear)=0x%08x\n", hc,
  2943. readl(hc_mmio + HC_CFG_OFS),
  2944. readl(hc_mmio + HC_IRQ_CAUSE_OFS));
  2945. /* Clear any currently outstanding hc interrupt conditions */
  2946. writelfl(0, hc_mmio + HC_IRQ_CAUSE_OFS);
  2947. }
  2948. /* Clear any currently outstanding host interrupt conditions */
  2949. writelfl(0, mmio + hpriv->irq_cause_ofs);
  2950. /* and unmask interrupt generation for host regs */
  2951. writelfl(hpriv->unmask_all_irqs, mmio + hpriv->irq_mask_ofs);
  2952. /*
  2953. * enable only global host interrupts for now.
  2954. * The per-port interrupts get done later as ports are set up.
  2955. */
  2956. mv_set_main_irq_mask(host, 0, PCI_ERR);
  2957. done:
  2958. return rc;
  2959. }
  2960. static int mv_create_dma_pools(struct mv_host_priv *hpriv, struct device *dev)
  2961. {
  2962. hpriv->crqb_pool = dmam_pool_create("crqb_q", dev, MV_CRQB_Q_SZ,
  2963. MV_CRQB_Q_SZ, 0);
  2964. if (!hpriv->crqb_pool)
  2965. return -ENOMEM;
  2966. hpriv->crpb_pool = dmam_pool_create("crpb_q", dev, MV_CRPB_Q_SZ,
  2967. MV_CRPB_Q_SZ, 0);
  2968. if (!hpriv->crpb_pool)
  2969. return -ENOMEM;
  2970. hpriv->sg_tbl_pool = dmam_pool_create("sg_tbl", dev, MV_SG_TBL_SZ,
  2971. MV_SG_TBL_SZ, 0);
  2972. if (!hpriv->sg_tbl_pool)
  2973. return -ENOMEM;
  2974. return 0;
  2975. }
  2976. static void mv_conf_mbus_windows(struct mv_host_priv *hpriv,
  2977. struct mbus_dram_target_info *dram)
  2978. {
  2979. int i;
  2980. for (i = 0; i < 4; i++) {
  2981. writel(0, hpriv->base + WINDOW_CTRL(i));
  2982. writel(0, hpriv->base + WINDOW_BASE(i));
  2983. }
  2984. for (i = 0; i < dram->num_cs; i++) {
  2985. struct mbus_dram_window *cs = dram->cs + i;
  2986. writel(((cs->size - 1) & 0xffff0000) |
  2987. (cs->mbus_attr << 8) |
  2988. (dram->mbus_dram_target_id << 4) | 1,
  2989. hpriv->base + WINDOW_CTRL(i));
  2990. writel(cs->base, hpriv->base + WINDOW_BASE(i));
  2991. }
  2992. }
  2993. /**
  2994. * mv_platform_probe - handle a positive probe of an soc Marvell
  2995. * host
  2996. * @pdev: platform device found
  2997. *
  2998. * LOCKING:
  2999. * Inherited from caller.
  3000. */
  3001. static int mv_platform_probe(struct platform_device *pdev)
  3002. {
  3003. static int printed_version;
  3004. const struct mv_sata_platform_data *mv_platform_data;
  3005. const struct ata_port_info *ppi[] =
  3006. { &mv_port_info[chip_soc], NULL };
  3007. struct ata_host *host;
  3008. struct mv_host_priv *hpriv;
  3009. struct resource *res;
  3010. int n_ports, rc;
  3011. if (!printed_version++)
  3012. dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
  3013. /*
  3014. * Simple resource validation ..
  3015. */
  3016. if (unlikely(pdev->num_resources != 2)) {
  3017. dev_err(&pdev->dev, "invalid number of resources\n");
  3018. return -EINVAL;
  3019. }
  3020. /*
  3021. * Get the register base first
  3022. */
  3023. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  3024. if (res == NULL)
  3025. return -EINVAL;
  3026. /* allocate host */
  3027. mv_platform_data = pdev->dev.platform_data;
  3028. n_ports = mv_platform_data->n_ports;
  3029. host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
  3030. hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
  3031. if (!host || !hpriv)
  3032. return -ENOMEM;
  3033. host->private_data = hpriv;
  3034. hpriv->n_ports = n_ports;
  3035. host->iomap = NULL;
  3036. hpriv->base = devm_ioremap(&pdev->dev, res->start,
  3037. res->end - res->start + 1);
  3038. hpriv->base -= MV_SATAHC0_REG_BASE;
  3039. /*
  3040. * (Re-)program MBUS remapping windows if we are asked to.
  3041. */
  3042. if (mv_platform_data->dram != NULL)
  3043. mv_conf_mbus_windows(hpriv, mv_platform_data->dram);
  3044. rc = mv_create_dma_pools(hpriv, &pdev->dev);
  3045. if (rc)
  3046. return rc;
  3047. /* initialize adapter */
  3048. rc = mv_init_host(host, chip_soc);
  3049. if (rc)
  3050. return rc;
  3051. dev_printk(KERN_INFO, &pdev->dev,
  3052. "slots %u ports %d\n", (unsigned)MV_MAX_Q_DEPTH,
  3053. host->n_ports);
  3054. return ata_host_activate(host, platform_get_irq(pdev, 0), mv_interrupt,
  3055. IRQF_SHARED, &mv6_sht);
  3056. }
  3057. /*
  3058. *
  3059. * mv_platform_remove - unplug a platform interface
  3060. * @pdev: platform device
  3061. *
  3062. * A platform bus SATA device has been unplugged. Perform the needed
  3063. * cleanup. Also called on module unload for any active devices.
  3064. */
  3065. static int __devexit mv_platform_remove(struct platform_device *pdev)
  3066. {
  3067. struct device *dev = &pdev->dev;
  3068. struct ata_host *host = dev_get_drvdata(dev);
  3069. ata_host_detach(host);
  3070. return 0;
  3071. }
  3072. static struct platform_driver mv_platform_driver = {
  3073. .probe = mv_platform_probe,
  3074. .remove = __devexit_p(mv_platform_remove),
  3075. .driver = {
  3076. .name = DRV_NAME,
  3077. .owner = THIS_MODULE,
  3078. },
  3079. };
  3080. #ifdef CONFIG_PCI
  3081. static int mv_pci_init_one(struct pci_dev *pdev,
  3082. const struct pci_device_id *ent);
  3083. static struct pci_driver mv_pci_driver = {
  3084. .name = DRV_NAME,
  3085. .id_table = mv_pci_tbl,
  3086. .probe = mv_pci_init_one,
  3087. .remove = ata_pci_remove_one,
  3088. };
  3089. /*
  3090. * module options
  3091. */
  3092. static int msi; /* Use PCI msi; either zero (off, default) or non-zero */
  3093. /* move to PCI layer or libata core? */
  3094. static int pci_go_64(struct pci_dev *pdev)
  3095. {
  3096. int rc;
  3097. if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
  3098. rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  3099. if (rc) {
  3100. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  3101. if (rc) {
  3102. dev_printk(KERN_ERR, &pdev->dev,
  3103. "64-bit DMA enable failed\n");
  3104. return rc;
  3105. }
  3106. }
  3107. } else {
  3108. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  3109. if (rc) {
  3110. dev_printk(KERN_ERR, &pdev->dev,
  3111. "32-bit DMA enable failed\n");
  3112. return rc;
  3113. }
  3114. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  3115. if (rc) {
  3116. dev_printk(KERN_ERR, &pdev->dev,
  3117. "32-bit consistent DMA enable failed\n");
  3118. return rc;
  3119. }
  3120. }
  3121. return rc;
  3122. }
  3123. /**
  3124. * mv_print_info - Dump key info to kernel log for perusal.
  3125. * @host: ATA host to print info about
  3126. *
  3127. * FIXME: complete this.
  3128. *
  3129. * LOCKING:
  3130. * Inherited from caller.
  3131. */
  3132. static void mv_print_info(struct ata_host *host)
  3133. {
  3134. struct pci_dev *pdev = to_pci_dev(host->dev);
  3135. struct mv_host_priv *hpriv = host->private_data;
  3136. u8 scc;
  3137. const char *scc_s, *gen;
  3138. /* Use this to determine the HW stepping of the chip so we know
  3139. * what errata to workaround
  3140. */
  3141. pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
  3142. if (scc == 0)
  3143. scc_s = "SCSI";
  3144. else if (scc == 0x01)
  3145. scc_s = "RAID";
  3146. else
  3147. scc_s = "?";
  3148. if (IS_GEN_I(hpriv))
  3149. gen = "I";
  3150. else if (IS_GEN_II(hpriv))
  3151. gen = "II";
  3152. else if (IS_GEN_IIE(hpriv))
  3153. gen = "IIE";
  3154. else
  3155. gen = "?";
  3156. dev_printk(KERN_INFO, &pdev->dev,
  3157. "Gen-%s %u slots %u ports %s mode IRQ via %s\n",
  3158. gen, (unsigned)MV_MAX_Q_DEPTH, host->n_ports,
  3159. scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
  3160. }
  3161. /**
  3162. * mv_pci_init_one - handle a positive probe of a PCI Marvell host
  3163. * @pdev: PCI device found
  3164. * @ent: PCI device ID entry for the matched host
  3165. *
  3166. * LOCKING:
  3167. * Inherited from caller.
  3168. */
  3169. static int mv_pci_init_one(struct pci_dev *pdev,
  3170. const struct pci_device_id *ent)
  3171. {
  3172. static int printed_version;
  3173. unsigned int board_idx = (unsigned int)ent->driver_data;
  3174. const struct ata_port_info *ppi[] = { &mv_port_info[board_idx], NULL };
  3175. struct ata_host *host;
  3176. struct mv_host_priv *hpriv;
  3177. int n_ports, rc;
  3178. if (!printed_version++)
  3179. dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
  3180. /* allocate host */
  3181. n_ports = mv_get_hc_count(ppi[0]->flags) * MV_PORTS_PER_HC;
  3182. host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
  3183. hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
  3184. if (!host || !hpriv)
  3185. return -ENOMEM;
  3186. host->private_data = hpriv;
  3187. hpriv->n_ports = n_ports;
  3188. /* acquire resources */
  3189. rc = pcim_enable_device(pdev);
  3190. if (rc)
  3191. return rc;
  3192. rc = pcim_iomap_regions(pdev, 1 << MV_PRIMARY_BAR, DRV_NAME);
  3193. if (rc == -EBUSY)
  3194. pcim_pin_device(pdev);
  3195. if (rc)
  3196. return rc;
  3197. host->iomap = pcim_iomap_table(pdev);
  3198. hpriv->base = host->iomap[MV_PRIMARY_BAR];
  3199. rc = pci_go_64(pdev);
  3200. if (rc)
  3201. return rc;
  3202. rc = mv_create_dma_pools(hpriv, &pdev->dev);
  3203. if (rc)
  3204. return rc;
  3205. /* initialize adapter */
  3206. rc = mv_init_host(host, board_idx);
  3207. if (rc)
  3208. return rc;
  3209. /* Enable message-switched interrupts, if requested */
  3210. if (msi && pci_enable_msi(pdev) == 0)
  3211. hpriv->hp_flags |= MV_HP_FLAG_MSI;
  3212. mv_dump_pci_cfg(pdev, 0x68);
  3213. mv_print_info(host);
  3214. pci_set_master(pdev);
  3215. pci_try_set_mwi(pdev);
  3216. return ata_host_activate(host, pdev->irq, mv_interrupt, IRQF_SHARED,
  3217. IS_GEN_I(hpriv) ? &mv5_sht : &mv6_sht);
  3218. }
  3219. #endif
  3220. static int mv_platform_probe(struct platform_device *pdev);
  3221. static int __devexit mv_platform_remove(struct platform_device *pdev);
  3222. static int __init mv_init(void)
  3223. {
  3224. int rc = -ENODEV;
  3225. #ifdef CONFIG_PCI
  3226. rc = pci_register_driver(&mv_pci_driver);
  3227. if (rc < 0)
  3228. return rc;
  3229. #endif
  3230. rc = platform_driver_register(&mv_platform_driver);
  3231. #ifdef CONFIG_PCI
  3232. if (rc < 0)
  3233. pci_unregister_driver(&mv_pci_driver);
  3234. #endif
  3235. return rc;
  3236. }
  3237. static void __exit mv_exit(void)
  3238. {
  3239. #ifdef CONFIG_PCI
  3240. pci_unregister_driver(&mv_pci_driver);
  3241. #endif
  3242. platform_driver_unregister(&mv_platform_driver);
  3243. }
  3244. MODULE_AUTHOR("Brett Russ");
  3245. MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
  3246. MODULE_LICENSE("GPL");
  3247. MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
  3248. MODULE_VERSION(DRV_VERSION);
  3249. MODULE_ALIAS("platform:" DRV_NAME);
  3250. #ifdef CONFIG_PCI
  3251. module_param(msi, int, 0444);
  3252. MODULE_PARM_DESC(msi, "Enable use of PCI MSI (0=off, 1=on)");
  3253. #endif
  3254. module_init(mv_init);
  3255. module_exit(mv_exit);