sata_mv.c 59 KB

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