sata_mv.c 95 KB

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