sata_mv.c 103 KB

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