sata_mv.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240
  1. /*
  2. * sata_mv.c - Marvell SATA support
  3. *
  4. * Copyright 2005: EMC Corporation, all rights reserved.
  5. * Copyright 2005 Red Hat, Inc. All rights reserved.
  6. *
  7. * Please ALWAYS copy linux-ide@vger.kernel.org on emails.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/pci.h>
  26. #include <linux/init.h>
  27. #include <linux/blkdev.h>
  28. #include <linux/delay.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/sched.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/device.h>
  33. #include <scsi/scsi_host.h>
  34. #include <scsi/scsi_cmnd.h>
  35. #include <linux/libata.h>
  36. #include <asm/io.h>
  37. #define DRV_NAME "sata_mv"
  38. #define DRV_VERSION "0.5"
  39. enum {
  40. /* BAR's are enumerated in terms of pci_resource_start() terms */
  41. MV_PRIMARY_BAR = 0, /* offset 0x10: memory space */
  42. MV_IO_BAR = 2, /* offset 0x18: IO space */
  43. MV_MISC_BAR = 3, /* offset 0x1c: FLASH, NVRAM, SRAM */
  44. MV_MAJOR_REG_AREA_SZ = 0x10000, /* 64KB */
  45. MV_MINOR_REG_AREA_SZ = 0x2000, /* 8KB */
  46. MV_PCI_REG_BASE = 0,
  47. MV_IRQ_COAL_REG_BASE = 0x18000, /* 6xxx part only */
  48. MV_SATAHC0_REG_BASE = 0x20000,
  49. MV_FLASH_CTL = 0x1046c,
  50. MV_GPIO_PORT_CTL = 0x104f0,
  51. MV_RESET_CFG = 0x180d8,
  52. MV_PCI_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  53. MV_SATAHC_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  54. MV_SATAHC_ARBTR_REG_SZ = MV_MINOR_REG_AREA_SZ, /* arbiter */
  55. MV_PORT_REG_SZ = MV_MINOR_REG_AREA_SZ,
  56. MV_USE_Q_DEPTH = ATA_DEF_QUEUE,
  57. MV_MAX_Q_DEPTH = 32,
  58. MV_MAX_Q_DEPTH_MASK = MV_MAX_Q_DEPTH - 1,
  59. /* CRQB needs alignment on a 1KB boundary. Size == 1KB
  60. * CRPB needs alignment on a 256B boundary. Size == 256B
  61. * SG count of 176 leads to MV_PORT_PRIV_DMA_SZ == 4KB
  62. * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
  63. */
  64. MV_CRQB_Q_SZ = (32 * MV_MAX_Q_DEPTH),
  65. MV_CRPB_Q_SZ = (8 * MV_MAX_Q_DEPTH),
  66. MV_MAX_SG_CT = 176,
  67. MV_SG_TBL_SZ = (16 * MV_MAX_SG_CT),
  68. MV_PORT_PRIV_DMA_SZ = (MV_CRQB_Q_SZ + MV_CRPB_Q_SZ + MV_SG_TBL_SZ),
  69. MV_PORTS_PER_HC = 4,
  70. /* == (port / MV_PORTS_PER_HC) to determine HC from 0-7 port */
  71. MV_PORT_HC_SHIFT = 2,
  72. /* == (port % MV_PORTS_PER_HC) to determine hard port from 0-7 port */
  73. MV_PORT_MASK = 3,
  74. /* Host Flags */
  75. MV_FLAG_DUAL_HC = (1 << 30), /* two SATA Host Controllers */
  76. MV_FLAG_IRQ_COALESCE = (1 << 29), /* IRQ coalescing capability */
  77. MV_COMMON_FLAGS = (ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  78. ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO |
  79. ATA_FLAG_NO_ATAPI),
  80. MV_6XXX_FLAGS = MV_FLAG_IRQ_COALESCE,
  81. CRQB_FLAG_READ = (1 << 0),
  82. CRQB_TAG_SHIFT = 1,
  83. CRQB_CMD_ADDR_SHIFT = 8,
  84. CRQB_CMD_CS = (0x2 << 11),
  85. CRQB_CMD_LAST = (1 << 15),
  86. CRPB_FLAG_STATUS_SHIFT = 8,
  87. EPRD_FLAG_END_OF_TBL = (1 << 31),
  88. /* PCI interface registers */
  89. PCI_COMMAND_OFS = 0xc00,
  90. PCI_MAIN_CMD_STS_OFS = 0xd30,
  91. STOP_PCI_MASTER = (1 << 2),
  92. PCI_MASTER_EMPTY = (1 << 3),
  93. GLOB_SFT_RST = (1 << 4),
  94. MV_PCI_MODE = 0xd00,
  95. MV_PCI_EXP_ROM_BAR_CTL = 0xd2c,
  96. MV_PCI_DISC_TIMER = 0xd04,
  97. MV_PCI_MSI_TRIGGER = 0xc38,
  98. MV_PCI_SERR_MASK = 0xc28,
  99. MV_PCI_XBAR_TMOUT = 0x1d04,
  100. MV_PCI_ERR_LOW_ADDRESS = 0x1d40,
  101. MV_PCI_ERR_HIGH_ADDRESS = 0x1d44,
  102. MV_PCI_ERR_ATTRIBUTE = 0x1d48,
  103. MV_PCI_ERR_COMMAND = 0x1d50,
  104. PCI_IRQ_CAUSE_OFS = 0x1d58,
  105. PCI_IRQ_MASK_OFS = 0x1d5c,
  106. PCI_UNMASK_ALL_IRQS = 0x7fffff, /* bits 22-0 */
  107. HC_MAIN_IRQ_CAUSE_OFS = 0x1d60,
  108. HC_MAIN_IRQ_MASK_OFS = 0x1d64,
  109. PORT0_ERR = (1 << 0), /* shift by port # */
  110. PORT0_DONE = (1 << 1), /* shift by port # */
  111. HC0_IRQ_PEND = 0x1ff, /* bits 0-8 = HC0's ports */
  112. HC_SHIFT = 9, /* bits 9-17 = HC1's ports */
  113. PCI_ERR = (1 << 18),
  114. TRAN_LO_DONE = (1 << 19), /* 6xxx: IRQ coalescing */
  115. TRAN_HI_DONE = (1 << 20), /* 6xxx: IRQ coalescing */
  116. PORTS_0_7_COAL_DONE = (1 << 21), /* 6xxx: IRQ coalescing */
  117. GPIO_INT = (1 << 22),
  118. SELF_INT = (1 << 23),
  119. TWSI_INT = (1 << 24),
  120. HC_MAIN_RSVD = (0x7f << 25), /* bits 31-25 */
  121. HC_MAIN_MASKED_IRQS = (TRAN_LO_DONE | TRAN_HI_DONE |
  122. PORTS_0_7_COAL_DONE | GPIO_INT | TWSI_INT |
  123. HC_MAIN_RSVD),
  124. /* SATAHC registers */
  125. HC_CFG_OFS = 0,
  126. HC_IRQ_CAUSE_OFS = 0x14,
  127. CRPB_DMA_DONE = (1 << 0), /* shift by port # */
  128. HC_IRQ_COAL = (1 << 4), /* IRQ coalescing */
  129. DEV_IRQ = (1 << 8), /* shift by port # */
  130. /* Shadow block registers */
  131. SHD_BLK_OFS = 0x100,
  132. SHD_CTL_AST_OFS = 0x20, /* ofs from SHD_BLK_OFS */
  133. /* SATA registers */
  134. SATA_STATUS_OFS = 0x300, /* ctrl, err regs follow status */
  135. SATA_ACTIVE_OFS = 0x350,
  136. PHY_MODE3 = 0x310,
  137. PHY_MODE4 = 0x314,
  138. PHY_MODE2 = 0x330,
  139. MV5_PHY_MODE = 0x74,
  140. MV5_LT_MODE = 0x30,
  141. MV5_PHY_CTL = 0x0C,
  142. SATA_INTERFACE_CTL = 0x050,
  143. MV_M2_PREAMP_MASK = 0x7e0,
  144. /* Port registers */
  145. EDMA_CFG_OFS = 0,
  146. EDMA_CFG_Q_DEPTH = 0, /* queueing disabled */
  147. EDMA_CFG_NCQ = (1 << 5),
  148. EDMA_CFG_NCQ_GO_ON_ERR = (1 << 14), /* continue on error */
  149. EDMA_CFG_RD_BRST_EXT = (1 << 11), /* read burst 512B */
  150. EDMA_CFG_WR_BUFF_LEN = (1 << 13), /* write buffer 512B */
  151. EDMA_ERR_IRQ_CAUSE_OFS = 0x8,
  152. EDMA_ERR_IRQ_MASK_OFS = 0xc,
  153. EDMA_ERR_D_PAR = (1 << 0),
  154. EDMA_ERR_PRD_PAR = (1 << 1),
  155. EDMA_ERR_DEV = (1 << 2),
  156. EDMA_ERR_DEV_DCON = (1 << 3),
  157. EDMA_ERR_DEV_CON = (1 << 4),
  158. EDMA_ERR_SERR = (1 << 5),
  159. EDMA_ERR_SELF_DIS = (1 << 7),
  160. EDMA_ERR_BIST_ASYNC = (1 << 8),
  161. EDMA_ERR_CRBQ_PAR = (1 << 9),
  162. EDMA_ERR_CRPB_PAR = (1 << 10),
  163. EDMA_ERR_INTRL_PAR = (1 << 11),
  164. EDMA_ERR_IORDY = (1 << 12),
  165. EDMA_ERR_LNK_CTRL_RX = (0xf << 13),
  166. EDMA_ERR_LNK_CTRL_RX_2 = (1 << 15),
  167. EDMA_ERR_LNK_DATA_RX = (0xf << 17),
  168. EDMA_ERR_LNK_CTRL_TX = (0x1f << 21),
  169. EDMA_ERR_LNK_DATA_TX = (0x1f << 26),
  170. EDMA_ERR_TRANS_PROTO = (1 << 31),
  171. EDMA_ERR_FATAL = (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
  172. EDMA_ERR_DEV_DCON | EDMA_ERR_CRBQ_PAR |
  173. EDMA_ERR_CRPB_PAR | EDMA_ERR_INTRL_PAR |
  174. EDMA_ERR_IORDY | EDMA_ERR_LNK_CTRL_RX_2 |
  175. EDMA_ERR_LNK_DATA_RX |
  176. EDMA_ERR_LNK_DATA_TX |
  177. EDMA_ERR_TRANS_PROTO),
  178. EDMA_REQ_Q_BASE_HI_OFS = 0x10,
  179. EDMA_REQ_Q_IN_PTR_OFS = 0x14, /* also contains BASE_LO */
  180. EDMA_REQ_Q_OUT_PTR_OFS = 0x18,
  181. EDMA_REQ_Q_PTR_SHIFT = 5,
  182. EDMA_RSP_Q_BASE_HI_OFS = 0x1c,
  183. EDMA_RSP_Q_IN_PTR_OFS = 0x20,
  184. EDMA_RSP_Q_OUT_PTR_OFS = 0x24, /* also contains BASE_LO */
  185. EDMA_RSP_Q_PTR_SHIFT = 3,
  186. EDMA_CMD_OFS = 0x28,
  187. EDMA_EN = (1 << 0),
  188. EDMA_DS = (1 << 1),
  189. ATA_RST = (1 << 2),
  190. EDMA_IORDY_TMOUT = 0x34,
  191. EDMA_ARB_CFG = 0x38,
  192. /* Host private flags (hp_flags) */
  193. MV_HP_FLAG_MSI = (1 << 0),
  194. MV_HP_ERRATA_50XXB0 = (1 << 1),
  195. MV_HP_ERRATA_50XXB2 = (1 << 2),
  196. MV_HP_ERRATA_60X1B2 = (1 << 3),
  197. MV_HP_ERRATA_60X1C0 = (1 << 4),
  198. MV_HP_50XX = (1 << 5),
  199. /* Port private flags (pp_flags) */
  200. MV_PP_FLAG_EDMA_EN = (1 << 0),
  201. MV_PP_FLAG_EDMA_DS_ACT = (1 << 1),
  202. };
  203. #define IS_50XX(hpriv) ((hpriv)->hp_flags & MV_HP_50XX)
  204. #define IS_60XX(hpriv) (((hpriv)->hp_flags & MV_HP_50XX) == 0)
  205. enum {
  206. /* Our DMA boundary is determined by an ePRD being unable to handle
  207. * anything larger than 64KB
  208. */
  209. MV_DMA_BOUNDARY = 0xffffU,
  210. EDMA_REQ_Q_BASE_LO_MASK = 0xfffffc00U,
  211. EDMA_RSP_Q_BASE_LO_MASK = 0xffffff00U,
  212. };
  213. enum chip_type {
  214. chip_504x,
  215. chip_508x,
  216. chip_5080,
  217. chip_604x,
  218. chip_608x,
  219. };
  220. /* Command ReQuest Block: 32B */
  221. struct mv_crqb {
  222. u32 sg_addr;
  223. u32 sg_addr_hi;
  224. u16 ctrl_flags;
  225. u16 ata_cmd[11];
  226. };
  227. /* Command ResPonse Block: 8B */
  228. struct mv_crpb {
  229. u16 id;
  230. u16 flags;
  231. u32 tmstmp;
  232. };
  233. /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
  234. struct mv_sg {
  235. u32 addr;
  236. u32 flags_size;
  237. u32 addr_hi;
  238. u32 reserved;
  239. };
  240. struct mv_port_priv {
  241. struct mv_crqb *crqb;
  242. dma_addr_t crqb_dma;
  243. struct mv_crpb *crpb;
  244. dma_addr_t crpb_dma;
  245. struct mv_sg *sg_tbl;
  246. dma_addr_t sg_tbl_dma;
  247. unsigned req_producer; /* cp of req_in_ptr */
  248. unsigned rsp_consumer; /* cp of rsp_out_ptr */
  249. u32 pp_flags;
  250. };
  251. struct mv_port_signal {
  252. u32 amps;
  253. u32 pre;
  254. };
  255. struct mv_host_priv;
  256. struct mv_hw_ops {
  257. void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio,
  258. unsigned int port);
  259. void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio);
  260. void (*read_preamp)(struct mv_host_priv *hpriv, int idx,
  261. void __iomem *mmio);
  262. int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio,
  263. unsigned int n_hc);
  264. void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio);
  265. void (*reset_bus)(struct pci_dev *pdev, void __iomem *mmio);
  266. };
  267. struct mv_host_priv {
  268. u32 hp_flags;
  269. struct mv_port_signal signal[8];
  270. const struct mv_hw_ops *ops;
  271. };
  272. static void mv_irq_clear(struct ata_port *ap);
  273. static u32 mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in);
  274. static void mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
  275. static u32 mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in);
  276. static void mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
  277. static void mv_phy_reset(struct ata_port *ap);
  278. static void __mv_phy_reset(struct ata_port *ap, int can_sleep);
  279. static void mv_host_stop(struct ata_host_set *host_set);
  280. static int mv_port_start(struct ata_port *ap);
  281. static void mv_port_stop(struct ata_port *ap);
  282. static void mv_qc_prep(struct ata_queued_cmd *qc);
  283. static unsigned int mv_qc_issue(struct ata_queued_cmd *qc);
  284. static irqreturn_t mv_interrupt(int irq, void *dev_instance,
  285. struct pt_regs *regs);
  286. static void mv_eng_timeout(struct ata_port *ap);
  287. static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  288. static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  289. unsigned int port);
  290. static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
  291. static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
  292. void __iomem *mmio);
  293. static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  294. unsigned int n_hc);
  295. static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
  296. static void mv5_reset_bus(struct pci_dev *pdev, void __iomem *mmio);
  297. static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  298. unsigned int port);
  299. static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
  300. static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
  301. void __iomem *mmio);
  302. static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  303. unsigned int n_hc);
  304. static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
  305. static void mv_reset_pci_bus(struct pci_dev *pdev, void __iomem *mmio);
  306. static void mv_channel_reset(struct mv_host_priv *hpriv, void __iomem *mmio,
  307. unsigned int port_no);
  308. static void mv_stop_and_reset(struct ata_port *ap);
  309. static struct scsi_host_template mv_sht = {
  310. .module = THIS_MODULE,
  311. .name = DRV_NAME,
  312. .ioctl = ata_scsi_ioctl,
  313. .queuecommand = ata_scsi_queuecmd,
  314. .eh_strategy_handler = ata_scsi_error,
  315. .can_queue = MV_USE_Q_DEPTH,
  316. .this_id = ATA_SHT_THIS_ID,
  317. .sg_tablesize = MV_MAX_SG_CT / 2,
  318. .max_sectors = ATA_MAX_SECTORS,
  319. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  320. .emulated = ATA_SHT_EMULATED,
  321. .use_clustering = ATA_SHT_USE_CLUSTERING,
  322. .proc_name = DRV_NAME,
  323. .dma_boundary = MV_DMA_BOUNDARY,
  324. .slave_configure = ata_scsi_slave_config,
  325. .bios_param = ata_std_bios_param,
  326. };
  327. static const struct ata_port_operations mv5_ops = {
  328. .port_disable = ata_port_disable,
  329. .tf_load = ata_tf_load,
  330. .tf_read = ata_tf_read,
  331. .check_status = ata_check_status,
  332. .exec_command = ata_exec_command,
  333. .dev_select = ata_std_dev_select,
  334. .phy_reset = mv_phy_reset,
  335. .qc_prep = mv_qc_prep,
  336. .qc_issue = mv_qc_issue,
  337. .eng_timeout = mv_eng_timeout,
  338. .irq_handler = mv_interrupt,
  339. .irq_clear = mv_irq_clear,
  340. .scr_read = mv5_scr_read,
  341. .scr_write = mv5_scr_write,
  342. .port_start = mv_port_start,
  343. .port_stop = mv_port_stop,
  344. .host_stop = mv_host_stop,
  345. };
  346. static const struct ata_port_operations mv6_ops = {
  347. .port_disable = ata_port_disable,
  348. .tf_load = ata_tf_load,
  349. .tf_read = ata_tf_read,
  350. .check_status = ata_check_status,
  351. .exec_command = ata_exec_command,
  352. .dev_select = ata_std_dev_select,
  353. .phy_reset = mv_phy_reset,
  354. .qc_prep = mv_qc_prep,
  355. .qc_issue = mv_qc_issue,
  356. .eng_timeout = mv_eng_timeout,
  357. .irq_handler = mv_interrupt,
  358. .irq_clear = mv_irq_clear,
  359. .scr_read = mv_scr_read,
  360. .scr_write = mv_scr_write,
  361. .port_start = mv_port_start,
  362. .port_stop = mv_port_stop,
  363. .host_stop = mv_host_stop,
  364. };
  365. static const struct ata_port_info mv_port_info[] = {
  366. { /* chip_504x */
  367. .sht = &mv_sht,
  368. .host_flags = MV_COMMON_FLAGS,
  369. .pio_mask = 0x1f, /* pio0-4 */
  370. .udma_mask = 0x7f, /* udma0-6 */
  371. .port_ops = &mv5_ops,
  372. },
  373. { /* chip_508x */
  374. .sht = &mv_sht,
  375. .host_flags = (MV_COMMON_FLAGS | MV_FLAG_DUAL_HC),
  376. .pio_mask = 0x1f, /* pio0-4 */
  377. .udma_mask = 0x7f, /* udma0-6 */
  378. .port_ops = &mv5_ops,
  379. },
  380. { /* chip_5080 */
  381. .sht = &mv_sht,
  382. .host_flags = (MV_COMMON_FLAGS | MV_FLAG_DUAL_HC),
  383. .pio_mask = 0x1f, /* pio0-4 */
  384. .udma_mask = 0x7f, /* udma0-6 */
  385. .port_ops = &mv5_ops,
  386. },
  387. { /* chip_604x */
  388. .sht = &mv_sht,
  389. .host_flags = (MV_COMMON_FLAGS | MV_6XXX_FLAGS),
  390. .pio_mask = 0x1f, /* pio0-4 */
  391. .udma_mask = 0x7f, /* udma0-6 */
  392. .port_ops = &mv6_ops,
  393. },
  394. { /* chip_608x */
  395. .sht = &mv_sht,
  396. .host_flags = (MV_COMMON_FLAGS | MV_6XXX_FLAGS |
  397. MV_FLAG_DUAL_HC),
  398. .pio_mask = 0x1f, /* pio0-4 */
  399. .udma_mask = 0x7f, /* udma0-6 */
  400. .port_ops = &mv6_ops,
  401. },
  402. };
  403. static const struct pci_device_id mv_pci_tbl[] = {
  404. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5040), 0, 0, chip_504x},
  405. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5041), 0, 0, chip_504x},
  406. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5080), 0, 0, chip_5080},
  407. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5081), 0, 0, chip_508x},
  408. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6040), 0, 0, chip_604x},
  409. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6041), 0, 0, chip_604x},
  410. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6080), 0, 0, chip_608x},
  411. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6081), 0, 0, chip_608x},
  412. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x0241), 0, 0, chip_604x},
  413. {} /* terminate list */
  414. };
  415. static struct pci_driver mv_pci_driver = {
  416. .name = DRV_NAME,
  417. .id_table = mv_pci_tbl,
  418. .probe = mv_init_one,
  419. .remove = ata_pci_remove_one,
  420. };
  421. static const struct mv_hw_ops mv5xxx_ops = {
  422. .phy_errata = mv5_phy_errata,
  423. .enable_leds = mv5_enable_leds,
  424. .read_preamp = mv5_read_preamp,
  425. .reset_hc = mv5_reset_hc,
  426. .reset_flash = mv5_reset_flash,
  427. .reset_bus = mv5_reset_bus,
  428. };
  429. static const struct mv_hw_ops mv6xxx_ops = {
  430. .phy_errata = mv6_phy_errata,
  431. .enable_leds = mv6_enable_leds,
  432. .read_preamp = mv6_read_preamp,
  433. .reset_hc = mv6_reset_hc,
  434. .reset_flash = mv6_reset_flash,
  435. .reset_bus = mv_reset_pci_bus,
  436. };
  437. /*
  438. * Functions
  439. */
  440. static inline void writelfl(unsigned long data, void __iomem *addr)
  441. {
  442. writel(data, addr);
  443. (void) readl(addr); /* flush to avoid PCI posted write */
  444. }
  445. static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
  446. {
  447. return (base + MV_SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
  448. }
  449. static inline unsigned int mv_hc_from_port(unsigned int port)
  450. {
  451. return port >> MV_PORT_HC_SHIFT;
  452. }
  453. static inline unsigned int mv_hardport_from_port(unsigned int port)
  454. {
  455. return port & MV_PORT_MASK;
  456. }
  457. static inline void __iomem *mv_hc_base_from_port(void __iomem *base,
  458. unsigned int port)
  459. {
  460. return mv_hc_base(base, mv_hc_from_port(port));
  461. }
  462. static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
  463. {
  464. return mv_hc_base_from_port(base, port) +
  465. MV_SATAHC_ARBTR_REG_SZ +
  466. (mv_hardport_from_port(port) * MV_PORT_REG_SZ);
  467. }
  468. static inline void __iomem *mv_ap_base(struct ata_port *ap)
  469. {
  470. return mv_port_base(ap->host_set->mmio_base, ap->port_no);
  471. }
  472. static inline int mv_get_hc_count(unsigned long host_flags)
  473. {
  474. return ((host_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
  475. }
  476. static void mv_irq_clear(struct ata_port *ap)
  477. {
  478. }
  479. /**
  480. * mv_start_dma - Enable eDMA engine
  481. * @base: port base address
  482. * @pp: port private data
  483. *
  484. * Verify the local cache of the eDMA state is accurate with an
  485. * assert.
  486. *
  487. * LOCKING:
  488. * Inherited from caller.
  489. */
  490. static void mv_start_dma(void __iomem *base, struct mv_port_priv *pp)
  491. {
  492. if (!(MV_PP_FLAG_EDMA_EN & pp->pp_flags)) {
  493. writelfl(EDMA_EN, base + EDMA_CMD_OFS);
  494. pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
  495. }
  496. assert(EDMA_EN & readl(base + EDMA_CMD_OFS));
  497. }
  498. /**
  499. * mv_stop_dma - Disable eDMA engine
  500. * @ap: ATA channel to manipulate
  501. *
  502. * Verify the local cache of the eDMA state is accurate with an
  503. * assert.
  504. *
  505. * LOCKING:
  506. * Inherited from caller.
  507. */
  508. static void mv_stop_dma(struct ata_port *ap)
  509. {
  510. void __iomem *port_mmio = mv_ap_base(ap);
  511. struct mv_port_priv *pp = ap->private_data;
  512. u32 reg;
  513. int i;
  514. if (MV_PP_FLAG_EDMA_EN & pp->pp_flags) {
  515. /* Disable EDMA if active. The disable bit auto clears.
  516. */
  517. writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
  518. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  519. } else {
  520. assert(!(EDMA_EN & readl(port_mmio + EDMA_CMD_OFS)));
  521. }
  522. /* now properly wait for the eDMA to stop */
  523. for (i = 1000; i > 0; i--) {
  524. reg = readl(port_mmio + EDMA_CMD_OFS);
  525. if (!(EDMA_EN & reg)) {
  526. break;
  527. }
  528. udelay(100);
  529. }
  530. if (EDMA_EN & reg) {
  531. printk(KERN_ERR "ata%u: Unable to stop eDMA\n", ap->id);
  532. /* FIXME: Consider doing a reset here to recover */
  533. }
  534. }
  535. #ifdef ATA_DEBUG
  536. static void mv_dump_mem(void __iomem *start, unsigned bytes)
  537. {
  538. int b, w;
  539. for (b = 0; b < bytes; ) {
  540. DPRINTK("%p: ", start + b);
  541. for (w = 0; b < bytes && w < 4; w++) {
  542. printk("%08x ",readl(start + b));
  543. b += sizeof(u32);
  544. }
  545. printk("\n");
  546. }
  547. }
  548. #endif
  549. static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
  550. {
  551. #ifdef ATA_DEBUG
  552. int b, w;
  553. u32 dw;
  554. for (b = 0; b < bytes; ) {
  555. DPRINTK("%02x: ", b);
  556. for (w = 0; b < bytes && w < 4; w++) {
  557. (void) pci_read_config_dword(pdev,b,&dw);
  558. printk("%08x ",dw);
  559. b += sizeof(u32);
  560. }
  561. printk("\n");
  562. }
  563. #endif
  564. }
  565. static void mv_dump_all_regs(void __iomem *mmio_base, int port,
  566. struct pci_dev *pdev)
  567. {
  568. #ifdef ATA_DEBUG
  569. void __iomem *hc_base = mv_hc_base(mmio_base,
  570. port >> MV_PORT_HC_SHIFT);
  571. void __iomem *port_base;
  572. int start_port, num_ports, p, start_hc, num_hcs, hc;
  573. if (0 > port) {
  574. start_hc = start_port = 0;
  575. num_ports = 8; /* shld be benign for 4 port devs */
  576. num_hcs = 2;
  577. } else {
  578. start_hc = port >> MV_PORT_HC_SHIFT;
  579. start_port = port;
  580. num_ports = num_hcs = 1;
  581. }
  582. DPRINTK("All registers for port(s) %u-%u:\n", start_port,
  583. num_ports > 1 ? num_ports - 1 : start_port);
  584. if (NULL != pdev) {
  585. DPRINTK("PCI config space regs:\n");
  586. mv_dump_pci_cfg(pdev, 0x68);
  587. }
  588. DPRINTK("PCI regs:\n");
  589. mv_dump_mem(mmio_base+0xc00, 0x3c);
  590. mv_dump_mem(mmio_base+0xd00, 0x34);
  591. mv_dump_mem(mmio_base+0xf00, 0x4);
  592. mv_dump_mem(mmio_base+0x1d00, 0x6c);
  593. for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
  594. hc_base = mv_hc_base(mmio_base, port >> MV_PORT_HC_SHIFT);
  595. DPRINTK("HC regs (HC %i):\n", hc);
  596. mv_dump_mem(hc_base, 0x1c);
  597. }
  598. for (p = start_port; p < start_port + num_ports; p++) {
  599. port_base = mv_port_base(mmio_base, p);
  600. DPRINTK("EDMA regs (port %i):\n",p);
  601. mv_dump_mem(port_base, 0x54);
  602. DPRINTK("SATA regs (port %i):\n",p);
  603. mv_dump_mem(port_base+0x300, 0x60);
  604. }
  605. #endif
  606. }
  607. static unsigned int mv_scr_offset(unsigned int sc_reg_in)
  608. {
  609. unsigned int ofs;
  610. switch (sc_reg_in) {
  611. case SCR_STATUS:
  612. case SCR_CONTROL:
  613. case SCR_ERROR:
  614. ofs = SATA_STATUS_OFS + (sc_reg_in * sizeof(u32));
  615. break;
  616. case SCR_ACTIVE:
  617. ofs = SATA_ACTIVE_OFS; /* active is not with the others */
  618. break;
  619. default:
  620. ofs = 0xffffffffU;
  621. break;
  622. }
  623. return ofs;
  624. }
  625. static u32 mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in)
  626. {
  627. unsigned int ofs = mv_scr_offset(sc_reg_in);
  628. if (0xffffffffU != ofs) {
  629. return readl(mv_ap_base(ap) + ofs);
  630. } else {
  631. return (u32) ofs;
  632. }
  633. }
  634. static void mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
  635. {
  636. unsigned int ofs = mv_scr_offset(sc_reg_in);
  637. if (0xffffffffU != ofs) {
  638. writelfl(val, mv_ap_base(ap) + ofs);
  639. }
  640. }
  641. /**
  642. * mv_host_stop - Host specific cleanup/stop routine.
  643. * @host_set: host data structure
  644. *
  645. * Disable ints, cleanup host memory, call general purpose
  646. * host_stop.
  647. *
  648. * LOCKING:
  649. * Inherited from caller.
  650. */
  651. static void mv_host_stop(struct ata_host_set *host_set)
  652. {
  653. struct mv_host_priv *hpriv = host_set->private_data;
  654. struct pci_dev *pdev = to_pci_dev(host_set->dev);
  655. if (hpriv->hp_flags & MV_HP_FLAG_MSI) {
  656. pci_disable_msi(pdev);
  657. } else {
  658. pci_intx(pdev, 0);
  659. }
  660. kfree(hpriv);
  661. ata_host_stop(host_set);
  662. }
  663. static inline void mv_priv_free(struct mv_port_priv *pp, struct device *dev)
  664. {
  665. dma_free_coherent(dev, MV_PORT_PRIV_DMA_SZ, pp->crpb, pp->crpb_dma);
  666. }
  667. /**
  668. * mv_port_start - Port specific init/start routine.
  669. * @ap: ATA channel to manipulate
  670. *
  671. * Allocate and point to DMA memory, init port private memory,
  672. * zero indices.
  673. *
  674. * LOCKING:
  675. * Inherited from caller.
  676. */
  677. static int mv_port_start(struct ata_port *ap)
  678. {
  679. struct device *dev = ap->host_set->dev;
  680. struct mv_port_priv *pp;
  681. void __iomem *port_mmio = mv_ap_base(ap);
  682. void *mem;
  683. dma_addr_t mem_dma;
  684. int rc = -ENOMEM;
  685. pp = kmalloc(sizeof(*pp), GFP_KERNEL);
  686. if (!pp)
  687. goto err_out;
  688. memset(pp, 0, sizeof(*pp));
  689. mem = dma_alloc_coherent(dev, MV_PORT_PRIV_DMA_SZ, &mem_dma,
  690. GFP_KERNEL);
  691. if (!mem)
  692. goto err_out_pp;
  693. memset(mem, 0, MV_PORT_PRIV_DMA_SZ);
  694. rc = ata_pad_alloc(ap, dev);
  695. if (rc)
  696. goto err_out_priv;
  697. /* First item in chunk of DMA memory:
  698. * 32-slot command request table (CRQB), 32 bytes each in size
  699. */
  700. pp->crqb = mem;
  701. pp->crqb_dma = mem_dma;
  702. mem += MV_CRQB_Q_SZ;
  703. mem_dma += MV_CRQB_Q_SZ;
  704. /* Second item:
  705. * 32-slot command response table (CRPB), 8 bytes each in size
  706. */
  707. pp->crpb = mem;
  708. pp->crpb_dma = mem_dma;
  709. mem += MV_CRPB_Q_SZ;
  710. mem_dma += MV_CRPB_Q_SZ;
  711. /* Third item:
  712. * Table of scatter-gather descriptors (ePRD), 16 bytes each
  713. */
  714. pp->sg_tbl = mem;
  715. pp->sg_tbl_dma = mem_dma;
  716. writelfl(EDMA_CFG_Q_DEPTH | EDMA_CFG_RD_BRST_EXT |
  717. EDMA_CFG_WR_BUFF_LEN, port_mmio + EDMA_CFG_OFS);
  718. writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI_OFS);
  719. writelfl(pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK,
  720. port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  721. writelfl(0, port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
  722. writelfl(0, port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
  723. writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI_OFS);
  724. writelfl(pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK,
  725. port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  726. pp->req_producer = pp->rsp_consumer = 0;
  727. /* Don't turn on EDMA here...do it before DMA commands only. Else
  728. * we'll be unable to send non-data, PIO, etc due to restricted access
  729. * to shadow regs.
  730. */
  731. ap->private_data = pp;
  732. return 0;
  733. err_out_priv:
  734. mv_priv_free(pp, dev);
  735. err_out_pp:
  736. kfree(pp);
  737. err_out:
  738. return rc;
  739. }
  740. /**
  741. * mv_port_stop - Port specific cleanup/stop routine.
  742. * @ap: ATA channel to manipulate
  743. *
  744. * Stop DMA, cleanup port memory.
  745. *
  746. * LOCKING:
  747. * This routine uses the host_set lock to protect the DMA stop.
  748. */
  749. static void mv_port_stop(struct ata_port *ap)
  750. {
  751. struct device *dev = ap->host_set->dev;
  752. struct mv_port_priv *pp = ap->private_data;
  753. unsigned long flags;
  754. spin_lock_irqsave(&ap->host_set->lock, flags);
  755. mv_stop_dma(ap);
  756. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  757. ap->private_data = NULL;
  758. ata_pad_free(ap, dev);
  759. mv_priv_free(pp, dev);
  760. kfree(pp);
  761. }
  762. /**
  763. * mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
  764. * @qc: queued command whose SG list to source from
  765. *
  766. * Populate the SG list and mark the last entry.
  767. *
  768. * LOCKING:
  769. * Inherited from caller.
  770. */
  771. static void mv_fill_sg(struct ata_queued_cmd *qc)
  772. {
  773. struct mv_port_priv *pp = qc->ap->private_data;
  774. unsigned int i = 0;
  775. struct scatterlist *sg;
  776. ata_for_each_sg(sg, qc) {
  777. dma_addr_t addr;
  778. u32 sg_len, len, offset;
  779. addr = sg_dma_address(sg);
  780. sg_len = sg_dma_len(sg);
  781. while (sg_len) {
  782. offset = addr & MV_DMA_BOUNDARY;
  783. len = sg_len;
  784. if ((offset + sg_len) > 0x10000)
  785. len = 0x10000 - offset;
  786. pp->sg_tbl[i].addr = cpu_to_le32(addr & 0xffffffff);
  787. pp->sg_tbl[i].addr_hi = cpu_to_le32((addr >> 16) >> 16);
  788. pp->sg_tbl[i].flags_size = cpu_to_le32(len);
  789. sg_len -= len;
  790. addr += len;
  791. if (!sg_len && ata_sg_is_last(sg, qc))
  792. pp->sg_tbl[i].flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
  793. i++;
  794. }
  795. }
  796. }
  797. static inline unsigned mv_inc_q_index(unsigned *index)
  798. {
  799. *index = (*index + 1) & MV_MAX_Q_DEPTH_MASK;
  800. return *index;
  801. }
  802. static inline void mv_crqb_pack_cmd(u16 *cmdw, u8 data, u8 addr, unsigned last)
  803. {
  804. *cmdw = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
  805. (last ? CRQB_CMD_LAST : 0);
  806. }
  807. /**
  808. * mv_qc_prep - Host specific command preparation.
  809. * @qc: queued command to prepare
  810. *
  811. * This routine simply redirects to the general purpose routine
  812. * if command is not DMA. Else, it handles prep of the CRQB
  813. * (command request block), does some sanity checking, and calls
  814. * the SG load routine.
  815. *
  816. * LOCKING:
  817. * Inherited from caller.
  818. */
  819. static void mv_qc_prep(struct ata_queued_cmd *qc)
  820. {
  821. struct ata_port *ap = qc->ap;
  822. struct mv_port_priv *pp = ap->private_data;
  823. u16 *cw;
  824. struct ata_taskfile *tf;
  825. u16 flags = 0;
  826. if (ATA_PROT_DMA != qc->tf.protocol) {
  827. return;
  828. }
  829. /* the req producer index should be the same as we remember it */
  830. assert(((readl(mv_ap_base(qc->ap) + EDMA_REQ_Q_IN_PTR_OFS) >>
  831. EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
  832. pp->req_producer);
  833. /* Fill in command request block
  834. */
  835. if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
  836. flags |= CRQB_FLAG_READ;
  837. }
  838. assert(MV_MAX_Q_DEPTH > qc->tag);
  839. flags |= qc->tag << CRQB_TAG_SHIFT;
  840. pp->crqb[pp->req_producer].sg_addr =
  841. cpu_to_le32(pp->sg_tbl_dma & 0xffffffff);
  842. pp->crqb[pp->req_producer].sg_addr_hi =
  843. cpu_to_le32((pp->sg_tbl_dma >> 16) >> 16);
  844. pp->crqb[pp->req_producer].ctrl_flags = cpu_to_le16(flags);
  845. cw = &pp->crqb[pp->req_producer].ata_cmd[0];
  846. tf = &qc->tf;
  847. /* Sadly, the CRQB cannot accomodate all registers--there are
  848. * only 11 bytes...so we must pick and choose required
  849. * registers based on the command. So, we drop feature and
  850. * hob_feature for [RW] DMA commands, but they are needed for
  851. * NCQ. NCQ will drop hob_nsect.
  852. */
  853. switch (tf->command) {
  854. case ATA_CMD_READ:
  855. case ATA_CMD_READ_EXT:
  856. case ATA_CMD_WRITE:
  857. case ATA_CMD_WRITE_EXT:
  858. mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
  859. break;
  860. #ifdef LIBATA_NCQ /* FIXME: remove this line when NCQ added */
  861. case ATA_CMD_FPDMA_READ:
  862. case ATA_CMD_FPDMA_WRITE:
  863. mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
  864. mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
  865. break;
  866. #endif /* FIXME: remove this line when NCQ added */
  867. default:
  868. /* The only other commands EDMA supports in non-queued and
  869. * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
  870. * of which are defined/used by Linux. If we get here, this
  871. * driver needs work.
  872. *
  873. * FIXME: modify libata to give qc_prep a return value and
  874. * return error here.
  875. */
  876. BUG_ON(tf->command);
  877. break;
  878. }
  879. mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
  880. mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
  881. mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
  882. mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
  883. mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
  884. mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
  885. mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
  886. mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
  887. mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1); /* last */
  888. if (!(qc->flags & ATA_QCFLAG_DMAMAP)) {
  889. return;
  890. }
  891. mv_fill_sg(qc);
  892. }
  893. /**
  894. * mv_qc_issue - Initiate a command to the host
  895. * @qc: queued command to start
  896. *
  897. * This routine simply redirects to the general purpose routine
  898. * if command is not DMA. Else, it sanity checks our local
  899. * caches of the request producer/consumer indices then enables
  900. * DMA and bumps the request producer index.
  901. *
  902. * LOCKING:
  903. * Inherited from caller.
  904. */
  905. static unsigned int mv_qc_issue(struct ata_queued_cmd *qc)
  906. {
  907. void __iomem *port_mmio = mv_ap_base(qc->ap);
  908. struct mv_port_priv *pp = qc->ap->private_data;
  909. u32 in_ptr;
  910. if (ATA_PROT_DMA != qc->tf.protocol) {
  911. /* We're about to send a non-EDMA capable command to the
  912. * port. Turn off EDMA so there won't be problems accessing
  913. * shadow block, etc registers.
  914. */
  915. mv_stop_dma(qc->ap);
  916. return ata_qc_issue_prot(qc);
  917. }
  918. in_ptr = readl(port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  919. /* the req producer index should be the same as we remember it */
  920. assert(((in_ptr >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
  921. pp->req_producer);
  922. /* until we do queuing, the queue should be empty at this point */
  923. assert(((in_ptr >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
  924. ((readl(port_mmio + EDMA_REQ_Q_OUT_PTR_OFS) >>
  925. EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK));
  926. mv_inc_q_index(&pp->req_producer); /* now incr producer index */
  927. mv_start_dma(port_mmio, pp);
  928. /* and write the request in pointer to kick the EDMA to life */
  929. in_ptr &= EDMA_REQ_Q_BASE_LO_MASK;
  930. in_ptr |= pp->req_producer << EDMA_REQ_Q_PTR_SHIFT;
  931. writelfl(in_ptr, port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  932. return 0;
  933. }
  934. /**
  935. * mv_get_crpb_status - get status from most recently completed cmd
  936. * @ap: ATA channel to manipulate
  937. *
  938. * This routine is for use when the port is in DMA mode, when it
  939. * will be using the CRPB (command response block) method of
  940. * returning command completion information. We assert indices
  941. * are good, grab status, and bump the response consumer index to
  942. * prove that we're up to date.
  943. *
  944. * LOCKING:
  945. * Inherited from caller.
  946. */
  947. static u8 mv_get_crpb_status(struct ata_port *ap)
  948. {
  949. void __iomem *port_mmio = mv_ap_base(ap);
  950. struct mv_port_priv *pp = ap->private_data;
  951. u32 out_ptr;
  952. out_ptr = readl(port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  953. /* the response consumer index should be the same as we remember it */
  954. assert(((out_ptr >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
  955. pp->rsp_consumer);
  956. /* increment our consumer index... */
  957. pp->rsp_consumer = mv_inc_q_index(&pp->rsp_consumer);
  958. /* and, until we do NCQ, there should only be 1 CRPB waiting */
  959. assert(((readl(port_mmio + EDMA_RSP_Q_IN_PTR_OFS) >>
  960. EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
  961. pp->rsp_consumer);
  962. /* write out our inc'd consumer index so EDMA knows we're caught up */
  963. out_ptr &= EDMA_RSP_Q_BASE_LO_MASK;
  964. out_ptr |= pp->rsp_consumer << EDMA_RSP_Q_PTR_SHIFT;
  965. writelfl(out_ptr, port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  966. /* Return ATA status register for completed CRPB */
  967. return (pp->crpb[pp->rsp_consumer].flags >> CRPB_FLAG_STATUS_SHIFT);
  968. }
  969. /**
  970. * mv_err_intr - Handle error interrupts on the port
  971. * @ap: ATA channel to manipulate
  972. *
  973. * In most cases, just clear the interrupt and move on. However,
  974. * some cases require an eDMA reset, which is done right before
  975. * the COMRESET in mv_phy_reset(). The SERR case requires a
  976. * clear of pending errors in the SATA SERROR register. Finally,
  977. * if the port disabled DMA, update our cached copy to match.
  978. *
  979. * LOCKING:
  980. * Inherited from caller.
  981. */
  982. static void mv_err_intr(struct ata_port *ap)
  983. {
  984. void __iomem *port_mmio = mv_ap_base(ap);
  985. u32 edma_err_cause, serr = 0;
  986. edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  987. if (EDMA_ERR_SERR & edma_err_cause) {
  988. serr = scr_read(ap, SCR_ERROR);
  989. scr_write_flush(ap, SCR_ERROR, serr);
  990. }
  991. if (EDMA_ERR_SELF_DIS & edma_err_cause) {
  992. struct mv_port_priv *pp = ap->private_data;
  993. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  994. }
  995. DPRINTK(KERN_ERR "ata%u: port error; EDMA err cause: 0x%08x "
  996. "SERR: 0x%08x\n", ap->id, edma_err_cause, serr);
  997. /* Clear EDMA now that SERR cleanup done */
  998. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  999. /* check for fatal here and recover if needed */
  1000. if (EDMA_ERR_FATAL & edma_err_cause) {
  1001. mv_stop_and_reset(ap);
  1002. }
  1003. }
  1004. /**
  1005. * mv_host_intr - Handle all interrupts on the given host controller
  1006. * @host_set: host specific structure
  1007. * @relevant: port error bits relevant to this host controller
  1008. * @hc: which host controller we're to look at
  1009. *
  1010. * Read then write clear the HC interrupt status then walk each
  1011. * port connected to the HC and see if it needs servicing. Port
  1012. * success ints are reported in the HC interrupt status reg, the
  1013. * port error ints are reported in the higher level main
  1014. * interrupt status register and thus are passed in via the
  1015. * 'relevant' argument.
  1016. *
  1017. * LOCKING:
  1018. * Inherited from caller.
  1019. */
  1020. static void mv_host_intr(struct ata_host_set *host_set, u32 relevant,
  1021. unsigned int hc)
  1022. {
  1023. void __iomem *mmio = host_set->mmio_base;
  1024. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  1025. struct ata_port *ap;
  1026. struct ata_queued_cmd *qc;
  1027. u32 hc_irq_cause;
  1028. int shift, port, port0, hard_port, handled;
  1029. unsigned int err_mask;
  1030. u8 ata_status = 0;
  1031. if (hc == 0) {
  1032. port0 = 0;
  1033. } else {
  1034. port0 = MV_PORTS_PER_HC;
  1035. }
  1036. /* we'll need the HC success int register in most cases */
  1037. hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
  1038. if (hc_irq_cause) {
  1039. writelfl(~hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
  1040. }
  1041. VPRINTK("ENTER, hc%u relevant=0x%08x HC IRQ cause=0x%08x\n",
  1042. hc,relevant,hc_irq_cause);
  1043. for (port = port0; port < port0 + MV_PORTS_PER_HC; port++) {
  1044. ap = host_set->ports[port];
  1045. hard_port = port & MV_PORT_MASK; /* range 0-3 */
  1046. handled = 0; /* ensure ata_status is set if handled++ */
  1047. if ((CRPB_DMA_DONE << hard_port) & hc_irq_cause) {
  1048. /* new CRPB on the queue; just one at a time until NCQ
  1049. */
  1050. ata_status = mv_get_crpb_status(ap);
  1051. handled++;
  1052. } else if ((DEV_IRQ << hard_port) & hc_irq_cause) {
  1053. /* received ATA IRQ; read the status reg to clear INTRQ
  1054. */
  1055. ata_status = readb((void __iomem *)
  1056. ap->ioaddr.status_addr);
  1057. handled++;
  1058. }
  1059. if (ap &&
  1060. (ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR)))
  1061. continue;
  1062. err_mask = ac_err_mask(ata_status);
  1063. shift = port << 1; /* (port * 2) */
  1064. if (port >= MV_PORTS_PER_HC) {
  1065. shift++; /* skip bit 8 in the HC Main IRQ reg */
  1066. }
  1067. if ((PORT0_ERR << shift) & relevant) {
  1068. mv_err_intr(ap);
  1069. err_mask |= AC_ERR_OTHER;
  1070. handled++;
  1071. }
  1072. if (handled && ap) {
  1073. qc = ata_qc_from_tag(ap, ap->active_tag);
  1074. if (NULL != qc) {
  1075. VPRINTK("port %u IRQ found for qc, "
  1076. "ata_status 0x%x\n", port,ata_status);
  1077. /* mark qc status appropriately */
  1078. if (!(qc->tf.ctl & ATA_NIEN)) {
  1079. qc->err_mask |= err_mask;
  1080. ata_qc_complete(qc);
  1081. }
  1082. }
  1083. }
  1084. }
  1085. VPRINTK("EXIT\n");
  1086. }
  1087. /**
  1088. * mv_interrupt -
  1089. * @irq: unused
  1090. * @dev_instance: private data; in this case the host structure
  1091. * @regs: unused
  1092. *
  1093. * Read the read only register to determine if any host
  1094. * controllers have pending interrupts. If so, call lower level
  1095. * routine to handle. Also check for PCI errors which are only
  1096. * reported here.
  1097. *
  1098. * LOCKING:
  1099. * This routine holds the host_set lock while processing pending
  1100. * interrupts.
  1101. */
  1102. static irqreturn_t mv_interrupt(int irq, void *dev_instance,
  1103. struct pt_regs *regs)
  1104. {
  1105. struct ata_host_set *host_set = dev_instance;
  1106. unsigned int hc, handled = 0, n_hcs;
  1107. void __iomem *mmio = host_set->mmio_base;
  1108. u32 irq_stat;
  1109. irq_stat = readl(mmio + HC_MAIN_IRQ_CAUSE_OFS);
  1110. /* check the cases where we either have nothing pending or have read
  1111. * a bogus register value which can indicate HW removal or PCI fault
  1112. */
  1113. if (!irq_stat || (0xffffffffU == irq_stat)) {
  1114. return IRQ_NONE;
  1115. }
  1116. n_hcs = mv_get_hc_count(host_set->ports[0]->flags);
  1117. spin_lock(&host_set->lock);
  1118. for (hc = 0; hc < n_hcs; hc++) {
  1119. u32 relevant = irq_stat & (HC0_IRQ_PEND << (hc * HC_SHIFT));
  1120. if (relevant) {
  1121. mv_host_intr(host_set, relevant, hc);
  1122. handled++;
  1123. }
  1124. }
  1125. if (PCI_ERR & irq_stat) {
  1126. printk(KERN_ERR DRV_NAME ": PCI ERROR; PCI IRQ cause=0x%08x\n",
  1127. readl(mmio + PCI_IRQ_CAUSE_OFS));
  1128. DPRINTK("All regs @ PCI error\n");
  1129. mv_dump_all_regs(mmio, -1, to_pci_dev(host_set->dev));
  1130. writelfl(0, mmio + PCI_IRQ_CAUSE_OFS);
  1131. handled++;
  1132. }
  1133. spin_unlock(&host_set->lock);
  1134. return IRQ_RETVAL(handled);
  1135. }
  1136. static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
  1137. {
  1138. void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
  1139. unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
  1140. return hc_mmio + ofs;
  1141. }
  1142. static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
  1143. {
  1144. unsigned int ofs;
  1145. switch (sc_reg_in) {
  1146. case SCR_STATUS:
  1147. case SCR_ERROR:
  1148. case SCR_CONTROL:
  1149. ofs = sc_reg_in * sizeof(u32);
  1150. break;
  1151. default:
  1152. ofs = 0xffffffffU;
  1153. break;
  1154. }
  1155. return ofs;
  1156. }
  1157. static u32 mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in)
  1158. {
  1159. void __iomem *mmio = mv5_phy_base(ap->host_set->mmio_base, ap->port_no);
  1160. unsigned int ofs = mv5_scr_offset(sc_reg_in);
  1161. if (ofs != 0xffffffffU)
  1162. return readl(mmio + ofs);
  1163. else
  1164. return (u32) ofs;
  1165. }
  1166. static void mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
  1167. {
  1168. void __iomem *mmio = mv5_phy_base(ap->host_set->mmio_base, ap->port_no);
  1169. unsigned int ofs = mv5_scr_offset(sc_reg_in);
  1170. if (ofs != 0xffffffffU)
  1171. writelfl(val, mmio + ofs);
  1172. }
  1173. static void mv5_reset_bus(struct pci_dev *pdev, void __iomem *mmio)
  1174. {
  1175. u8 rev_id;
  1176. int early_5080;
  1177. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  1178. early_5080 = (pdev->device == 0x5080) && (rev_id == 0);
  1179. if (!early_5080) {
  1180. u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1181. tmp |= (1 << 0);
  1182. writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1183. }
  1184. mv_reset_pci_bus(pdev, mmio);
  1185. }
  1186. static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
  1187. {
  1188. writel(0x0fcfffff, mmio + MV_FLASH_CTL);
  1189. }
  1190. static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
  1191. void __iomem *mmio)
  1192. {
  1193. void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
  1194. u32 tmp;
  1195. tmp = readl(phy_mmio + MV5_PHY_MODE);
  1196. hpriv->signal[idx].pre = tmp & 0x1800; /* bits 12:11 */
  1197. hpriv->signal[idx].amps = tmp & 0xe0; /* bits 7:5 */
  1198. }
  1199. static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
  1200. {
  1201. u32 tmp;
  1202. writel(0, mmio + MV_GPIO_PORT_CTL);
  1203. /* FIXME: handle MV_HP_ERRATA_50XXB2 errata */
  1204. tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1205. tmp |= ~(1 << 0);
  1206. writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1207. }
  1208. static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  1209. unsigned int port)
  1210. {
  1211. void __iomem *phy_mmio = mv5_phy_base(mmio, port);
  1212. const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
  1213. u32 tmp;
  1214. int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
  1215. if (fix_apm_sq) {
  1216. tmp = readl(phy_mmio + MV5_LT_MODE);
  1217. tmp |= (1 << 19);
  1218. writel(tmp, phy_mmio + MV5_LT_MODE);
  1219. tmp = readl(phy_mmio + MV5_PHY_CTL);
  1220. tmp &= ~0x3;
  1221. tmp |= 0x1;
  1222. writel(tmp, phy_mmio + MV5_PHY_CTL);
  1223. }
  1224. tmp = readl(phy_mmio + MV5_PHY_MODE);
  1225. tmp &= ~mask;
  1226. tmp |= hpriv->signal[port].pre;
  1227. tmp |= hpriv->signal[port].amps;
  1228. writel(tmp, phy_mmio + MV5_PHY_MODE);
  1229. }
  1230. #undef ZERO
  1231. #define ZERO(reg) writel(0, port_mmio + (reg))
  1232. static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
  1233. unsigned int port)
  1234. {
  1235. void __iomem *port_mmio = mv_port_base(mmio, port);
  1236. writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
  1237. mv_channel_reset(hpriv, mmio, port);
  1238. ZERO(0x028); /* command */
  1239. writel(0x11f, port_mmio + EDMA_CFG_OFS);
  1240. ZERO(0x004); /* timer */
  1241. ZERO(0x008); /* irq err cause */
  1242. ZERO(0x00c); /* irq err mask */
  1243. ZERO(0x010); /* rq bah */
  1244. ZERO(0x014); /* rq inp */
  1245. ZERO(0x018); /* rq outp */
  1246. ZERO(0x01c); /* respq bah */
  1247. ZERO(0x024); /* respq outp */
  1248. ZERO(0x020); /* respq inp */
  1249. ZERO(0x02c); /* test control */
  1250. writel(0xbc, port_mmio + EDMA_IORDY_TMOUT);
  1251. }
  1252. #undef ZERO
  1253. #define ZERO(reg) writel(0, hc_mmio + (reg))
  1254. static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  1255. unsigned int hc)
  1256. {
  1257. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  1258. u32 tmp;
  1259. ZERO(0x00c);
  1260. ZERO(0x010);
  1261. ZERO(0x014);
  1262. ZERO(0x018);
  1263. tmp = readl(hc_mmio + 0x20);
  1264. tmp &= 0x1c1c1c1c;
  1265. tmp |= 0x03030303;
  1266. writel(tmp, hc_mmio + 0x20);
  1267. }
  1268. #undef ZERO
  1269. static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  1270. unsigned int n_hc)
  1271. {
  1272. unsigned int hc, port;
  1273. for (hc = 0; hc < n_hc; hc++) {
  1274. for (port = 0; port < MV_PORTS_PER_HC; port++)
  1275. mv5_reset_hc_port(hpriv, mmio,
  1276. (hc * MV_PORTS_PER_HC) + port);
  1277. mv5_reset_one_hc(hpriv, mmio, hc);
  1278. }
  1279. return 0;
  1280. }
  1281. #undef ZERO
  1282. #define ZERO(reg) writel(0, mmio + (reg))
  1283. static void mv_reset_pci_bus(struct pci_dev *pdev, void __iomem *mmio)
  1284. {
  1285. u32 tmp;
  1286. tmp = readl(mmio + MV_PCI_MODE);
  1287. tmp &= 0xff00ffff;
  1288. writel(tmp, mmio + MV_PCI_MODE);
  1289. ZERO(MV_PCI_DISC_TIMER);
  1290. ZERO(MV_PCI_MSI_TRIGGER);
  1291. writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT);
  1292. ZERO(HC_MAIN_IRQ_MASK_OFS);
  1293. ZERO(MV_PCI_SERR_MASK);
  1294. ZERO(PCI_IRQ_CAUSE_OFS);
  1295. ZERO(PCI_IRQ_MASK_OFS);
  1296. ZERO(MV_PCI_ERR_LOW_ADDRESS);
  1297. ZERO(MV_PCI_ERR_HIGH_ADDRESS);
  1298. ZERO(MV_PCI_ERR_ATTRIBUTE);
  1299. ZERO(MV_PCI_ERR_COMMAND);
  1300. }
  1301. #undef ZERO
  1302. static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
  1303. {
  1304. u32 tmp;
  1305. mv5_reset_flash(hpriv, mmio);
  1306. tmp = readl(mmio + MV_GPIO_PORT_CTL);
  1307. tmp &= 0x3;
  1308. tmp |= (1 << 5) | (1 << 6);
  1309. writel(tmp, mmio + MV_GPIO_PORT_CTL);
  1310. }
  1311. /**
  1312. * mv6_reset_hc - Perform the 6xxx global soft reset
  1313. * @mmio: base address of the HBA
  1314. *
  1315. * This routine only applies to 6xxx parts.
  1316. *
  1317. * LOCKING:
  1318. * Inherited from caller.
  1319. */
  1320. static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  1321. unsigned int n_hc)
  1322. {
  1323. void __iomem *reg = mmio + PCI_MAIN_CMD_STS_OFS;
  1324. int i, rc = 0;
  1325. u32 t;
  1326. /* Following procedure defined in PCI "main command and status
  1327. * register" table.
  1328. */
  1329. t = readl(reg);
  1330. writel(t | STOP_PCI_MASTER, reg);
  1331. for (i = 0; i < 1000; i++) {
  1332. udelay(1);
  1333. t = readl(reg);
  1334. if (PCI_MASTER_EMPTY & t) {
  1335. break;
  1336. }
  1337. }
  1338. if (!(PCI_MASTER_EMPTY & t)) {
  1339. printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
  1340. rc = 1;
  1341. goto done;
  1342. }
  1343. /* set reset */
  1344. i = 5;
  1345. do {
  1346. writel(t | GLOB_SFT_RST, reg);
  1347. t = readl(reg);
  1348. udelay(1);
  1349. } while (!(GLOB_SFT_RST & t) && (i-- > 0));
  1350. if (!(GLOB_SFT_RST & t)) {
  1351. printk(KERN_ERR DRV_NAME ": can't set global reset\n");
  1352. rc = 1;
  1353. goto done;
  1354. }
  1355. /* clear reset and *reenable the PCI master* (not mentioned in spec) */
  1356. i = 5;
  1357. do {
  1358. writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
  1359. t = readl(reg);
  1360. udelay(1);
  1361. } while ((GLOB_SFT_RST & t) && (i-- > 0));
  1362. if (GLOB_SFT_RST & t) {
  1363. printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
  1364. rc = 1;
  1365. }
  1366. done:
  1367. return rc;
  1368. }
  1369. static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
  1370. void __iomem *mmio)
  1371. {
  1372. void __iomem *port_mmio;
  1373. u32 tmp;
  1374. tmp = readl(mmio + MV_RESET_CFG);
  1375. if ((tmp & (1 << 0)) == 0) {
  1376. hpriv->signal[idx].amps = 0x7 << 8;
  1377. hpriv->signal[idx].pre = 0x1 << 5;
  1378. return;
  1379. }
  1380. port_mmio = mv_port_base(mmio, idx);
  1381. tmp = readl(port_mmio + PHY_MODE2);
  1382. hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
  1383. hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
  1384. }
  1385. static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
  1386. {
  1387. writel(0x00000060, mmio + MV_GPIO_PORT_CTL);
  1388. }
  1389. static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  1390. unsigned int port)
  1391. {
  1392. void __iomem *port_mmio = mv_port_base(mmio, port);
  1393. u32 hp_flags = hpriv->hp_flags;
  1394. int fix_phy_mode2 =
  1395. hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
  1396. int fix_phy_mode4 =
  1397. hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
  1398. u32 m2, tmp;
  1399. if (fix_phy_mode2) {
  1400. m2 = readl(port_mmio + PHY_MODE2);
  1401. m2 &= ~(1 << 16);
  1402. m2 |= (1 << 31);
  1403. writel(m2, port_mmio + PHY_MODE2);
  1404. udelay(200);
  1405. m2 = readl(port_mmio + PHY_MODE2);
  1406. m2 &= ~((1 << 16) | (1 << 31));
  1407. writel(m2, port_mmio + PHY_MODE2);
  1408. udelay(200);
  1409. }
  1410. /* who knows what this magic does */
  1411. tmp = readl(port_mmio + PHY_MODE3);
  1412. tmp &= ~0x7F800000;
  1413. tmp |= 0x2A800000;
  1414. writel(tmp, port_mmio + PHY_MODE3);
  1415. if (fix_phy_mode4) {
  1416. u32 m4;
  1417. m4 = readl(port_mmio + PHY_MODE4);
  1418. if (hp_flags & MV_HP_ERRATA_60X1B2)
  1419. tmp = readl(port_mmio + 0x310);
  1420. m4 = (m4 & ~(1 << 1)) | (1 << 0);
  1421. writel(m4, port_mmio + PHY_MODE4);
  1422. if (hp_flags & MV_HP_ERRATA_60X1B2)
  1423. writel(tmp, port_mmio + 0x310);
  1424. }
  1425. /* Revert values of pre-emphasis and signal amps to the saved ones */
  1426. m2 = readl(port_mmio + PHY_MODE2);
  1427. m2 &= ~MV_M2_PREAMP_MASK;
  1428. m2 |= hpriv->signal[port].amps;
  1429. m2 |= hpriv->signal[port].pre;
  1430. m2 &= ~(1 << 16);
  1431. writel(m2, port_mmio + PHY_MODE2);
  1432. }
  1433. static void mv_channel_reset(struct mv_host_priv *hpriv, void __iomem *mmio,
  1434. unsigned int port_no)
  1435. {
  1436. void __iomem *port_mmio = mv_port_base(mmio, port_no);
  1437. writelfl(ATA_RST, port_mmio + EDMA_CMD_OFS);
  1438. if (IS_60XX(hpriv)) {
  1439. u32 ifctl = readl(port_mmio + SATA_INTERFACE_CTL);
  1440. ifctl |= (1 << 12) | (1 << 7);
  1441. writelfl(ifctl, port_mmio + SATA_INTERFACE_CTL);
  1442. }
  1443. udelay(25); /* allow reset propagation */
  1444. /* Spec never mentions clearing the bit. Marvell's driver does
  1445. * clear the bit, however.
  1446. */
  1447. writelfl(0, port_mmio + EDMA_CMD_OFS);
  1448. hpriv->ops->phy_errata(hpriv, mmio, port_no);
  1449. if (IS_50XX(hpriv))
  1450. mdelay(1);
  1451. }
  1452. static void mv_stop_and_reset(struct ata_port *ap)
  1453. {
  1454. struct mv_host_priv *hpriv = ap->host_set->private_data;
  1455. void __iomem *mmio = ap->host_set->mmio_base;
  1456. mv_stop_dma(ap);
  1457. mv_channel_reset(hpriv, mmio, ap->port_no);
  1458. __mv_phy_reset(ap, 0);
  1459. }
  1460. static inline void __msleep(unsigned int msec, int can_sleep)
  1461. {
  1462. if (can_sleep)
  1463. msleep(msec);
  1464. else
  1465. mdelay(msec);
  1466. }
  1467. /**
  1468. * __mv_phy_reset - Perform eDMA reset followed by COMRESET
  1469. * @ap: ATA channel to manipulate
  1470. *
  1471. * Part of this is taken from __sata_phy_reset and modified to
  1472. * not sleep since this routine gets called from interrupt level.
  1473. *
  1474. * LOCKING:
  1475. * Inherited from caller. This is coded to safe to call at
  1476. * interrupt level, i.e. it does not sleep.
  1477. */
  1478. static void __mv_phy_reset(struct ata_port *ap, int can_sleep)
  1479. {
  1480. struct mv_port_priv *pp = ap->private_data;
  1481. struct mv_host_priv *hpriv = ap->host_set->private_data;
  1482. void __iomem *port_mmio = mv_ap_base(ap);
  1483. struct ata_taskfile tf;
  1484. struct ata_device *dev = &ap->device[0];
  1485. unsigned long timeout;
  1486. int retry = 5;
  1487. u32 sstatus;
  1488. VPRINTK("ENTER, port %u, mmio 0x%p\n", ap->port_no, port_mmio);
  1489. DPRINTK("S-regs after ATA_RST: SStat 0x%08x SErr 0x%08x "
  1490. "SCtrl 0x%08x\n", mv_scr_read(ap, SCR_STATUS),
  1491. mv_scr_read(ap, SCR_ERROR), mv_scr_read(ap, SCR_CONTROL));
  1492. /* Issue COMRESET via SControl */
  1493. comreset_retry:
  1494. scr_write_flush(ap, SCR_CONTROL, 0x301);
  1495. __msleep(1, can_sleep);
  1496. scr_write_flush(ap, SCR_CONTROL, 0x300);
  1497. __msleep(20, can_sleep);
  1498. timeout = jiffies + msecs_to_jiffies(200);
  1499. do {
  1500. sstatus = scr_read(ap, SCR_STATUS) & 0x3;
  1501. if ((sstatus == 3) || (sstatus == 0))
  1502. break;
  1503. __msleep(1, can_sleep);
  1504. } while (time_before(jiffies, timeout));
  1505. /* work around errata */
  1506. if (IS_60XX(hpriv) &&
  1507. (sstatus != 0x0) && (sstatus != 0x113) && (sstatus != 0x123) &&
  1508. (retry-- > 0))
  1509. goto comreset_retry;
  1510. DPRINTK("S-regs after PHY wake: SStat 0x%08x SErr 0x%08x "
  1511. "SCtrl 0x%08x\n", mv_scr_read(ap, SCR_STATUS),
  1512. mv_scr_read(ap, SCR_ERROR), mv_scr_read(ap, SCR_CONTROL));
  1513. if (sata_dev_present(ap)) {
  1514. ata_port_probe(ap);
  1515. } else {
  1516. printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
  1517. ap->id, scr_read(ap, SCR_STATUS));
  1518. ata_port_disable(ap);
  1519. return;
  1520. }
  1521. ap->cbl = ATA_CBL_SATA;
  1522. /* even after SStatus reflects that device is ready,
  1523. * it seems to take a while for link to be fully
  1524. * established (and thus Status no longer 0x80/0x7F),
  1525. * so we poll a bit for that, here.
  1526. */
  1527. retry = 20;
  1528. while (1) {
  1529. u8 drv_stat = ata_check_status(ap);
  1530. if ((drv_stat != 0x80) && (drv_stat != 0x7f))
  1531. break;
  1532. __msleep(500, can_sleep);
  1533. if (retry-- <= 0)
  1534. break;
  1535. }
  1536. tf.lbah = readb((void __iomem *) ap->ioaddr.lbah_addr);
  1537. tf.lbam = readb((void __iomem *) ap->ioaddr.lbam_addr);
  1538. tf.lbal = readb((void __iomem *) ap->ioaddr.lbal_addr);
  1539. tf.nsect = readb((void __iomem *) ap->ioaddr.nsect_addr);
  1540. dev->class = ata_dev_classify(&tf);
  1541. if (!ata_dev_present(dev)) {
  1542. VPRINTK("Port disabled post-sig: No device present.\n");
  1543. ata_port_disable(ap);
  1544. }
  1545. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1546. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  1547. VPRINTK("EXIT\n");
  1548. }
  1549. static void mv_phy_reset(struct ata_port *ap)
  1550. {
  1551. __mv_phy_reset(ap, 1);
  1552. }
  1553. /**
  1554. * mv_eng_timeout - Routine called by libata when SCSI times out I/O
  1555. * @ap: ATA channel to manipulate
  1556. *
  1557. * Intent is to clear all pending error conditions, reset the
  1558. * chip/bus, fail the command, and move on.
  1559. *
  1560. * LOCKING:
  1561. * This routine holds the host_set lock while failing the command.
  1562. */
  1563. static void mv_eng_timeout(struct ata_port *ap)
  1564. {
  1565. struct ata_queued_cmd *qc;
  1566. printk(KERN_ERR "ata%u: Entering mv_eng_timeout\n",ap->id);
  1567. DPRINTK("All regs @ start of eng_timeout\n");
  1568. mv_dump_all_regs(ap->host_set->mmio_base, ap->port_no,
  1569. to_pci_dev(ap->host_set->dev));
  1570. qc = ata_qc_from_tag(ap, ap->active_tag);
  1571. printk(KERN_ERR "mmio_base %p ap %p qc %p scsi_cmnd %p &cmnd %p\n",
  1572. ap->host_set->mmio_base, ap, qc, qc->scsicmd,
  1573. &qc->scsicmd->cmnd);
  1574. mv_err_intr(ap);
  1575. mv_stop_and_reset(ap);
  1576. if (!qc) {
  1577. printk(KERN_ERR "ata%u: BUG: timeout without command\n",
  1578. ap->id);
  1579. } else {
  1580. qc->err_mask |= AC_ERR_TIMEOUT;
  1581. ata_eh_qc_complete(qc);
  1582. }
  1583. }
  1584. /**
  1585. * mv_port_init - Perform some early initialization on a single port.
  1586. * @port: libata data structure storing shadow register addresses
  1587. * @port_mmio: base address of the port
  1588. *
  1589. * Initialize shadow register mmio addresses, clear outstanding
  1590. * interrupts on the port, and unmask interrupts for the future
  1591. * start of the port.
  1592. *
  1593. * LOCKING:
  1594. * Inherited from caller.
  1595. */
  1596. static void mv_port_init(struct ata_ioports *port, void __iomem *port_mmio)
  1597. {
  1598. unsigned long shd_base = (unsigned long) port_mmio + SHD_BLK_OFS;
  1599. unsigned serr_ofs;
  1600. /* PIO related setup
  1601. */
  1602. port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
  1603. port->error_addr =
  1604. port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
  1605. port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
  1606. port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
  1607. port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
  1608. port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
  1609. port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
  1610. port->status_addr =
  1611. port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
  1612. /* special case: control/altstatus doesn't have ATA_REG_ address */
  1613. port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST_OFS;
  1614. /* unused: */
  1615. port->cmd_addr = port->bmdma_addr = port->scr_addr = 0;
  1616. /* Clear any currently outstanding port interrupt conditions */
  1617. serr_ofs = mv_scr_offset(SCR_ERROR);
  1618. writelfl(readl(port_mmio + serr_ofs), port_mmio + serr_ofs);
  1619. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1620. /* unmask all EDMA error interrupts */
  1621. writelfl(~0, port_mmio + EDMA_ERR_IRQ_MASK_OFS);
  1622. VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
  1623. readl(port_mmio + EDMA_CFG_OFS),
  1624. readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS),
  1625. readl(port_mmio + EDMA_ERR_IRQ_MASK_OFS));
  1626. }
  1627. static int mv_chip_id(struct pci_dev *pdev, struct mv_host_priv *hpriv,
  1628. unsigned int board_idx)
  1629. {
  1630. u8 rev_id;
  1631. u32 hp_flags = hpriv->hp_flags;
  1632. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  1633. switch(board_idx) {
  1634. case chip_5080:
  1635. hpriv->ops = &mv5xxx_ops;
  1636. hp_flags |= MV_HP_50XX;
  1637. switch (rev_id) {
  1638. case 0x1:
  1639. hp_flags |= MV_HP_ERRATA_50XXB0;
  1640. break;
  1641. case 0x3:
  1642. hp_flags |= MV_HP_ERRATA_50XXB2;
  1643. break;
  1644. default:
  1645. dev_printk(KERN_WARNING, &pdev->dev,
  1646. "Applying 50XXB2 workarounds to unknown rev\n");
  1647. hp_flags |= MV_HP_ERRATA_50XXB2;
  1648. break;
  1649. }
  1650. break;
  1651. case chip_504x:
  1652. case chip_508x:
  1653. hpriv->ops = &mv5xxx_ops;
  1654. hp_flags |= MV_HP_50XX;
  1655. switch (rev_id) {
  1656. case 0x0:
  1657. hp_flags |= MV_HP_ERRATA_50XXB0;
  1658. break;
  1659. case 0x3:
  1660. hp_flags |= MV_HP_ERRATA_50XXB2;
  1661. break;
  1662. default:
  1663. dev_printk(KERN_WARNING, &pdev->dev,
  1664. "Applying B2 workarounds to unknown rev\n");
  1665. hp_flags |= MV_HP_ERRATA_50XXB2;
  1666. break;
  1667. }
  1668. break;
  1669. case chip_604x:
  1670. case chip_608x:
  1671. hpriv->ops = &mv6xxx_ops;
  1672. switch (rev_id) {
  1673. case 0x7:
  1674. hp_flags |= MV_HP_ERRATA_60X1B2;
  1675. break;
  1676. case 0x9:
  1677. hp_flags |= MV_HP_ERRATA_60X1C0;
  1678. break;
  1679. default:
  1680. dev_printk(KERN_WARNING, &pdev->dev,
  1681. "Applying B2 workarounds to unknown rev\n");
  1682. hp_flags |= MV_HP_ERRATA_60X1B2;
  1683. break;
  1684. }
  1685. break;
  1686. default:
  1687. printk(KERN_ERR DRV_NAME ": BUG: invalid board index %u\n", board_idx);
  1688. return 1;
  1689. }
  1690. hpriv->hp_flags = hp_flags;
  1691. return 0;
  1692. }
  1693. /**
  1694. * mv_init_host - Perform some early initialization of the host.
  1695. * @pdev: host PCI device
  1696. * @probe_ent: early data struct representing the host
  1697. *
  1698. * If possible, do an early global reset of the host. Then do
  1699. * our port init and clear/unmask all/relevant host interrupts.
  1700. *
  1701. * LOCKING:
  1702. * Inherited from caller.
  1703. */
  1704. static int mv_init_host(struct pci_dev *pdev, struct ata_probe_ent *probe_ent,
  1705. unsigned int board_idx)
  1706. {
  1707. int rc = 0, n_hc, port, hc;
  1708. void __iomem *mmio = probe_ent->mmio_base;
  1709. struct mv_host_priv *hpriv = probe_ent->private_data;
  1710. /* global interrupt mask */
  1711. writel(0, mmio + HC_MAIN_IRQ_MASK_OFS);
  1712. rc = mv_chip_id(pdev, hpriv, board_idx);
  1713. if (rc)
  1714. goto done;
  1715. n_hc = mv_get_hc_count(probe_ent->host_flags);
  1716. probe_ent->n_ports = MV_PORTS_PER_HC * n_hc;
  1717. for (port = 0; port < probe_ent->n_ports; port++)
  1718. hpriv->ops->read_preamp(hpriv, port, mmio);
  1719. rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc);
  1720. if (rc)
  1721. goto done;
  1722. hpriv->ops->reset_flash(hpriv, mmio);
  1723. hpriv->ops->reset_bus(pdev, mmio);
  1724. hpriv->ops->enable_leds(hpriv, mmio);
  1725. for (port = 0; port < probe_ent->n_ports; port++) {
  1726. if (IS_60XX(hpriv)) {
  1727. void __iomem *port_mmio = mv_port_base(mmio, port);
  1728. u32 ifctl = readl(port_mmio + SATA_INTERFACE_CTL);
  1729. ifctl |= (1 << 12);
  1730. writelfl(ifctl, port_mmio + SATA_INTERFACE_CTL);
  1731. }
  1732. hpriv->ops->phy_errata(hpriv, mmio, port);
  1733. }
  1734. for (port = 0; port < probe_ent->n_ports; port++) {
  1735. void __iomem *port_mmio = mv_port_base(mmio, port);
  1736. mv_port_init(&probe_ent->port[port], port_mmio);
  1737. }
  1738. for (hc = 0; hc < n_hc; hc++) {
  1739. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  1740. VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
  1741. "(before clear)=0x%08x\n", hc,
  1742. readl(hc_mmio + HC_CFG_OFS),
  1743. readl(hc_mmio + HC_IRQ_CAUSE_OFS));
  1744. /* Clear any currently outstanding hc interrupt conditions */
  1745. writelfl(0, hc_mmio + HC_IRQ_CAUSE_OFS);
  1746. }
  1747. /* Clear any currently outstanding host interrupt conditions */
  1748. writelfl(0, mmio + PCI_IRQ_CAUSE_OFS);
  1749. /* and unmask interrupt generation for host regs */
  1750. writelfl(PCI_UNMASK_ALL_IRQS, mmio + PCI_IRQ_MASK_OFS);
  1751. writelfl(~HC_MAIN_MASKED_IRQS, mmio + HC_MAIN_IRQ_MASK_OFS);
  1752. VPRINTK("HC MAIN IRQ cause/mask=0x%08x/0x%08x "
  1753. "PCI int cause/mask=0x%08x/0x%08x\n",
  1754. readl(mmio + HC_MAIN_IRQ_CAUSE_OFS),
  1755. readl(mmio + HC_MAIN_IRQ_MASK_OFS),
  1756. readl(mmio + PCI_IRQ_CAUSE_OFS),
  1757. readl(mmio + PCI_IRQ_MASK_OFS));
  1758. done:
  1759. return rc;
  1760. }
  1761. /**
  1762. * mv_print_info - Dump key info to kernel log for perusal.
  1763. * @probe_ent: early data struct representing the host
  1764. *
  1765. * FIXME: complete this.
  1766. *
  1767. * LOCKING:
  1768. * Inherited from caller.
  1769. */
  1770. static void mv_print_info(struct ata_probe_ent *probe_ent)
  1771. {
  1772. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  1773. struct mv_host_priv *hpriv = probe_ent->private_data;
  1774. u8 rev_id, scc;
  1775. const char *scc_s;
  1776. /* Use this to determine the HW stepping of the chip so we know
  1777. * what errata to workaround
  1778. */
  1779. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  1780. pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
  1781. if (scc == 0)
  1782. scc_s = "SCSI";
  1783. else if (scc == 0x01)
  1784. scc_s = "RAID";
  1785. else
  1786. scc_s = "unknown";
  1787. dev_printk(KERN_INFO, &pdev->dev,
  1788. "%u slots %u ports %s mode IRQ via %s\n",
  1789. (unsigned)MV_MAX_Q_DEPTH, probe_ent->n_ports,
  1790. scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
  1791. }
  1792. /**
  1793. * mv_init_one - handle a positive probe of a Marvell host
  1794. * @pdev: PCI device found
  1795. * @ent: PCI device ID entry for the matched host
  1796. *
  1797. * LOCKING:
  1798. * Inherited from caller.
  1799. */
  1800. static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1801. {
  1802. static int printed_version = 0;
  1803. struct ata_probe_ent *probe_ent = NULL;
  1804. struct mv_host_priv *hpriv;
  1805. unsigned int board_idx = (unsigned int)ent->driver_data;
  1806. void __iomem *mmio_base;
  1807. int pci_dev_busy = 0, rc;
  1808. if (!printed_version++)
  1809. dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
  1810. rc = pci_enable_device(pdev);
  1811. if (rc) {
  1812. return rc;
  1813. }
  1814. rc = pci_request_regions(pdev, DRV_NAME);
  1815. if (rc) {
  1816. pci_dev_busy = 1;
  1817. goto err_out;
  1818. }
  1819. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  1820. if (probe_ent == NULL) {
  1821. rc = -ENOMEM;
  1822. goto err_out_regions;
  1823. }
  1824. memset(probe_ent, 0, sizeof(*probe_ent));
  1825. probe_ent->dev = pci_dev_to_dev(pdev);
  1826. INIT_LIST_HEAD(&probe_ent->node);
  1827. mmio_base = pci_iomap(pdev, MV_PRIMARY_BAR, 0);
  1828. if (mmio_base == NULL) {
  1829. rc = -ENOMEM;
  1830. goto err_out_free_ent;
  1831. }
  1832. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  1833. if (!hpriv) {
  1834. rc = -ENOMEM;
  1835. goto err_out_iounmap;
  1836. }
  1837. memset(hpriv, 0, sizeof(*hpriv));
  1838. probe_ent->sht = mv_port_info[board_idx].sht;
  1839. probe_ent->host_flags = mv_port_info[board_idx].host_flags;
  1840. probe_ent->pio_mask = mv_port_info[board_idx].pio_mask;
  1841. probe_ent->udma_mask = mv_port_info[board_idx].udma_mask;
  1842. probe_ent->port_ops = mv_port_info[board_idx].port_ops;
  1843. probe_ent->irq = pdev->irq;
  1844. probe_ent->irq_flags = SA_SHIRQ;
  1845. probe_ent->mmio_base = mmio_base;
  1846. probe_ent->private_data = hpriv;
  1847. /* initialize adapter */
  1848. rc = mv_init_host(pdev, probe_ent, board_idx);
  1849. if (rc) {
  1850. goto err_out_hpriv;
  1851. }
  1852. /* Enable interrupts */
  1853. if (pci_enable_msi(pdev) == 0) {
  1854. hpriv->hp_flags |= MV_HP_FLAG_MSI;
  1855. } else {
  1856. pci_intx(pdev, 1);
  1857. }
  1858. mv_dump_pci_cfg(pdev, 0x68);
  1859. mv_print_info(probe_ent);
  1860. if (ata_device_add(probe_ent) == 0) {
  1861. rc = -ENODEV; /* No devices discovered */
  1862. goto err_out_dev_add;
  1863. }
  1864. kfree(probe_ent);
  1865. return 0;
  1866. err_out_dev_add:
  1867. if (MV_HP_FLAG_MSI & hpriv->hp_flags) {
  1868. pci_disable_msi(pdev);
  1869. } else {
  1870. pci_intx(pdev, 0);
  1871. }
  1872. err_out_hpriv:
  1873. kfree(hpriv);
  1874. err_out_iounmap:
  1875. pci_iounmap(pdev, mmio_base);
  1876. err_out_free_ent:
  1877. kfree(probe_ent);
  1878. err_out_regions:
  1879. pci_release_regions(pdev);
  1880. err_out:
  1881. if (!pci_dev_busy) {
  1882. pci_disable_device(pdev);
  1883. }
  1884. return rc;
  1885. }
  1886. static int __init mv_init(void)
  1887. {
  1888. return pci_module_init(&mv_pci_driver);
  1889. }
  1890. static void __exit mv_exit(void)
  1891. {
  1892. pci_unregister_driver(&mv_pci_driver);
  1893. }
  1894. MODULE_AUTHOR("Brett Russ");
  1895. MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
  1896. MODULE_LICENSE("GPL");
  1897. MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
  1898. MODULE_VERSION(DRV_VERSION);
  1899. module_init(mv_init);
  1900. module_exit(mv_exit);